/* Try to find a good move for the specified colour */ int suggest_move(c4_t board, char colour) { int c; /* look for a winning move for colour */ for (c=0; c<WIDTH; c++) { /* temporarily move in column c... */ if (do_move(board, c+1, colour)) { /* ... and check to see the outcome */ if (winner_found(board) == colour) { /* it is good, so unassign and return c */ undo_move(board, c+1); return c+1; } else { undo_move(board, c+1); } } } /* ok, no winning move, look for a blocking move */ if (colour == RED) { colour = YELLOW; } else { colour = RED; } for (c=0; c<WIDTH; c++) { /* temporarily move in column c... */ if (do_move(board, c+1, colour)) { /* ... and check to see the outcome */ if (winner_found(board) == colour) { /* it is good, so unassign and return c */ undo_move(board, c+1); return c+1; } else { undo_move(board, c+1); } } } /* no moves found? then pick at random... */ c = rand()%WIDTH; while (board[HEIGHT-1][c]!=EMPTY) { c = rand()%WIDTH; } return c+1; }
void *thread_game(void *threadInfo) { struct readThreadParams *thisTheadInfo = (struct readThreadParams *)threadInfo; FILE *fptr = thisTheadInfo->filep; int sock = thisTheadInfo->sock; int status,n; //connect 4 variables c4_t board; int clientMove, serverMove; //uint16 used so all systems can play! uint16_t clientMoveNewtorked, serverMoveNetworked; printLog(fptr,thisTheadInfo,"client connected" ,-1,CLIENT); //connect 4 actions here srand(RSEED); //create a new board init_empty(board); /* main loop does two moves each iteration, one from the human * playing, and then one from the computer program (or game server) */ while (1) { //first we want the clients move if( (n = recv(sock, &clientMove, sizeof(uint16_t), 0)) != sizeof(uint16_t) ) { printf("socket closed\n"); status = STATUS_ABNORMAL; printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT); break; } clientMoveNewtorked = htons(clientMove); //do move if (do_move(board, (int)clientMoveNewtorked, YELLOW)!=1) { printf("Panic\n"); status = STATUS_ABNORMAL; printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT); break; } //if move possible we write to log printLog(fptr, thisTheadInfo, "client's move", (int)clientMoveNewtorked,CLIENT); // print_config(board); if (winner_found(board) == YELLOW) { /* rats, the person beat us! */ printf("Petty human wins\n"); status = STATUS_USER_WON; printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT); break; } /* was that the last possible move? */ if (!move_possible(board)) { /* yes, looks like it was */ printf("An honourable draw\n"); status = STATUS_DRAW; printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT); break; } /* otherwise, look for a move from the computer */ serverMove = suggest_move(board, RED); serverMoveNetworked = htons(serverMove); /* then play the move */ // printf(" I play in column %d\n", serverMove); if(send(sock, &serverMoveNetworked,sizeof(uint16_t),0) != sizeof(uint16_t)) { printf("error\n"); status = STATUS_ABNORMAL; printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT); break; } if (do_move(board, serverMove, RED)!=1) { printf("Panic\n"); status = STATUS_ABNORMAL; printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT); break; } printLog(fptr, thisTheadInfo, "server's move", serverMove,SERVER); // print_config(board); /* and did we win??? */ if (winner_found(board) == RED) { /* yes!!! */ printf("Server wins again!\n"); status = STATUS_AI_WON; printLog(fptr, thisTheadInfo, "game over, code",status,CLIENT); break; } /* otherwise, the game goes on */ } return NULL; }
/*------------------------- Main ---------------------*/ int main(int argc, char *argv[]) { /* Ensure enough arguments are passed */ if(argc < 3) { fprintf(stderr, "Usage: %s host port.\n", argv[0]); exit(EXIT_FAILURE); } /* Initialize a socket to communicate to the server */ int socket_fd = initialize_client_socket(argv[1], atoi(argv[2])); /* Begin the connect4 game against the server. */ /* The data structures required for this game */ c4_t board; int move, n; init_empty(board); print_config(board); /* This loop does 2 moves each iteration. One for the * human player and one for the server. */ while ((move = get_move(board)) != EOF) { /* process the person's move */ if (do_move(board, move, YELLOW)!=1) { printf("Panic\n"); break; } /* Send the move to the server */ int converted_move = htonl(move); n = write(socket_fd, &converted_move, sizeof(converted_move)); if(n < 0) { perror("Failed to write to socket.\n"); break; } print_config(board); /* and did they win??? */ if (winner_found(board) == YELLOW) { /* rats, the person beat us! */ printf("Ok, you beat me, beginner's luck!\n"); break; } /* was that the last possible move? */ if (!move_possible(board)) { /* yes, looks like it was */ printf("An honourable draw\n"); break; } /* Ask the server for its move */ n = read(socket_fd, &move, sizeof(move)); move = ntohl(move); if(n < 0) { perror("Failed to read from socket.\n"); break; } /* pretend to be thinking hard */ printf("Ok, let's see now...."); sleep(1); /* then play the move */ printf(" I play in column %d\n", move); if (do_move(board, move, RED)!=1) { printf("Panic\n"); break; } print_config(board); /* and did we win??? */ if (winner_found(board) == RED) { /* yes!!! */ printf("I guess I have your measure!\n"); break; } /* and did they win??? */ if (winner_found(board) == YELLOW) { /* rats, the person beat us! */ printf("Ok, you beat me, beginner's luck!\n"); break; } /* Was that the last possible move? */ if (!move_possible(board)) { /* yes, looks like it was */ printf("An honourable draw\n"); break; } /* otherwise, the game goes on */ } printf("FINALMOVE=%d", move); printf("\n"); close(socket_fd); return 0; }