Project
Project
INTERNATIONAL
SCHOOL
COMPUTER SCIENCE(PYTHON)
PROJECT
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:
import numpy as np
import random
from time import sleep
def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
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)
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)
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
winner = player
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