int main(void) { char done; printf("This is the game of Tic Tac Toe.\n"); printf("You will be playing against the computer.\n"); done = ' '; init_matrix(); do { disp_matrix(); get_player_move(); done = check(); /* see if winner */ if(done!= ' ') break; /* winner!*/ get_computer_move(); done = check(); /* see if winner */ } while(done== ' '); if(done=='X') printf("You won!\n"); else printf("I won!!!!\n"); disp_matrix(); /* show final positions */ return 0; }
void get_player_move(void) { int x, y,i,j,n; printf("Enter X,Y coordinates for Player O: "); scanf("%d%*c%d", &x, &y); int temp1=htonl(x); int temp2=htonl(y); n=write(newsockfd,&temp1,sizeof(temp1)); if(n<0) error("write failed"); n=write(newsockfd,&temp2,sizeof(temp2)); if(n<0) error("write failed"); if(n=0) error("client closed"); x--; y--; if(matrix[x][y]!= ' '){ printf("Invalid move, try again.\n"); get_player_move(); } else matrix[x][y] = 'O'; counter++; }
/* Get a player's move. */ void get_player_move(void) { int x, y; printf("Enter X,Y coordinates for your move: "); scanf("%d%*c%d", &x, &y); x--; y--; if(matrix[x][y]!= ' '){ printf("Invalid move, try again.\n"); get_player_move(); } else matrix[x][y] = 'X'; }
//int main(int argc, char *argv[], char *env[]) void main(void) { int i, j; mvtype movemade; mousex=100; mousey=100; iconmenu=0; set_default_options(); options=doptions; read_options(); initialise(startbrd); initial_displays(current); no_more_openings=FALSE; quit=game_over=0; g_path.move[0].f=0; randomize(); FLAGnewstart=1; while(!quit) { while(game.movenum<maxmoves && !quit) { whites=0; blacks=0; if (FLAGnewstart) { starttime=time(NULL); FLAGnewstart=0; } newtime=0; if (!game_over && ((current.mvr=='w' && options.whiteplayer==PROGRAM) || (current.mvr=='b' && options.blackplayer==PROGRAM))) movemade=get_computer_move(current); else movemade=get_player_move(current, game_over); if (kbhit()) { if (getch()==27) { options.whiteplayer=USER; options.blackplayer=USER; } } if (current.mvr=='w') (whites=time(NULL)-starttime); else (blacks=time(NULL)-starttime); if (movemade.t!=0) if (movemade.t<100) makemv(movemade); else {menu_option(movemade.t); game_over=0;} regular_displays(current, movemade); if (!game_over) game_over=over(); assert(game.lastmove<maxmoves); } options.whiteplayer=USER; options.blackplayer=USER; } }
int main(int argc, char *argv[]) { int portno; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr =inet_addr("127.0.0.1"); //INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); puts("This is the game of Tic Tac Toe.\n"); puts("waiting connection to established....\n"); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); char done; done = ' '; init_matrix(); do { disp_matrix(); puts("waiting for client move\n"); get_client_move(); disp_matrix(); done = check(); /* see if winner */ if(done!= ' ') break; /* winner!*/ get_player_move(); disp_matrix(); done = check(); /* see if winner */ } while(done== ' '); if(done=='X') printf("Player X won!\n"); else printf("Player O won!!!!\n"); disp_matrix(); /* show final positions */ close(newsockfd); close(sockfd); return 0; }