0% found this document useful (0 votes)
162 views

Experiment-5: AIM: Write A Program To Implement The Tic-Tac-Toe Game Problem. Theory

The document describes an experiment to program a Tic-Tac-Toe game. It explains the rules of Tic-Tac-Toe and the optimal strategy that leads to a draw. It then provides the Python code to implement the game. The code includes functions to create an empty board, select random places on the board, check for wins in rows, columns and diagonals, and evaluate the winner or tie after each move. The main function runs the game logic and prints the output.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views

Experiment-5: AIM: Write A Program To Implement The Tic-Tac-Toe Game Problem. Theory

The document describes an experiment to program a Tic-Tac-Toe game. It explains the rules of Tic-Tac-Toe and the optimal strategy that leads to a draw. It then provides the Python code to implement the game. The code includes functions to create an empty board, select random places on the board, check for wins in rows, columns and diagonals, and evaluate the winner or tie after each move. The main function runs the game logic and prints the output.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT- 5

AIM: Write a program to implement the Tic-Tac-Toe game problem.

Theory:
Tic-tac-toe who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing
three of their marks in a horizontal, vertical, or diagonal row wins the game. Players soon
discover that the best play from both parties leads to a draw. Hence, tic-tac-toe is most often
played by young children, who often have not yet discovered the optimal strategy. Because of
the simplicity of tic-tac-toe, it is often used as a pedagogical tool for teaching the concepts of
good sportsmanship and the branch of artificial intelligence that deals with the searching of game
trees. It is straightforward to write a computer program to play tic-tac-toe perfectly or to
enumerate the 765 essentially different positions (the state space complexity) or the 26,830
possible games up to rotations and reflections (the game tree complexity) on this space. The
game can be generalized to an m,n,k-game in which two players alternate placing stones of their
own colour on an m×n board, with the goal of getting k of their own colour in a row. Tic-tac-toe
is the (3,3,3)-game. Tic-tac-toe is the game where n equals 3 and d equals 2. If played properly,
the game will end in a draw, making tic-tac-toe a futile game.

Code:
# Tic-Tac-Toe Program using
# random number in Python
# importing all necessary libraries
import numpy as np
import random
from time import sleep
# Creates an empty board
def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
# Check for empty places on board
def possibilities(board):
l = []
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
# Select a random place for the player
def random_place(board, player):
selection = possibilities(board)
current_loc = random.choice(selection)
board[current_loc] = player
return(board)
# Checks whether the player has three
# of their marks in a horizontal row
def row_win(board, player):
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
# Checks whether the player has three
# of their marks in a vertical row
def col_win(board, player):
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
# Checks whether the player has three
# of their marks in a diagonal row
def diag_win(board, player):
win = True
for x in range(len(board)):
if board[x, x] != player:
win = False
return(win)
# Evaluates whether there is
# a winner or a tie
def evaluate(board):
winner = 0

for player in [1, 2]:


if (row_win(board, player) or
col_win(board,player) or
diag_win(board,player)):
winner = player
if np.all(board != 0) and winner == 0:
winner = -1
return winner
# Main function to start the game
def play_game():
board, winner, counter = create_board(), 0, 1
print(board)
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + " move")
print(board)
counter += 1
winner = evaluate(board)
if winner != 0:
break
if winner == -1:
return("DRAW")
return(winner)
# Driver Code
print("Winner is: " + str("DRAW" if play_game() == -1 else play_game()))
print("\nThis program is submitted by Shubham Vashisth\nEnrollment Number: A2305216464")
OUTPUT

Result:
The program is successfully written and compiled in python.

You might also like