CLI Tic Tac Toe
CLI based Tic Tac Toe with 2 players.
Since, there are only two users, and no centralised server, the game logic will reside in each file. So I created a
TicTacToe class. The class has the following methods:
||Ask Player 1's move and will keep asking untill it gets a valid input|
||Ask Player 2's move and will keep asking untill it gets a valid input|
||Will find the winner at the current state of the game and return the value|
||Display's the board neatly|
||will decide if the game is over|
Now, let's discuss how I have decided the logic of the game. I am storing the board in a linear array of 9 elements, initially they have 0 else if p1 made a move it will be +1 else for p2 it will be -1. Now to check we will check if the rows, cols, diagonals and anti-diagonals have +3 or -3 if so then there is a winner else no winner.
One optimisation instead of going through the entire board every single time, I am looking at the corresponding rows, and cols, and diagonals of the last move, because only that will decide the game.
For the client and server script its simple socket programming.
Thanks to @lugnitdgp for their awesome guidance <3
Top comments (0)