This project is building a 3 by 3 board Tic-Tac-Toe game using Ruby and OOP concept. The project is divided into four milestones. They are Project setup, user interface, game logic and game instructions, each with dedicated git branches to be merged to the default development branch.
The project has two folders namely bin and lib, in addition to the root directory. The bin folder is the one which holds the executable file, 'main'. The lib folder is the one which holds all the library files that have all the game classes and methods.
- main.rb file is reponsible for user interaction, and all the puts and gets are written here but nowhere else. It is setup to be executable and it does not have the .rb extension. Call commands to the lib files are written at the top of these file.
Tic-Tac-Toe is a game played by two players. It has a board with different sizes in general, but this particular game has 3 by 3 spots board as shown below.
7 | 8 | 9
The players will choose their own symbols and make moves alternatively and to win the game the player should make horizonal or vertical or diagonal pattern with his symbol first. The winning conditions are: X | X | X
7 | 8 | 9
7 | 8 | 9
X | X | X
X | 8 | 9
7 | X | 9
7 | 8 | X
7 | 8 | X
X | 8 | 9
The game can be played on terminal. To start the game just type 'ruby bin/main' on terminal and a welcome message pops up followed by instruction to begin the game.
-
Once the game starts the game object is created and the game asks players to enter the players names.
$ ruby bin/main Hi, Welcome to TicTacToe game Insert name of two players:
-
The players should feed valid player names, other wise error message will pop out until valid names are given. Names should have more than one character and cannot start with number
Henok Nikola
-
Once the name of the two players are given the game creates player objects and displays welcome message for the players
7 | 8 | 9
-
Then the gams asks each player to choose their symbols. If the symbols are not valid the game displays error message. Numbers and more than one character is not allowed in the game.
Ok Henok please pick your symbol: X Ok Nikola please pick your symbol: Y
-
Game assigns the input symbols to the respective player objects and displays message which asks players to choose the beginner.
Henok choses X Nikola choses Y Game is ready. Now pick the player for the first move:
-
One of the players input their name to begin. If players enter a name different from the creater player objects names, the game will display error message. At this point only one of the players names can be accepted.
Nikola
-
The game accepts the input and sets the beginner, and asks the chosen beginner to pick a cell from displayed board. Nikola will start'
7 | 8 | 9
-
The player picks his/her cell by using number index. The number index of each cell is shown in the About the game section. The player can only pick index number from 1 to 9 only, other wise the game will display error message until valid input is given.
1
-
The game accepts the player input and updates the board and displays it to the player
7 | 8 | 9
-
The game asks the next player to make a move while displaying the current board
7 | 8 | 9
-
Now the next player picks available cell using the index number left. Picking already occupied cell will invoke error message until the player picks available spot. The game displays the current board to teh player so that the current player can see what available spots are there. 5
-
Once the player make move the game will check if teh move is valid and it updates the board and displays it.
7 | 8 | 9
-
The game continues with the same loop with maximum of 9 as described above until three conditions are met: a) Game ends with win result (refer win conditions in the about the game section) b) Game ends with draw result (will be discussed in section below)
The game loops through the same game steps until game over condition is met. The maximum possible loop is 9 when there is no win or draw conditions are met earier.
At the end of every loop step, the game checks if either win or draw conditions are met.
Winning move: As described in the about section, the winning move is achieved when horizontal, vertical or diagonal full patterns are made by a players symbol as demonstrated below. In this case the game declares the winner and displayes a message the game is over.
6
You made the winning move. You won!!!
Y | 2 | Y
----------
X | X | X
----------
7 | Y | 9
Game is over. Would you like to continue? (Y) or quit? (N):
Draw move: The game also checks if there is no more winning conditions can be achieved in the subsequent steps. The game starts to check if move will cause draw result when two spots are left in the game.
Game ends with draw result
Y | X | 3
----------
X | Y | Y
----------
7 | Y | X
Game is over. Would you like to continue? (Y) or quit? (N):
As demonstrated in the example above, even though the game has two more spots to play with, there will be no winning condition, no matter what moves the players make subsequently. Therefore, the game declares the game end s with draw result. Finally, after game is over, the game will give option if plpayers can repeat the game or quit
Game is over. Would you like to continue? (Y) or quit? (N):
- Ruby
- VS code
- Ruby
Run the code demonstrations below
main
π€ Nikola Zdarle
- GitHub: @zdrale
- Twitter: [@zdralenikola]https://twitter.com/zdralenikola)
- LinkedIn: Nikola Zdrale
π€ Henok Mossissa
- GitHub: @henatan99
- Twitter: @henatan99
- LinkedIn: Henok Mossissa
Contributions, issues, and feature requests are welcome!
Give a βοΈ if you like this project!
This project is MIT licensed.