Gaming is one form of entertainment available to people. Games of all kinds are available online, on mobile devices, on desktop computers, etc. Now is not the time to create one of those challenging games.
Using Python, we’ll make a CLI tic-tac-toe game. Let’s use Python to build a straightforward Tic Tac Toe game. It will assist you in creating game logic and learning how to organize code.
Tic-Tac-Toe
Before we begin coding, I thought I’d quickly summarize the game and discuss my plan for developing it.
First, we’ll establish the assumption that Xs always take the initiative and move first, giving the user the opportunity to act.
From the console, a number between 1 and 9 will be inputted. In a grid where each number corresponds to a different number (top left is 1, bottom right is 9).
The computer will automatically determine and make its move once the user moves. One primary game loop that calls a number of various routines will be used by me.
Which function is being finished is probably indicated in each header of this text-based lesson.
Designing a Board
We’ll depict our board using a grid layout, as I indicated previously. We’ll make a list named board in Python to do this, and it will begin with 10 empty values.
Because the user can only enter numbers 1 through 9, not 0 through 8, we have 10 empty values rather than 9. So, in order to simplify things, we’ll give our list’s first entry the value “empty text.”
In this manner, we may use 1–9 instead of 0–8 to index the entries in our list.
insertLetter()
Letter and Pos will be the two inputs for this function. It will only put the requested letter at the requested location.
spaceIsFree(pos)
Simply said, this method will inform us if the specified area is free. Hence, there isn’t a letter there already. Pos, its sole argument, will be an integer between 1 and 9.
printBoard(board)
The board is sent as an argument to this method, which displays it on the console.
isWinner()
Based on the present board, this function will let us know if the supplied letter has won. It has two parameters: le and board (letter).
It has to be an “X” or an “O.” Simply look to see if the specified letter is present on each potential winning line on the board.
main()
When we want to start the game, we shall call this method. It will call every single one of our program’s many functions and direct how the program is supposed to run.
isBoardFull()
When given aboard as an input, this method will simply return True if the board is full and False otherwise.
playerMove()
In this method, we will request a move from the user and validate it. The letter will be added to the board if the move is legitimate. If not, we shall keep requesting the user’s feedback.
CompMove() – AI
The AI will now begin. The movement of the computers will be controlled by this function. It will evaluate the board and choose the best move to make.
Following is a list of the algorithm we’ll use to do this.
- If the current step cannot be finished, move on to the following one.
- Take the winning move if you can.
- Move into that position on the following turn if the player has a potentially winning move.
- Pick a corner and turn it. Choose at random if there are several options.
- Place yourself in the middle.
- Consider one of the edges. Choose at random if there are several options.
- If there is no way to move, the game ends in a tie.
selectRandom()
Given a list of potential locations, this function will choose at random the action to take.
The Game’s Start
The game can now be started now that we have finished all of our tasks. All we would need to do is call main if we just wanted to run the game once.
But in our situation, we want the game to continue running until the user decides they no longer want to play, so we’ll add a tiny while loop to the main line.
Complete Code
Here is the whole code for our game, which is now complete.
Output
Conclusion
Hurray!
A game that you made entirely from scratch. It is not a game that we play on a regular basis. But it aids in the writing of logic and the maintenance of a clear structure in code.
Happy coding!
Leave a Reply