Skip to content

MainActivityKt/Simple-Tic-Tac-Toe-Kotlin

Repository files navigation

Simple Tic-Tac-Toe

Everybody remembers this paper-and-pencil game from childhood: Tic-Tac-Toe, also known as Noughts and crosses or Xs and Os.
A single mistake usually costs you the game, but thankfully it is simple enough that most players discover the best strategy quickly.
In this project, a console version of this game is made using the language Kotlin.

Stage 1/5: Welcome to the battlefield!

In this stage, the game grid is printed to the console output. Each line contains three characters (X’s and O’s) to represent a game of tic-tac-toe where all fields of the grid have been filled in.
Stage implementation: SimpleLayout.kt

Example:

X O X
O X O
X X O 

Stage 2/5: The user is the game master

It is now time to modify the program, so it would allow the user to enter a string representing the game state, and print the 3x3 game grid based on this input.
Stage implementation: GameMaster.kt

Examples:
Note: The greater-than symbol followed by a space (> ) represents the user input.

> O_OXXO_XX
---------
| O _ O |
| X X O |
| _ X X |
---------

Example 2:

> _XO__X___
---------
| _ X O |
| _ _ X |
| _ _ _ |
---------

Stage 3/5: What's up on the field?

In this stage, our program will analyze the game state to determine if either player has already won the game or it is still ongoing, if the game is a draw, or if the user has entered an impossible game state (two winners, or with one player having made too many moves).

Possible states:

  • Game not finished ➡ when neither side has three in a row but the grid still has empty cells.
  • Draw ➡ when no side has a three in a row and the grid has no empty cells.
  • X wins ➡ when the grid has three X’s in a row (including diagonals).
  • O wins ➡ when the grid has three O’s in a row (including diagonals).
  • Impossible ➡ when the grid has three X’s in a row as well as three O’s in a row, or there are a lot more X's than O's or vice versa

Stage implementation: GameAnalyzer.kt

Example 1:

> XXXOO__O_
---------
| X X X |
| O O _ |
| _ O _ |
---------
X wins

Example 2:

> XOOOXOXXO
---------
| X O O |
| O X O |
| X X O |
---------
O wins

Example 3:

> XO_OOX_X_
---------
| X O   |
| O O X |
|   X   |
---------
Game not finished

Stage 4/5: First move

Our game is getting interactive!
Now, the user can make a move, and the program is asking them to enter the coordinates of the cell where they want to make a move to. So, the grid is divided into cells.
In this stage, the user plays as X, not O.

Summary:

  • The program gets the initial 3x3 grid from the input as in the previous stages. Here the user should input 9 symbols representing the field, for example, _XXOO_OX_ and the grid is printed out immediately.

  • The user is prompted to input 2 coordinate numbers that represent the cell where they want to place their X, for example, 1 1.

  • If the input is incorrect, they're informed about the problem.'s move (based on the first line of input) and once after the user has entered valid coordinates (then you need to update the grid to include that move).

Stage implementation: MoveMaker.kt

Example 1:

> X_X_O____
---------
| X   X |
|   O   |
|       |
---------
> 3 1
---------
| X   X |
|   O   |
| X     |
---------

Example 2:

> _XXOO_OX_
---------
|   X X |
| O O   |
| O X   |
---------
> 3 1
This cell is occupied! Choose another one!
> 1 1
---------
| X X X |
| O O   |
| O X   |
---------

Stage 5/5: Fight!

Description Our game is now ready! In this stage, the previous approaches are combined in order to make a game of tic-tac-toe that two players can play from the beginning (with an empty grid) through to the end (until there is a draw, or one of the players wins).
The first player has to play as X and their opponent plays as O.

Stage implementation: TicTacToe.kt

Example:

---------
|       |
|       |
|       |
---------
> 2 2
---------
|       |
|   X   |
|       |
---------
> 2 2
This cell is occupied! Choose another one!
> two two
You should enter numbers!
> 1 4
Coordinates should be from 1 to 3!
> 1 1
---------
| O     |
|   X   |
|       |
---------
> 3 3
---------
| O     |
|   X   |
|     X |
---------
> 2 1
---------
| O     |
| O X   |
|     X |
---------
> 3 1
---------
| O     |
| O X   |
| X   X |
---------
> 2 3
---------
| O     |
| O X O |
| X   X |
---------
> 3 2
---------
| O     |
| O X O |
| X X X |
---------
X wins

Releases

No releases published

Packages

No packages published

Languages