void start_game(t_grid *grid) { int random; int i; int winner; ft_putendl("starting the game"); srand(time(NULL)); random = rand() % 2; i = 0; while ((!(winner = check_winner(grid, 'X'))) && (!(winner = check_winner(grid, 'O'))) && (!(winner = tie(grid)))) { random ^= 1; if (random) { while (player_turn(grid) == 0) i++; } else ai_turn(grid); } display_grid(grid); call_winner(winner); }
void turn_decision (gameType * game) { /* Make a decision on what to do in a turn, based on game state */ char player; /* Refresh background information */ player = game->whoseTurn; game_update_valid_moves(game); game_update_scoring(game); /* Board is full: end the game */ if (board_missing_char('.', &game->board)) { sysMessage(2, game); } /* Player has no move options: pass */ else if (board_missing_char(player, &game->validMove)) { printf("%c passes.\n", game->whoseTurn); game_next_player(game); (game->passes)++; /* Both players passed: end the game */ if (game->passes > 1) { sysMessage(3, game); } return; } /* AI player: place a tile */ else if ((player == 'O') && ((game->pTypeO) != 0)) { ai_turn(game->pTypeO, game); } else if ((player == 'X') && ((game->pTypeX) != 0)) { ai_turn(game->pTypeX, game); } /* Human player: input prompt */ else { input_turn(game); } }
bool play(bool with_ai) { char grid[SIZE][SIZE]; Index2d pos; int moves = 0; int max_moves = SIZE * SIZE; static int player1_score = 0, player2_score = 0; srand((unsigned)time(NULL)); rand(); /* Initialize grid */ memset(grid, ' ', SIZE * SIZE); char player1 = rand() % 2 ? 'x' : 'o'; char player2 = (player1 == 'x' ? 'o' : 'x'); if (with_ai) { printf("You are %c\n", player1); if (player1 == 'x') print_grid(grid); } else { printf("Player 1 is %c. Player 2 is %c\n", player1, player2); print_grid(grid); } if (player1 == 'o') { if (with_ai) ai_turn(grid, player1, player2); else { puts("Player 2's turn"); player_turn(grid, player2); } ++moves; print_grid(grid); } for (;;) { /* Player 1 */ if (moves < max_moves) { if (!with_ai) puts("Player 1's turn"); pos = player_turn(grid, player1); ++moves; if (won(grid, pos, player1)) { if (with_ai) puts("You won"); else puts("Player 1 won"); ++player1_score; break; } /* Don't print the grid before the AI's turn */ if (!with_ai) print_grid(grid); } /* Player 2 / AI */ if (moves < max_moves) { if (with_ai) pos = ai_turn(grid, player1, player2); else { puts("Player 2's turn"); pos = player_turn(grid, player2); } ++moves; if (won(grid, pos, player2)) { if (with_ai) puts("You lost"); else puts("Player 2 won"); ++player2_score; break; } print_grid(grid); } if (moves == max_moves) { puts("Tie"); break; } } print_grid(grid); printf("Player 1 Player 2\n" "-------- --------\n" "%8d%10d\n", player1_score, player2_score); return prompt_bool("Play again?", true); }