Skip to content

Commit

Permalink
finish bonus
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouliang-yu committed Oct 19, 2021
1 parent 4aae628 commit a5e8d59
Show file tree
Hide file tree
Showing 14 changed files with 628 additions and 153 deletions.
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

2 changes: 0 additions & 2 deletions .idea/operatSystem.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

16 changes: 0 additions & 16 deletions Assignment_1_120040077/source/.vscode/c_cpp_properties.json

This file was deleted.

225 changes: 116 additions & 109 deletions Assignment_2_120040077/source/hw2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
#include <fcntl.h>

#define ROW 10
#define COLUMN 50
#define THREADNUM 11
#define COLUMN 50
#define THREADNUM 10
bool isWin = 0;
bool isLose = 0;
bool isExit = 0;

pthread_mutex_t mutex;
pthread_cond_t cond;
int logs_speed[ROW - 1];
int logs_size[ROW -1];
int left_end[ROW - 1];
int right_end[ROW - 1];
int logs_speed[ROW + 10];
int logs_size[ROW + 10];
int left_end[ROW + 10];
int right_end[ROW + 10];

struct Node{
int x , y;
Node( int _x , int _y ) : x( _x ) , y( _y ) {};
Node(){} ;
} frog ;
} frog, speed;


char map[ROW+10][COLUMN] ;
Expand Down Expand Up @@ -65,135 +65,124 @@ int kbhit(void){

void *logs_move( void *t ){
/* initilize the setting of logs*/
int *log_id = (int*) t;
long log_id;
log_id = (long) t;

while (isExit == 0 && isWin == 0 && isLose == 0) {

while (!isExit) {
pthread_mutex_lock(&mutex);

/* draw the river*/
for (int i = 0; i < COLUMN - 1; i++) {
map[*log_id][i] = ' ';
}

if (*log_id % 2){

if (log_id % 2){
/* odd row move from left to right*/
right_end[*log_id] = (right_end[*log_id] + 1) % (COLUMN - 1);
left_end[*log_id] = (left_end[*log_id] + 1) % (COLUMN - 1);
left_end[log_id] = (left_end[log_id] + 1) % (COLUMN - 1);
}
else {
/* even row move from right to left*/
left_end[*log_id] = (left_end[*log_id] - 1) % (COLUMN - 1);
right_end[*log_id] = (right_end[*log_id] - 1) % (COLUMN - 1);
left_end[log_id] = left_end[log_id] - 1;
if (left_end[log_id] < 0) left_end[log_id] += COLUMN - 1;
}

if (right_end[*log_id] > left_end[*log_id]) { // the log is crossing the right boundary
for (int j = left_end[*log_id]; j < logs_size[*log_id]; j ++) {
map[*log_id][j] = '=';
}
}else { // the log is crossing the boundary
for (int j = left_end[*log_id]; j < COLUMN - 1; j ++) {
map[*log_id][j] = '=';
}
for (int k = 0; k < right_end[*log_id]; k++) {
map[*log_id][k] = '=';
}
}
pthread_mutex_lock(&mutex);

/* draw the two sides*/
for (int j = 0; j < COLUMN - 1; j ++) {
map[ROW][j] = '|';
map[0][j] = '|';
/* draw the river*/
for (int j = 0; j < COLUMN - 1; ++j) {
map[log_id][j] = ' ';
}

/* Check keyboard hits, to change frog's position or quit the game. */
if (kbhit()) {
char dir = getchar();
if ( dir == 'w' || dir == 'W') {
frog.x = frog.x + 1;
}
if ( dir == 'a' || dir == 'A') {
frog.y = frog.y - 1;
}
if ( (dir == 's' || dir == 'S') && frog.x != ROW) {
frog.x = frog.x - 1;
}
if (dir == 'd' || dir == 'D') {
frog.y = frog.y + 1;
}
if (dir == 'q' || dir == 'Q') {
isExit = 1;
}
for (int i = 0, j = left_end[log_id]; i < logs_size[log_id]; ++j, ++i) {
map[log_id][j%(COLUMN - 1)] = '=';
}

map[frog.x][frog.y] = '0';
/* draw the two sides*/
for (int j = 0; j < COLUMN - 1; j ++) {
map[ROW][j] = map[0][j] = '|' ;
}

/* Check game's status */
if (map[frog.x][frog.y] == ' ') { // the flog falls to the river
isExit = 1;
}
for( int j = 0; j < COLUMN - 1; ++j )
map[ROW + 1][j] = '-';

if (frog.y <= 0) { //touches the right boundary
isExit = 1;
isLose = 1;
}

if (frog.y >= COLUMN - 1) { //touches the left boundary
isExit = 1;
isLose = 1;
}

if (frog.x == 0) {
isExit = 1;
isWin = 1;
}
/* Print the map on the screen */
/* Check keyboard hits, to change frog's position or quit the game. */
if (kbhit()) {
char dir = getchar();
if ( dir == 'w' || dir == 'W') {
frog.x = frog.x - 1;
}
if ( (dir == 'a' || dir == 'A') && frog.y != 0) {
frog.y = frog.y - 1;
}
if ( (dir == 's' || dir == 'S') && frog.x != ROW) {
frog.x = frog.x + 1;
}
if ((dir == 'd' || dir == 'D') && frog.y!= COLUMN - 2) {
frog.y = frog.y + 1;
}
if (dir == 'q' || dir == 'Q') {
isExit = 1;
}

/* switch the speed*/
if ((dir == 'j' || dir == 'J') && speed.y != 0) {
speed.y = speed.y - 1;
}
if ((dir == 'k' || dir == 'K') && speed.y != COLUMN - 2) {
speed.y = speed.y + 1;
}
}

/* Check game's status */
if (map[frog.x][frog.y] == ' ') { // the flog falls to the river
isLose = 1;
}

printf("\033c");
//Print the map into screen
for( int i = 0; i <= ROW; ++i) {
puts( map[i] );
}
usleep(1000);
}
pthread_mutex_unlock(&mutex);
usleep(logs_speed[*log_id] * 6000);
pthread_exit(NULL);
}
if (frog.y <= 0) { //touches the left boundary
isLose = 1;
}

void *log_ini() {
/* initilize the setting of logs*/
srand((unsigned int)time(NULL));
if (frog.y >= COLUMN - 1) { //touches the right boundary
isLose = 1;
}

for (int i = 1; i < ROW; i ++) {
logs_speed[i] = rand()%20 + 10;
logs_size[i] = rand()%5 + 13;
left_end[i] = rand() % (COLUMN - 1);
right_end[i] = (left_end[i] + logs_size[i]) % (COLUMN - 1);
if (frog.x == 0) {
isWin = 1;
}

if (right_end[i] > left_end[i]) {
for (int k = left_end[i]; k < right_end[i]; k++) {
map[i][k] = '=';
}
}else {
for (int k = left_end[i]; k < COLUMN - 1; k ++) {
map[i][k] = '=';
if (isExit == 0 && isWin == 0 && isLose == 0) {
/* Print the map on the screen */
if (frog.x == log_id && map[frog.x][frog.y] == '=') {
if (frog.x % 2) frog.y ++;
else frog.y --;
}
for (int j = 0; j < right_end[i]; j ++) {
map[i][j] = '=';
}
}
}

// printf("\033[0;0h\033[2J");
printf("\033[H\033[2J");
//Print the map into screen
usleep(1000);
map[frog.x][frog.y] = '0';
map[speed.x][speed.y] = '>';
for( int i = 0; i <= ROW + 1; ++i) {
puts( map[i] );
}
}

pthread_mutex_unlock(&mutex);
usleep(logs_speed[log_id] * 6000 * (COLUMN - 1 - speed.y)/ (COLUMN - 1));
}

pthread_exit(NULL);
}





int main( int argc, char *argv[] ){

pthread_t threads[THREADNUM];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
srand((unsigned int)time(NULL));

// Initialize the river map and frog's starting position
memset( map , 0, sizeof( map ) ) ;
Expand All @@ -203,36 +192,55 @@ int main( int argc, char *argv[] ){
for( i = 1; i < ROW; ++i ){
for( j = 0; j < COLUMN - 1; ++j )
map[i][j] = ' ' ;
logs_speed[i] = rand()%20 + 10;
logs_size[i] = rand()%5 + 13;
left_end[i] = rand() % (COLUMN - 1);
}


/* initilize the setting of logs*/


for( j = 0; j < COLUMN - 1; ++j )
map[ROW][j] = map[0][j] = '|' ;


/* set the speed changing slide bar*/
for( j = 0; j < COLUMN - 1; ++j )
map[0][j] = map[0][j] = '|' ;
map[ROW + 1][j] = '-';

speed = Node(ROW + 1, 1);
map[speed.x][speed.y] = '>';

frog = Node( ROW, (COLUMN-1) / 2 ) ;
map[frog.x][frog.y] = '0' ;

/* initialize the logs*/
log_ini();
/* set the speed changing slide bar*/



//Print the map into screen
for( i = 0; i <= ROW; ++i) {
for( i = 0; i <= ROW + 1; ++i) {
puts( map[i] );
}



/* Create pthreads for logs move and frog control. */
for (int i = 1; i < THREADNUM; ++ i) {
pthread_create(&threads[i], NULL, logs_move, (void*) i);
int rc;
rc = pthread_create(&threads[i], NULL, logs_move, (void*)i);
if (rc) {
printf("ERROR: return code from pthread_create() is %d",rc);
exit(1);
}
usleep(200);
}

for (int i = 1; i < THREADNUM; ++i) {
pthread_join(threads[i], NULL);
}

printf("\033[0;0H\033[2J\033[?25h");
usleep(1000) ;

Expand All @@ -253,7 +261,6 @@ int main( int argc, char *argv[] ){


pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
pthread_exit(NULL);

return 0;
Expand Down
5 changes: 5 additions & 0 deletions ass2report/Report.aux
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\relax
\@writefile{toc}{\contentsline {section}{\numberline {1}What I Learnt From the task}{1}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {1.1}-lpthread}{1}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {1.2}pthread\_join}{1}{}\protected@file@percent }
\gdef \@abspage@last{1}
Loading

0 comments on commit a5e8d59

Please sign in to comment.