Developing A Tic-Tac-Toe Python Game

Tic-Tac-Toe Python
credit: pub nub

In recent times, making a Tic-Tac-Toe Python game has been trending among amateur developers. We know that gaming has become one of the most preferred entertainment choices in recent times. Moreover, if you have surfed through various streaming platforms then you must know that gaming is no longer a mere entertainment choice. Rather, it has evolved into a prominent profession for many. On YouTube, Twitch and other platforms you will get to see that millions of followers follow numerous gamers. Thus, for this very reason, gaming has become a very good way to earn your livelihood.

There are numerous games in the market that have earned huge popularity. However, those are high-end games and they are great to play as a player. However, if you looking to develop games, it is better to start with a simple game like Tic-Tac-Toe. Moreover, Python is a very popular and widely used programming platform around the globe. Therefore, it is better to start developing or creating a Tic-Tac-Toe game through Python. Therefore, if you wish to create this simple game using Python, then this article is the perfect stop for you. Keep reading this article to know all the details involved in the creative process.

About Tic-Tac-Toe Python

Tic-Tac-Toe is a very simple game that most of us have played at one point or the other in our childhood days. In order to make things clear, let me inform you that it is a game involving two players. Each player will have to select a sign between ‘0’ and ‘x’. In addition, there will be a board with 9 rooms. First, a player will place his/her sign on a random room of the board. Then the other player will put his/her sign. The aim of the players will be to place their respective signs in a consequent manner, either in a row, a column or in a diagonal symmetry. 

If you are interested to know, then let me inform you that theoretically there are eight ways to win the game by arranging the signs in a consequent sequence. However, apart from winning and losing there is also a fair chance of ending the game in a draw. In our childhoods, we mostly played the game on a piece of paper. However, now is the time to see the way to design the game using Python. 

The Code Algorithm
credit: medium

The Code Algorithm

Now, it is time to know about the algorithm to write the code. In fact, if you follow this algorithm, you will be able to form the game in any programming language. Here goes the algorithm:

  • Initialising each element is empty, the first step is to create board with the 2-dimensional array. There are various ways to represent emptiness with symbols. You can use the hyphen ‘-’ for this purpose.
  • Now, it is time to check if the board is filled or empty. For this purpose, write a function. If the result comes as ‘False’, then the board has the sign of emptiness. On the other hand, if it returns as ‘True’, then it is not empty.
  • After this, you have to check whether a player has won or not. For this purpose, again you have to write a program. In case of this checking, one needs to check all the eight possibilities of winning. 
  • Now, it is time to write a function that will show the board. 
  • Finally, it is time to write a function to begin the game.
  • Now let a user see the board for selecting the spot of the next move. 
  • Let the user enter the column and row number.
  • Update the respective player sign with the spot selected.
  • Now check the result, whether it is a win or a loss for the user. 
  • Then write a message to break the loop. 
  • If the board is filled then write a message of the draw to break the loop.

Even if you do not understand the process right now, there is nothing to worry about. This article will provide a code for you which is made through python.

The Tic-Tac-Toe Python Coding

The Tic-Tac-Toe Python Coding
credit: wikipedia

The code is as follows for the coding of the game in python:

import random

class TicTacToe:

def __init__(self):

self.board = []

def create_board(self):

for i in range(3):

row = []

for j in range(3):

row.append(‘-‘)

self. board.append(row)

def get_random_first_player(self):

return random.randint(0, 1)

def fix_spot(self, row, col, player):

self.board

= player

def is_player_win(self, player):

win = None

n = len(self.board)

# checking rows

for i in range(n):

win = True

for j in range(n):

if self.board[i][j] != player:

win = False

break

if win:

return win

# checking columns

for i in range(n):

win = True

 for j in range(n):

 if self.board[j][i] != player:

 win = False

Break

f win:

return win

# checking diagonals

win = True

for i in range(n):

if self.board[i][i] != player:

win = False

break

if win:

return win

win = True

for i in range(n):

if self.board[i][n – 1 – i] != player:

win = False

break

if win:

return win

return False

for row in self.board:

for item in row:

if item == ‘-‘:

return False

return True

def swap_player_turn(self, player):

return ‘X’ if player == ‘O’ else ‘O’

def show_board(self):

for row in self.board:

for item in row:

print(item, end=” “)

print()

def start(self):

self.create_board()

player = ‘X’ if self.get_random_first_player() == 1 else ‘O’

while True:

print(f”Player {player} turn”)

self.show_board()

# taking user input

row, col = list(

map(int, input(“Enter row and column numbers to fix spot: “).split()))

print()

 # fixing the spot

self.fix_spot(row – 1, col – 1, player)

# checking whether current player is won or not

if self.is_player_win(player):

print(f”Player {player} wins the game!”)

break

# checking whether the game is draw or not 

if self.is_board_filled():

print(“Match Draw!”)

break

# swapping the turn

player = self.swap_player_turn(player)

# showing the final view of board

print()

self.show_board()

# starting the game

tic_tac_toe = TicTacToe()

tic_tac_toe.start()

Concluding Lines

So, now you know to develop the Tik-Tac-Toe using Python. Therefore, go and check out the code fast.

Previous Article
iOS 16 Update Stuck at Update Requested

iOS Update Stuck at Update Requested: How to Fix

Next Article
Valorant Ranks

Valorant Ranks: All You Need To Know About It

Related Posts