Unbeatable Tic-Tac-Toe Bot
Play against an unbeatable Tic-Tac-Toe algorithm using minimax!
Overview
This project is a command-line implementation of the classic Tic-Tac-Toe game, where you can play against an unbeatable computer opponent. The computer uses the Minimax algorithm to make optimal moves, ensuring that it will always either win or the game will end in a draw (sorry to those competitive people out there!).
Gameplay
To play the game, you need to enter your move as two integer inputs representing
the row and column where you want to place your mark (X or O). For example, to
place your mark in the top-left corner, you would input 0 0
, and to place it
in the middle, you would input 1 1
.The game continues until one player wins or
it's a draw. Note: You can also choose to play on a 4x4 grid but for the first
couple moves it will take longer than usual for the computer to choose an optimal
position.
Implementation
The project is written in C and focuses on efficient memory allocation and deallocation to minimize resource usage. The minimax algorithm is used to determine the computer's moves, ensuring a high level of difficulty for human players. Additionally, this particular implementation of minimax uses alpha-beta pruning to optimize performance. After the algorithm recursively finds optimal moves for both players then it will stop searching and place the player on said square. This reduces the number of comparisons you have to make and speeds up the amount of time it takes to choose a position.
Memory Allocation and Deallocation
Memory is allocated responsibly to avoid memory leaks. When a game is completed, all dynamically allocated memory is freed to ensure clean and efficient resource management.
Getting Started
Follow these steps to get started with the Tic-Tac-Toe game:
- Clone the repository to your local machine:
$ git clone https://github.com/ccrawford4/tic-tac-toe.git
-
Navigate to the project directory:
cd tic-tac-toe
-
Compile the code using the provided
Makefile
. If you don't have GCC (opens in a new tab) or Make (opens in a new tab) installed then do that. To perform the compilation just type:make
into your command line and press enter. -
Run the game:
$ ./Play
-
Follow the on-screen instructions to play the game. Input your moves as row and column integers.
-
Cleanup (Optional)
If you want to remove the compiled output files and to clean up your directory, you can use the collowing command:
make clean
This will remove any generated object files and executables.