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 a automatic game will be lots of fun. Let’s see how to do this.
random Python libraries are used to build this game. Instead of asking the user to put a mark on the board, 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 draw, then it returns -1.
play_game() is the main function, which performs following tasks :
- Calls create_board() to create a 9×9 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 a diagonal has the same player number. If so, displays the winner name. If after 9 moves, there are 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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Tic Tac Toe Game using PyQt5 in Python
- Tic Tac Toe GUI In Python using PyGame
- Draw a Tic Tac Toe Board using Python-Turtle
- Automatic Birthday mail sending with Python
- Implementation of Page Rank using Random Walk method in Python
- random.random() function in Python
- Random Walk (Implementation in Python)
- Conway's Game Of Life (Python Implementation)
- Random sampling in numpy | random() function
- Number Guessing Game in Python using Binary Search
- Generating Random id's using UUID in Python
- Python | Random Password Generator using Tkinter
- Dice Rolling Simulator using Python-random
- ML | Reinforcement Learning Algorithm : Python Implementation using Q-learning
- Python 3.6 dictionary implementation using hash tables
- ML | Naive Bayes Scratch Implementation using Python
- Implementation of Ridge Regression from Scratch using Python
- Implementation of Lasso Regression From Scratch using Python
- Linear Regression Implementation From Scratch using Python
- Implementation of K-Nearest Neighbors from Scratch using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.