Post

Tic Tac Toe AI

Tic-Tac-Toe with Minimax Algorithm

Overview

The project is a console-based implementation of the classic game Tic-Tac-Toe, with an option to play against an AI opponent that employs the Minimax algorithm. The game allows players to choose between a Player vs. Player (PvP) mode and a Player vs. AI mode. In PvP mode, two human players take turns to make their moves, and in Player vs. AI mode, the player competes against an AI opponent that uses the Minimax algorithm to make optimal decisions.

Contributors Forks Stargazers Issues LinkedIn


Open GitHub Repo »

Report Bug

Built With

  • c

Features

  • Game Modes:
    • PvP: Two human players take turns making moves.
    • Player vs. AI: The player competes against an AI opponent using the Minimax algorithm.
  • Dynamic Board Display:
    • The game board is dynamically displayed after each move, providing a clear visual representation of the current state.
  • Input Validation:
    • Player inputs are validated to ensure they are within the valid range of 1-9, and the chosen location is not already occupied.
  • Win Detection:
    • The game checks for a winning condition after each move and announces the winner or a tie.
  • AI Opponent:
    • The AI opponent uses the Minimax algorithm to make intelligent moves, aiming to maximize its chances of winning or force a tie.
  • Play Again Option:
    • After each game, the player is prompted to play again, enhancing the user experience.

Minimax Algorithm

The Minimax algorithm is a decision-making algorithm used in two-player games to find the optimal move for a player, assuming that the opponent is also playing optimally. The algorithm evaluates possible future moves, assigns a score to each, and selects the move with the highest score for the maximizing player and the lowest score for the minimizing player.

Code Snippet

A small snippet of the logic regarding writing to the DirectoryService which allows for persistant data even if app was closed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
int minimax(char board[3][3],int depth, bool isMaxingorMining){
    int result =   checkWin(board); //upon run of the recursive function minimax, 
    //if game reached a terminal state, result is returned.
    if (result !=-2){
        return result;
    }
    if (isMaxingorMining){ // minimax function in this project is trying to
    //maximize the score for the ai and minimize it for the human player. 
        int bestScore = INT_MIN;
 for (int row = 0;row<3;row++){
for (int col = 0;col<3;col++){
if (board[row][col]<59){

        char initial = board[row][col];
        board[row][col] = 'O';
        int score = minimax(board,depth+1,false);
        board[row][col] = initial;
        if (score > bestScore){
            bestScore=score;
        }

        }
        }} 
        return bestScore;
    }
    else //choosing the minimal score for the human. 
    {
 int bestScore = INT_MAX;
 for (int row = 0;row<3;row++){
        for (int col = 0;col<3;col++){
        if (board[row][col]<59){
        char initial = board[row][col];

        board[row][col] = 'X'; 
        int score = minimax(board,depth+1,true); 

        if (score < bestScore){ 
            bestScore=score;
        }

        }}} 
        return bestScore;
    }
}
This post is licensed under CC BY 4.0 by the author.