Python implementation of automatic Tic Tac Toe game using random number
Tic-tac-toe is a very popular game, so let’s implement an automatic Tic-tac-toe game using Python. The game is automatically played by the program and hence, no user input is needed. Still, developing an automatic game will be lots of fun. Let’s see how to do this. NumPy and random Python libraries are used to build this game. Instead of asking the user to put a mark on the board, the code randomly chooses a place on the board and put the mark. It will display the board after each turn unless a player wins. If the game gets drawn, then it returns -1.
Explanation: play_game() is the main function, which performs the following tasks :
- Calls create_board() to create a 3×3 board and initializes with 0.
- For each player (1 or 2), calls the random_place() function to randomly choose a location on board and mark that location with the player number, alternatively.
- Print the board after each move.
- Evaluate the board after each move to check whether a row or column or diagonal has the same player number. If so, displays the winner’s name. If after 9 moves, there is no winner then displays -1.
Below is the code for the above game:
[[0 0 0] [0 0 0] [0 0 0]] Board after 1 move [[0 0 0] [0 0 0] [1 0 0]] Board after 2 move [[0 0 0] [0 2 0] [1 0 0]] Board after 3 move [[0 1 0] [0 2 0] [1 0 0]] Board after 4 move [[0 1 0] [2 2 0] [1 0 0]] Board after 5 move [[1 1 0] [2 2 0] [1 0 0]] Board after 6 move [[1 1 0] [2 2 0] [1 2 0]] Board after 7 move [[1 1 0] [2 2 0] [1 2 1]] Board after 8 move [[1 1 0] [2 2 2] [1 2 1]] Winner is: 2
- The code starts by importing all the necessary libraries.
- Next, it creates an empty board and checks for empty places on the board.
- The possibilities() function then selects a random place for the player and returns the board.
- The row_win(), col_win(), and diag_win() functions check whether the player has three of their marks in a horizontal row, vertical row, or diagonal row, respectively.
- If so, they return True and win is set to that player.
- If not, they continue checking until either one of these conditions is met.
- Finally, evaluate() determines whether there is a winner or tie based on the results of the other two functions.
- If there is no winner (i.e., all players have zero marks), then no action is taken and the program terminates with an error message stating that there was no game played!
- Otherwise, if both players have at least one mark in each column and row but not in any diagonal line (a situation called a deadlock), then play continues as normal with whoever has more wins being declared the winner.
- In case of a tie, play goes back to evaluating who won last time; this process repeats until somebody wins or somebody loses all their pieces (which ends up being Game Over
- The code creates an empty board and then checks for the player having three marks in a horizontal row, vertical row or diagonal row.
- If the player has achieved this, the code sets the winner variable to be equal to the corresponding value from that row on the board.
- If there is no winner, then all of the players’ pieces are set to 0 and the program ends.