This program implements a classic Tic Tac Toe game where you play against an AI opponent that uses the minimax algorithm to make optimal moves. The game runs in the console and features a smart computer player that's difficult to beat!
- Save the code in a file named
tictactoe.c - Compile with GCC:
./build - Run the executable:
./tictactoe
- Player vs Computer: You play as 'X' and the computer plays as 'O'
- Smart AI: Computer uses the minimax algorithm to make optimal moves
- Win Detection: Automatically detects wins for either player
- Draw Detection: Recognizes when the game ends in a tie
- Input Validation: Checks for valid moves and occupied positions
- The game board positions are numbered 1-9:
1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 - On your turn, enter a number (1-9) to place your 'X' in that position
- The computer will automatically respond with its 'O' move
- The game ends when either:
- A player gets three in a row (horizontally, vertically, or diagonally)
- The board is full (draw)
- Implements the minimax algorithm to evaluate board positions
- Returns a score representing the game state:
10: Computer ('O') wins-10: Player ('X') wins0: Draw or neutral position
- Recursively evaluates all possible moves to find the optimal play
- Uses the minimax scores to determine the computer's best move
- Iterates through all empty positions
- Selects the move with the highest minimax score for the computer
- Checks all possible win conditions:
- Horizontal rows
- Vertical columns
- Main diagonal
- Anti-diagonal
- Returns 1 if the specified player has won, 0 otherwise
- Checks if the board is completely filled
- Returns 1 if full (draw), 0 otherwise
- Displays the current game board in a user-friendly format
- Shows player moves and board boundaries