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

Project

Uploaded by

Shailja Bahuguna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Project

Uploaded by

Shailja Bahuguna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

GAUTAM

INTERNATIONAL
SCHOOL
COMPUTER SCIENCE(PYTHON)

PROJECT

SUBMITTED TO: MRS.EKTA


CHOUDHARY
SUBMITTED BY:SHAILJA BAHUGUNA

CERTIFICATE
This certify that the project entitled “tic,
tac, toe.. game” is the benefitted work
done by “SHAILJA BAHUGUNA” of class
12 sci. A session 2019-2020.This project
has been carried out under my direct
provision and guidance.

EXAMINER TEACHER’s
SIGN. SIGN.
ACKNOWLEDGEMENT
I would like to express my special
gratitude and thanks to our Computer
science teacher,
Mrs. EKTA CHOUDHARY
for giving me attention and her precious
time.I would like to express my gratitude
towards my parents and teachers who
helped me in completion of this project
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 a 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, 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.
EXPLAINATION:

In the program, while loop runs until the


number guessed by the user is equal to the
generated number or the number of attempts
is less than the maximum number of chances.
If the number of attempts becomes greater
than the number of chances the game stops
and the user loses the game. If the user
guesses the correct number in the given
number of chances then he or she will win.
After every guess made by the user, the
program informs the user whether the
guessed number was smaller, greater than
the actual generated number.
In this code, a random number is generated
between 1-9 using the randint() function. The
user is given a specific number of chances to
guess the number if the chances exceed the
user guesses the user would lose.
# 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)
sleep(2)

while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + move")
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)

# Driver Code

print("Winner is: " + str(play_game()))


OUTPUT:
[[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

You might also like