void mouse(int button, int state, int x, int y) { // We convert windows mouse coords to out openGL coords mouse_x = (18 * (float) ((float)x/(float)Win_x))/6; mouse_y = (18 * (float) ((float)y/(float)Win_y))/6; // What square have they clicked in? object_select = mouse_x + mouse_y * 3; if ( start_game == 0) { if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) { player = 1; computer = -1; init_game(); return; } if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) { player = -1; computer = 1; init_game(); computer_move(); return; } } if ( start_game == 1) { if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) { if (win == 0) { if (box_map[ object_select ] == 0) { box_map[ object_select ] = player; win = check_move(); if (win == 1) { start_game = 0; return; } computer_move(); win = check_move(); if (win == 1) { win = -1; start_game = 0; } } } } } if ( win == 2 )start_game = 0; }
void play_game() { char name[50]; printf("Hi!\n\nI am Z3TA.\n\nWhat's your name: "); scanf("%s", name); int play_first = 0; printf("\n\nDo you want to play first? 1 - Yes, 0 - No: "); scanf("%d", &play_first); printf("\n\n\nX: Z3TA\nO: %s\n\n\n",name); if(!play_first) { while(!board_full(board) && !check_win(board)) { computer_move(); printf("Z3TA just made a move...\n"); print_board(); if(board_full(board) || check_win(board)) { break; } player_move(); printf("%s just made a move...\n", name); print_board(); } } else { while(!board_full(board) && !check_win(board)) { player_move(); printf("%s just made a move...\n", name); print_board(); if(board_full(board) || check_win(board)) { break; } computer_move(); printf("Z3TA just made a move...\n"); print_board(); } } printf("\n\n------------------------------\n\n"); if(check_win(board) == 0) { printf("It's a DRAW!\n"); } else if(check_win(board) == 1) { printf("%s WON!\n", name); } else { printf("Z3TA WON!\n"); } printf("\n------------------------------\n"); }
int main(void){ int choice; char done; printmenu(); printf("play??\n"); scanf("%d, &choice"); if(choice == 2) return; done = ' '; init_matrix(); do{ display_matrix(); player_move(); /* see if player is winner */ done = check(); if(done != ' ') break; /* player is winner!*/ computer_move(); done = check(); /* see if computer is winner */ } while(done == ' '); if(done == 'X') printf("-----YOU WON!!!-----\n"); else{ printf("-----I WON!!!!-----\n"); printf("-----OOPS!!! YOU LOSE...TRY NEXT TIME-----\n"); printmenu(); } display_matrix(); /* show final positions */ return 0; }
int main(void) { TicTacToe game; int size; printf("This is the game of Tic Tac Toe.\n"); printf("You will be playing against the computer.\n"); printf("Enter the size of the board: "); scanf("%d", &size); printf("The game board is %d by %d.\n", size, size); init_game(&game, size); // initialise the board int done; do { print_game(game); do { done = player_move(&game); } while (!done); // loop until valid move if(check(&game) != FALSE) break; // was a winner or a draw computer_move(&game); if(check(&game) != FALSE) break; // was a winner or a draw } while (TRUE); print_result(game); print_game(game); // show final positions free_game(&game); return 0; }
void play_y() { player = -1; computer = 1; init_game(); computer_move(); }
int main(void) { char board[9]; char player = who_plays_first(); //funkcja losuje kto gra pierwszy char move; system("cls"); fill_in_board(board); //wypelniamy tablice liczbami od 1 do 9 do { if(player == 'x') { player = 'o'; //zmieniamy gracz display_board(board); do { move = read_move(player); //odczytujemy ruch gracza } while(!check_move_possibility(move, board)); } else { player = 'x'; //zmieniamy gracza move = computer_move(board, player); system("cls"); printf("Komputer wybral %d.\n\n", move + 1); } save_move(move, player, board); } while(!is_it_end(board)); //powtarzamy az do konca gry print_result(board, player); play_again(); return 0; }
/*----------------------------------------------------------------------------*/ void set_rating_handicap (double rating) { int h, handicap, i, j; YT_GOBAN *goban = pa->goban; handicap = (int) (fabs(rating)); if (rating >= 0) { // первый ход - компутер for (h = 0; h < handicap; h++) { //computer_move (); //if (goban_random_stone_new (goban, &i, &j)) if (goban_random_handicap (goban, handicap, &i, &j)) goban_set_move (goban, i, j, STONE_WHITE); } //////////// } else { //////////// for (h = 0; h < handicap; h++) { //if (goban_random_stone_new (goban, &i, &j)) // не сработает только, если нет места if (goban_random_handicap (goban, handicap, &i, &j)) // не сработает только, если нет места human_move (i, j); } computer_move (STONE_WHITE); } return; }
/*----------------------------------------------------------------------------*/ void baby_onestep_moves (int i, int j) { if (i == -1 && j == -1) { // комп играет сам с собой, т.е. делает ход за человека if (! computer_move (STONE_BLACK)) return; } else { if (! human_move (i, j)) {return; OUTD (555);} } if (update_check_end ()) return; //draw_status ("Я думаю.."); if (! computer_move (STONE_WHITE)) return; if (update_check_end ()) return; //draw_status ("Ваш ход.."); return; }
/* Loop through 9 turns or until somebody wins. */ void play_game(void) { int turn; for (turn = 1; turn <= 9; turn++) { /* Check if turn is even or odd to determine which player should move. */ if (turn % 2 == 1) { if (computer == 'X') computer_move(); else player_move(); } else { if (computer == 'O') computer_move(); else player_move(); } draw_board(); if (symbol_won(computer)) { printf("\nI WIN!!!\n\n"); return; } else if (symbol_won(user)) { printf("\nCongratulations, you win!\n\n"); return; } } printf("\nThe game is a draw.\n\n"); return; }
int main() { int i,j; char again[2],winner,**board; board=(char**)malloc(sizeof(char*)*3); for (i=0; i<3; i++) { board[i]=(char*)malloc(sizeof(char)*3); } printf("\nWelcome to a game of Tic-Tac-Toe!\n"); do { board=initboard(board); displayboard(board); do { board=human_move(board,PLAYER1); displayboard(board); winner=checkwin(board,PLAYER1); if (winner!=PLAYER1 && winner!=' ') { if (computer_move(board,PLAYER2,PLAYER1)) { winner=checkwin(board,PLAYER2); } else { printf("\nComputer passed\n"); } displayboard(board); } } while (winner==BLANK); if (winner!=' ') { printf("THE WINNER IS %c!\n",winner); } else { printf("IT'S A TIE.\n"); } printf("\nPlay again (y)?: "); scanf("%s",again); } while(again[0]=='y'); for (i=0; i<3; i++) { free(board[i]); } free(board); printf("\nThank you for playing.\n\n"); return 0; }
static int do_pass(Gameinfo *gameinfo, int *passes, int force) { (*passes)++; init_sgf(gameinfo); gnugo_play_move(PASS_MOVE, gameinfo->to_move); sgffile_add_debuginfo(sgftree.lastnode, 0.0); sgftreeAddPlay(&sgftree, gameinfo->to_move, -1, -1); sgffile_output(&sgftree); gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); if (force) { gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player); sgftreeAddComment(&sgftree, "forced"); return 0; } return computer_move(gameinfo, passes); }
double player_move(const Grid& grid, Cache& cache,int depth) { if (depth == 0) // End of branch { return has_move(grid) ? evaluate_heuristic(grid) : 0; // has_move(grid) Penalizes dead end } double best_score = 0; for(int direction = 0; direction < 4; direction++){ Grid computer_grid = grid; move(computer_grid, direction); if (computer_grid == grid) // No change due to movement { continue; // Skip to next direction } double computer_score = 0; // Pruning Cache::const_iterator iter = cache.find(computer_grid); if (iter != cache.end()) { computer_score = iter->second; } else { computer_score = computer_move(computer_grid, depth - 1); cache[computer_grid] = computer_score; } if (computer_score > best_score){ best_score = computer_score; } } return best_score; }
static int do_move(Gameinfo *gameinfo, char *command, int *passes, int force) { int move = string_to_location(board_size, command); if (move == NO_MOVE) { printf("\nInvalid move: %s\n", command); return 0; } if (!is_allowed_move(move, gameinfo->to_move)) { printf("\nIllegal move: %s", command); return 0; } *passes = 0; TRACE("\nyour move: %1m\n\n", move); init_sgf(gameinfo); gnugo_play_move(move, gameinfo->to_move); sgffile_add_debuginfo(sgftree.lastnode, 0.0); sgftreeAddPlay(&sgftree, gameinfo->to_move, I(move), J(move)); sgffile_output(&sgftree); if (opt_showboard) { ascii_showboard(); printf("GNU Go is thinking...\n"); } if (force) { gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player); gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); sgftreeAddComment(&sgftree, "forced"); return 0; } gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); return computer_move(gameinfo, passes); }
int best_direction(const Grid& grid, int depth) { double best_score = 0; int best_dir = -1; for(int direction = 0; direction < 4; direction++){ Grid computer_grid = grid; move(computer_grid, direction); if (computer_grid == grid) // No change due to movement { continue; } double computer_score = computer_move(computer_grid, 2 * depth - 1); if (computer_score >= best_score){ // Equality : Forces a move even when deadend is expected best_score = computer_score; best_dir = direction; } } return best_dir; }
void do_play_ascii(Gameinfo *gameinfo) { int m, num; float fnum; int passes = 0; /* two passes and its over */ int tmp; char line[80]; char *line_ptr = line; char *command; char *tmpstring; int state = 1; if (have_time_settings()) clock_on = 1; while (state == 1) { state = 0; /* No score is estimated yet. */ current_score_estimate = NO_SCORE; /* Allow resignation at interface level (the engine may still be not * allowed to resign. */ resignation_allowed = 1; printf("\nBeginning ASCII mode game.\n\n"); gameinfo_print(gameinfo); /* Does the computer play first? If so, make a move. */ if (gameinfo->computer_player == gameinfo->to_move) state = computer_move(gameinfo, &passes); /* main ASCII Play loop */ while (state == 0) { /* Display game board. */ if (opt_showboard) ascii_showboard(); #if !READLINE /* Print the prompt */ mprintf("%s(%d): ", color_to_string(gameinfo->to_move), movenum + 1); /* Read a line of input. */ line_ptr = line; if (!fgets(line, 80, stdin)) return; #else snprintf(line, 79, "%s(%d): ", color_to_string(gameinfo->to_move), movenum + 1); if (!(line_ptr = readline(line))) return; add_history(line_ptr); #endif while (state == 0 && (command = strtok(line_ptr, ";"), line_ptr = 0, command)) { /* Get the command or move. */ switch (get_command(command)) { case RESIGN: state = ascii_endgame(gameinfo, 1); break; case END: case EXIT: case QUIT: return; case HELP: show_commands(); break; case CMD_HELPDEBUG: printf(DEBUG_COMMANDS); break; case SHOWBOARD: opt_showboard = !opt_showboard; break; case INFO: printf("\n"); gameinfo_print(gameinfo); break; case SETBOARDSIZE: if (sgf_initialized) { printf("Boardsize cannot be changed after record is started!\n"); break; } command += 10; if (sscanf(command, "%d", &num) != 1) { printf("\nInvalid command syntax!\n"); break; } if (!check_boardsize(num, stdout)) break; /* Init board. */ board_size = num; clear_board(); /* In case max handicap changes on smaller board. */ gameinfo->handicap = place_fixed_handicap(gameinfo->handicap); sgfOverwritePropertyInt(sgftree.root, "SZ", board_size); sgfOverwritePropertyInt(sgftree.root, "HA", gameinfo->handicap); break; case SETHANDICAP: if (sgf_initialized) { printf("Handicap cannot be changed after game is started!\n"); break; } command += 9; if (sscanf(command, "%d", &num) != 1) { printf("\nInvalid command syntax!\n"); break; } if (num < 0 || num > MAX_HANDICAP) { printf("\nInvalid handicap: %d\n", num); break; } /* Init board. */ clear_board(); /* Place stones on board but don't record sgf * in case we change more info. */ gameinfo->handicap = place_fixed_handicap(num); printf("\nSet handicap to %d\n", gameinfo->handicap); gameinfo->to_move = (gameinfo->handicap ? WHITE : BLACK); break; case FREEHANDICAP: if (sgf_initialized) { printf("Handicap cannot be changed after game is started!\n"); break; } while (*command && *command != ' ') command++; ascii_free_handicap(gameinfo, command); break; case SETKOMI: if (sgf_initialized) { printf("Komi cannot be modified after game record is started!\n"); break; } command += 5; if (sscanf(command, "%f", &fnum) != 1) { printf("\nInvalid command syntax!\n"); break; } komi = fnum; printf("\nSet Komi to %.1f\n", komi); break; case SETDEPTH: command += 6; if (sscanf(command, "%d", &num) != 1) { printf("\nInvalid command syntax!\n"); break; } mandated_depth = num; printf("\nSet depth to %d\n", mandated_depth); break; case SETLEVEL: command += 6; if (sscanf(command, "%d", &num) != 1) { printf("\nInvalid command syntax!\n"); break; } set_level(num); printf("\nSet level to %d\n", num); break; case DISPLAY: if (!opt_showboard) ascii_showboard(); break; case FORCE: command += 6; /* skip the force part... */ switch (get_command(command)) { case MOVE: state = do_move(gameinfo, command, &passes, 1); break; case PASS: state = do_pass(gameinfo, &passes, 1); break; default: printf("Illegal forced move: %s %d\n", command, get_command(command)); break; } break; case MOVE: state = do_move(gameinfo, command, &passes, 0); break; case PASS: state = do_pass(gameinfo, &passes, 0); break; case PLAY: command += 5; if (sscanf(command, "%d", &num) != 1) { printf("\nInvalid command syntax!\n"); break; } if (num >= 0) for (m = 0; m < num; m++) { gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player); state = computer_move(gameinfo, &passes); if (state) break; if (passes >= 2) break; } else { printf("\nInvalid number of moves specified: %d\n", num); break; } break; case PLAYBLACK: if (gameinfo->computer_player == WHITE) gameinfo->computer_player = BLACK; if (gameinfo->computer_player == gameinfo->to_move) state = computer_move(gameinfo, &passes); break; case PLAYWHITE: if (gameinfo->computer_player == BLACK) gameinfo->computer_player = WHITE; if (gameinfo->computer_player == gameinfo->to_move) state = computer_move(gameinfo, &passes); break; case SWITCH: gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player); state = computer_move(gameinfo, &passes); break; case UNDO: case CMD_BACK: if (undo_move(1)) { sgftreeAddComment(&sgftree, "undone"); sgftreeBack(&sgftree); gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); } else printf("\nCan't undo.\n"); break; case CMD_FORWARD: if (sgftreeForward(&sgftree)) gameinfo->to_move = gnugo_play_sgfnode(sgftree.lastnode, gameinfo->to_move); else printf("\nEnd of game tree.\n"); break; case CMD_LAST: while (sgftreeForward(&sgftree)) gameinfo->to_move = gnugo_play_sgfnode(sgftree.lastnode, gameinfo->to_move); break; case COMMENT: printf("\nEnter comment. Press ENTER when ready.\n"); fgets(line, 80, stdin); sgftreeAddComment(&sgftree, line); break; case SCORE: showscore = !showscore; if (!showscore) current_score_estimate = NO_SCORE; break; case CMD_DEAD: silent_examine_position(FULL_EXAMINE_DRAGONS); showdead = !showdead; break; case CMD_CAPTURE: strtok(command, " "); showcapture(strtok(NULL, " ")); break; case CMD_DEFEND: strtok(command, " "); showdefense(strtok(NULL, " ")); break; case CMD_SHOWMOYO: tmp = printmoyo; printmoyo = PRINTMOYO_MOYO; silent_examine_position(EXAMINE_DRAGONS); printmoyo = tmp; break; case CMD_SHOWTERRI: tmp = printmoyo; printmoyo = PRINTMOYO_TERRITORY; silent_examine_position(EXAMINE_DRAGONS); printmoyo = tmp; break; case CMD_SHOWAREA: tmp = printmoyo; printmoyo = PRINTMOYO_AREA; silent_examine_position(EXAMINE_DRAGONS); printmoyo = tmp; break; case CMD_SHOWDRAGONS: silent_examine_position(EXAMINE_DRAGONS); showboard(1); break; case CMD_GOTO: strtok(command, " "); ascii_goto(gameinfo, strtok(NULL, " ")); break; case CMD_SAVE: strtok(command, " "); tmpstring = strtok(NULL, " "); if (tmpstring) { /* discard newline */ tmpstring[strlen(tmpstring) - 1] = 0; /* make sure we are saving proper handicap */ init_sgf(gameinfo); writesgf(sgftree.root, tmpstring); printf("You may resume the game"); printf(" with -l %s --mode ascii\n", tmpstring); printf("or load %s\n", tmpstring); } else printf("Please specify filename\n"); break; case CMD_LOAD: strtok(command, " "); tmpstring = strtok(NULL, " "); if (tmpstring) { /* discard newline */ tmpstring[strlen(tmpstring) - 1] = 0; if (!sgftree_readfile(&sgftree, tmpstring)) { fprintf(stderr, "Cannot open or parse '%s'\n", tmpstring); break; } /* to avoid changing handicap etc. */ if (gameinfo_play_sgftree(gameinfo, &sgftree, NULL) == EMPTY) fprintf(stderr, "Cannot load '%s'\n", tmpstring); else { sgf_initialized = 1; sgfOverwritePropertyInt(sgftree.root, "HA", gameinfo->handicap); } } else printf("Please specify a filename\n"); break; case CMD_LISTDRAGONS: silent_examine_position(EXAMINE_DRAGONS); show_dragons(); break; default: printf("\nInvalid command: %s", command); break; } if (passes >= 2) state = ascii_endgame(gameinfo, 0); } #if READLINE free(line_ptr); #endif } sgffile_output(&sgftree); passes = 0; /* Play a different game next time. */ update_random_seed(); /* Free the sgf tree and prepare for a new game. */ sgfFreeNode(sgftree.root); sgftree_clear(&sgftree); sgftreeCreateHeaderNode(&sgftree, board_size, komi, gameinfo->handicap); sgf_initialized = 0; gameinfo_clear(gameinfo); } }
int main() { srand((time(0))); int i,j; char again[2],winner,**board,agin='y'; board=(char**)malloc(sizeof(char*)*3); for (i=0; i<3; i++) board[i]=(char*)malloc(sizeof(char)*3); do { board=initboard(board); char first = 'y'; printf("Play first? "); scanf("%c",&first); displayboard(board); if (first == 'y'){ do { board=human_move(board,PLAYER1); displayboard(board); winner=checkwin(board,PLAYER1); if (winner!=PLAYER1 && winner!=' ') { if (computer_move(board,PLAYER2,PLAYER1)) winner=checkwin(board,PLAYER2); else printf("\nComputer passed\n"); displayboard(board); } } while (winner==BLANK); }else{ do{ if (computer_move(board,PLAYER2,PLAYER1)) winner=checkwin(board,PLAYER2); else printf("\nComputer passed\n"); displayboard(board); if (winner!=PLAYER2 && winner!=' ') { board=human_move(board,PLAYER1); winner=checkwin(board,PLAYER1); displayboard(board); } }while (winner == BLANK); } if (winner!=' ') printf("THE WINNER IS %c!\n",winner); else printf("IT'S A TIE.\n"); printf("\nPlay again (y)?: "); scanf("%s",again); getchar(); } while(again[0]=='y'); // getchar(); // scanf("%c",&agin); // } while(agin=='y'); //Free Allocated Space for (i=0; i<3; i++) free(board[i]); free(board); printf("\nThank you for playing.\n\n"); return 0; }
int main(int argc, char **argv) { setbuf(stdout, NULL); signal(SIGINT, INThandler); unsigned char ply = 6; int opt; while ((opt = getopt(argc, argv, "p:")) != -1) { switch (opt) { case 'p': ply = atoi(optarg); break; default: fprintf(stderr, "Usage: %s [-p ply]\n", argv[0]); exit(EXIT_FAILURE); } } struct position pos; init_position(&pos); assert(consistency(&pos)); struct timespec realt_old, realt_new; tt = new_trans_tables(); #ifdef _USE_HISTORY hist = init_history(); #endif #ifdef _DEBUG testing(); #endif #ifndef _XBOARD print_position(&pos); #endif while (1) { if (is_check(&pos)) { fprintf(stderr, "check.\n"); } user_input(&pos); #ifndef _XBOARD print_position(&pos); #endif assert(consistency(&pos)); if (is_check(&pos)) { fprintf(stderr, "check.\n"); } current_utc_time(&realt_old); computer_move(&pos, ply); assert(consistency(&pos)); current_utc_time(&realt_new); #ifndef _XBOARD fprintf(stderr, "One second: %12llu ns\n", (long long unsigned)BILLION); fprintf(stderr, "Real time: %12llu ns\n", (long long unsigned)minus_time(&realt_new, &realt_old)); print_position(&pos); #endif } return (0); }