This repository contains a Tic Tac Toe game implemented in Kotlin, featuring a command-line interface (CLI) and an extensible AI system. The game supports customizable AI algorithms, with a default implementation of the Minimax algorithm.
- Object-Oriented Design: The project uses Kotlin's
enum class,data class, and inheritance to create a modular and extensible design. - AI Support: The game includes a pluggable AI system, with Minimax as the default algorithm.
- Command-Line Interface: A simple and intuitive CLI for playing Tic Tac Toe.
- Extensibility: Easily add custom AI algorithms by extending the
Algorithmabstract class.
Mark: Represents the state of each cell in the game board (X,O, orBLANK).Position: Represents the coordinates of a cell on the board.Board: Manages the game state, including setting marks, checking for win conditions, and finding available moves.Algorithm: An abstract class for AI algorithms. Custom AI can be added by extending this class and implementing theplaymethod.Minimax: The default AI algorithm using the Minimax strategy for optimal moves.
set(position: Position, mark: Mark): Marks a cell withX,O, orBLANK.get(position: Position): Mark: Retrieves the mark of a cell.checkWin(): Mark?: Checks if there's a winner and returns the winning mark, ornullif there's no winner.getBlanks(): List<Position>: Returns a list of all available positions on the board.
To implement a custom AI, extend the Algorithm class:
abstract class Algorithm {
abstract fun play(board: Board, c: Mark): Position?
}Example: Adding a new AI algorithm:
object CustomAI : Algorithm() {
override fun play(board: Board, c: Mark): Position? {
// Your custom logic here
}
}- Clone the repository:
git clone https://github.com/HadiAgdam/TicTacToeKotlin.git
- Open the project in your favorite Kotlin IDE (e.g., IntelliJ IDEA).
- Build and run the project.
- Run the application.
- Choose whether you want to play as
XorO. - Make your moves by entering the coordinates of the cell (e.g.,
0 1for the cell at row 0, column 1). - The AI will make its moves automatically.
The Minimax algorithm evaluates all possible moves to determine the best one for the AI. It uses a recursive approach with the following logic:
- Maximizes the AI's score.
- Minimizes the opponent's score.
- Stops recursion when a win, loss, or draw is detected, or a maximum depth is reached.
- Add support for larger board sizes and custom win conditions.
- Implement additional AI strategies (e.g., Alpha-Beta Pruning, Random AI).
- Add a GUI for better user interaction.
Contributions are welcome! If you'd like to add a new feature or improve the AI, feel free to fork the repository and create a pull request.