- Introduction
- Implementation Choices
- Computational Costs
- Ideas for Future Implementation
- Sitography
- Running the Game
- Running Tests
The project is based on a generalization of the classic Connect 4 game. The difference is that matches take place in a game matrix of M×N, where M is the number of rows and N is the number of columns. Players aim to connect X consecutive pieces to win. Pieces can be connected vertically, horizontally, and diagonally. The project aims to create a bot capable of playing and winning ConnectX games against other bots, making decisions based on opponent moves while adhering to a given TIMEOUT constraint.
The goal of selectColumn()
is to choose a column from the available options in which to make a move. The best move is determined using a search strategy called Iterative Deepening, which utilizes a minimax algorithm with alpha/beta pruning.
This technique maximizes the time available for making a decision, increasing depth with each iteration until either the number of free cells is reached or a win is achieved.
Alpha/Beta Pruning significantly reduces the number of nodes explored by ignoring sub-trees that wouldn't influence the evaluation of the optimal move.
The MiddleSort()
function reorders an array of integers to prioritize moves in the center of the board, enhancing the efficiency of the search.
Heuristics are based on the concept of tokens—chains of connected pieces that could lead to victory. Tokens are counted for both the player and the opponent, using methods to evaluate vertical, horizontal, and diagonal connections.
- Count Vertical: O(MN)
- Count Horizontal: O(MNX)
- Count Diagonal: O(MNX)
- Heuristic Calculation: O(MNX)
The computational cost of alpha/beta pruning in the worst case remains O(N^p), with a total cost of O(MN^pX) when nodes are evaluated.
The selectColumn
function's total cost is O(MN^pXP) due to the nested calls to alpha/beta pruning.
Function | Cost |
---|---|
Count Vertical | O(MN) |
Count Horizontal | O(MNX) |
Count Diagonal | O(MNX) |
Heuristic | O(MNX) |
MiddleSort | O(N) |
Alphabet | O(MN^pX) |
SelectColumn | O(MN^pXP) |
To enhance performance and explore a greater depth, a hash table can be incorporated using the Zobrist hashing technique, allowing for quick verification of previously explored game states.
- The idea of implementing heuristics is inspired by various literature.
- Iterative deepening: link.
- Compile the project:
javac -cp ".." *.java */*.java
- Human vs Computer:
java -cp ".." connectx.CXGame 6 7 4 connectx.AnitaMaxMin.AnitaMaxMin
- Computer vs Computer:
java -cp ".." connectx.CXGame 6 7 4 connectx.AnitaMaxMin.AnitaMaxMin connectx.L1.L1
- Output score only:
java -cp ".." connectx.CXPlayerTester 6 7 4 connectx.AnitaMaxMin.AnitaMaxMin connectx.L1.L1
- Verbose output:
java -cp ".." connectx.CXPlayerTester 6 7 4 connectx.AnitaMaxMin.AnitaMaxMin connectx.L1.L1 -v
- Verbose output with customized timeout (1 sec) and number of game repetitions (10 rounds):
java -cp ".." connectx.CXPlayerTester 6 7 4 connectx.AnitaMaxMin.AnitaMaxMin connectx.L1.L1 -v -t 1 -r 10