This is a simple two-player Tic Tac Toe game built using Kotlin and XML layouts for Android.
- 3x3 grid with touch interaction
- Win/draw detection
- Score tracking
- Play again and reset options
- Game and UI state persistence using onSaveInstanceState()
- Android Studio
- Kotlin
- Target SDK: API 28+
- Clone the repo or open the project in Android Studio.
- Run the app on an emulator or physical device.
- MainActivity.kt: Manages the UI logic and responds to user interactions.
- TicTacToeGame.kt: Contains the game logic including board state, win/draw checking, and score keeping.
The game logic is encapsulated in TicTacToeGame
, a class that maintains the board, current turn, player scores, and win/draw state. It includes methods for:
- Making moves
- Switching turns
- Checking win and draw conditions
- Resetting game or board
- Exposing necessary getters for state recovery
- The UI is defined using XML layouts and manipulated in
MainActivity
. - A list of buttons is used to represent the 3x3 game board.
- Each move updates both the board UI and the internal state.
- After each move, the app checks if a player has won or if the game ends in a draw.
- The game state (board, turn, scores) is saved using
onSaveInstanceState()
and restored inonCreate()
to survive screen rotation. - Turn display and text colors are also restored accordingly.
boardButtonClicked()
responds to player moves.playAgainButtonClicked()
resets only the board.resetGameButtonClicked()
resets both board and scores.