Let us write a code for tic tac toe in python and implement tic tac toe game in python. tic-tac-toe is one of the popular games during our school and college times and everyone will be familiar with it. Today we are going to create a basic design for the tic tac toe game. we can design various versions of tic tac toe but here we are going to write code for the tic-tac-toe game in python.
If you are not familiar with the tic tac toe game you can play it visually here to understand its rules more clearly.
Before going to write code for the Tic Tac Toe game let us understand the basics of the tic tac toe program.
Tic Tac Toe is a two-player paper-and-pencil game in which each player takes turns placing their symbol (either X or O) on a 3x3 grid. The goal is to get three of your symbols in a row, either horizontally, vertically, or diagonally, before your opponent does.
If all of the spaces on the grid are filled and no player has won, then the game is a draw. When it is your turn, you must place your symbol on an empty space on the grid and you cannot place it in a space that is already occupied. In addition to trying to get three in a row, players should also try to block their opponent from achieving this goal and create a row of their own.
Algorithm for tic tac toe program
Here is the basic algorithm for the tic tac toe program and a step-by-step explanation of the underlying tic tac program code.
- We are creating tic tac toe board using a 2-Dimensional array and initializing it with an empty string.
- we are checking whether the board is filled or not using a function
- has_won function is checking whether a particular player won or not.
- we will be checking the diagonal, each row, and each column.
- Function to check whether a move is valid or not.
Here is a simple implementation of the tic-tac-toe game in python.
board = [[" " for _ in range(3)] for _ in range(3)]
def draw_board():
print(" 0 1 2")
for i, row in enumerate(board):
print(i, *row)
def get_move(player):
while True:
row = input(f"{player}, enter row: ")
col = input(f"{player}, enter col: ")
if row.isdigit() and col.isdigit():
row, col = int(row), int(col)
if 0 <= row < 3 and 0 <= col < 3 and board[row][col] == " ":
board[row][col] = player
return
print("Invalid move. Try again.")
def has_won(player):
# check rows
for row in board:
if row == [player, player, player]:
return True
# check columns
for col in range(3):
if board[0][col] == player and board[1][col] == player and board[2][col] == player:
return True
# check diagonals
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
return True
if board[0][2] == player and board[1][1] == player and board[2][0] == player:
return True
return False
def main():
draw_board()
while True:
get_move("X")
draw_board()
if has_won("X"):
print("X has won!")
break
get_move("O")
draw_board()
if has_won("O"):
print("O has won!")
break
main()
Here is the output for the following code of tic tac toe.
This program will enable two people to play Tic Tac Toe on the command line. Players will input the row and column where they want to place their symbol ("X" or "O") and the board will be refreshed and displayed again after each turn. The game will finish when one player has won or all the spaces on the board have been filled.
Hope you enjoyed reading and understanding the code for the tic tac toe game we can even design tic tac toe with a 4x4 grid in a similar way.