This code implements a simple turn-based game similar to a...
This code implements a simple turn-based game similar to a Connect Four-like game or grid-based "four in a row" game for two players: a human and a computer AI. Here’s an overview of what the code does:
1. Board Setup
-
It creates an 8x8 grid board initialized with
'-'
, representing empty cells. -
The board is displayed in a readable format (with row labels as letters
A-H
and column labels as numbers1-8
). -
Example:
1 2 3 4 5 6 7 8 A - - - - - - - - B - - - - - - - - C - - - - - - - - D - - - - - - - - E - - - - - - - - F - - - - - - - - G - - - - - - - - H - - - - - - - -
2. Game Rules
-
Players take turns (human or computer's AI).
-
A player (HUMAN:
'O'
, or COMPUTER:'X'
) chooses an empty cell to place their mark. -
The first player to align 4 of their marks in a row, either:
- Horizontally
- Vertically
- Diagonally wins the game.
-
If the board is filled and neither player has won, the game results in a tie.
3. Core Gameplay Logic
- The game alternates between the human and the computer:
- For the human's turn: The player inputs a move (e.g.,
"E5"
for rowE
and column5
). The input is validated. - For the computer's turn: The AI calculates its move using the Alpha-Beta Pruning algorithm paired with a heuristic evaluation function.
- For the human's turn: The player inputs a move (e.g.,
4. Computer AI: Alpha-Beta Pruning
The computer uses Alpha-Beta Pruning, a depth-limited search algorithm, to make its decision:
- Maximizing Player: The computer AI (
'X'
) aims to maximize its advantage. - Minimizing Player: The human (
'O'
) aims to minimize the computer's advantage.
Heuristic Evaluation:
- The computer assigns scores to different board states based on:
- Threats (lines of 3 or 2 marks with empty spaces).
- Immediate winning or blocking opportunities.
- Higher scores mean stronger positions for the computer, while lower (negative) scores mean better positions for the human.
- The evaluation scores are calculated for rows, columns, and diagonals of length 4 on the board.
Time Constraint:
- The AI calculates the optimal move recursively but halts its search if the computation time exceeds
TIME_LIMIT
(5 seconds).
5. Functions Breakdown
Here is a quick summary of important functions:
create_board()
: Initializes the empty 8x8 board.print_board(board)
: Displays the board in a user-friendly format.is_winner(board, player)
: Checks if a given player ('O'
or'X'
) has 4 marks aligned in a row (horizontally, vertically, or diagonally).is_full(board)
: Checks if the board is completely filled, indicating a tie.get_legal_moves(board)
: Returns a list of all unoccupied cells (available moves).apply_move(board, move, player)
: Places a player's mark on the board at the specified position.undo_move(board, move)
: Reverts a move made on the board (used during AI evaluation to simulate future moves).
6. Execution Flow: main()
- Starts the game with an empty board.
- Asks the user if they want to play first.
- Takes turns between the human and the computer, applying moves to the board until:
- A player wins (4 aligned marks).
- The board is full (tie).
- The program declares the winner (human/computer) or announces a tie.
7. User Interaction
- The human player inputs moves in algebraic notation (e.g.,
"B3"
,"E5"
) which are parsed into(row, col)
coordinates viaparse_move()
. - Invalid moves prompt the user to retry.
Summary
This program is a turn-based "four in a row" game on a fixed 8x8 grid, featuring a competitive AI opponent. The AI makes strategic decisions using Alpha-Beta Pruning with heuristic evaluation to maximize its chances of winning, while ensuring a responsive play experience by adhering to a time limit. The player interacts via text prompts, making it a simple console-based game.