TicTacToe implementation in kotlin
- Presentation layer
- Domain Layer
- Ai interface
- Working ai
Clone this git repository and set it up with gradle in intellij or any other preffered IDE 🙂
In the package net.monarezio.domain.ai
create a new class:
class RandomAi {
}
implement the interface Ai
class RandomAi : Ai {
override fun nextCoordinates(game: AiTicTacToe): Coordinate
= TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
Write your code
class RandomAi : Ai {
override fun nextCoordinates(game: AiTicTacToe): Coordinate {
val board = game.getBoard().getFields()
while (true) {
val x = Int.random(0, board.size - 1)
val y = Int.random(0, board[x].size - 1)
if (board[x][y] == Field.ANON)
return Coordinate(x, y)
}
}
}
Build and run the project, in main menu choose one of the options with the Ai 🙂