Rajat and Naman

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

THE NEW ERA PUBLIC SCHOOL

Session -2022-23
Class – xii [commerce]
Subject: Informatics Practices [065]
TOPIC: TIC TAC TOE GAME

SUBMITTED BY SUBMITTED TO
Mas. Rajat Verma Mr. Navin Piplodiya
Mas. Naman Jain
1
Certificate
This is to certify that Mast. Rajat Verma &
Naman Jain Student of Class XI [commerce] of THE

NEW ERA PUBLIC SCHOOL, Indore has completed


a project work in the subject Informatics Practices
on the topic TIC TAC TOE GAME in the session
2022-23.

It is a bona fide piece of work under my supervision


and guidance to the best of my knowledge.

It is original work of the candidate and is the result of


his own efforts in partial fulfillment Examination
2022-23

Internal Signature Roll.No. _____________ Date : _______

Principal

2
ACKNOWLEDGEMENT
I extend my sincere thanks to my school THE NEW ERA

PUBLIC SCHOOL. Which provide me the opportunity to fulfill


my wish and achieve my goal.

I would like to express my special thanks and gratitude to


my teacher Mr. Navin Piplodiya who gave me the golden
opportunity to do this project on the TOPIC: TIC TAC TOE GAME.

I express my sincere gratitude for his encouragement and


help given during course of completion of Project.

I also thank our respected Principal Mrs. Archana Tiwari


for creating the required academic environment which made
my task appreciable during this Session.

Secondly I would also like to thank my parents and friends


who helped a lot to finalizing the project within the limited
time period.

3
Index
1. About Python 5–8
2. About Tic Tac Toe Game 9 – 10
3. Source code 11 – 18
4. Output Screens 19
5. Reference 20

4
About Python
Python is one of those rare languages which can claim to be
both simple and powerful. You will find yourself pleasantly
surprised to see how easy it is to concentrate on the solution to the
problem rather than the syntax and structure of the language you
are programming in.

Guido van Rossum, the creator of the Python language, named the
language after the BBC show "Monty Python's Flying Circus". He
doesn't particularly like snakes that kill animals for food by
winding their long bodies around them and crushing them.

5
Features of Python programming language

1. Readable: Python is a very readable language.

2. Easy to Learn: Learning python is easy as this is a expressive and


high level programming language, which means it is easy to
understand the language and thus easy to learn.

3. Cross platform: Python is available and can run on various


operating systems such as Mac, Windows, Linux, Unix etc. This
makes it a cross platform and portable language.

4. Open Source: Python is a open source programming language.


6
5. Large standard library: Python comes with a large standard
library that has some handy codes and functions which we can use
while writing code in Python.

6. Free: Python is free to download and use. This means we can


download it for free and use it in your application. See: Open Source
Python License. Python is an example of a FLOSS (Free/Libre Open
Source Software), which means we can freely distribute copies of
this software, read its source code and modify it.

7. Supports exception handling: If you are new, you may wonder


what is an exception? An exception is an event that can occur during
program exception and can disrupt the normal flow of program.
Python supports exception handling which means we can write less
error prone code and can test various scenarios that can cause an
exception later on.

8. Automatic memory management: Python supports automatic


memory management which means the memory is cleared and freed
automatically. You do not have to bother clearing the memory.

7
The official introduction to Python is:

Python is an easy to learn, powerful programming language. It has


efficient high-level data structures and a simple but effective
approach to object-oriented programming. Python's elegant syntax
and dynamic typing, together with its interpreted nature, make it
an ideal language for scripting and rapid application development
in many areas on most platforms.

8
INTRODUCTION

Tic- Tac- Toe


This game is very popular amongst all of us and even fun to
build as a Python project.
It is a two-player game and consists of a nine-square grid.
Each player chooses their move and with O or X and marks
their square one at each chance. The player who succeeds in
making their marks all in one line whether diagonally,
horizontally, or vertically wins. The challenge for the other
player is to block the game for their opponent and also to
make their chain.

9
Game Rules

1. Traditionally the first player plays with "X". So we can


decide who wants to go with "X" and who wants to go
with "O".
2. Only one player can play at a time.
3. If any of the players have filled a square then the other
player and the same player cannot override that square.
4. There are only two conditions that may match will be
draw or may win.
5. The player that succeeds in placing three respective marks
(X or O) in a horizontal, vertical, or diagonal row wins
the game.

Winning condition

Whoever places three respective marks (X or O) horizontally,

vertically, or diagonally will be the winner.

10
Source Code:
import random

import sys

board=[i for i in range(0,9)]

player, computer = '',''

# Corners, Center and Others, respectively

moves=((1,7,3,9),(5,),(2,4,6,8))

# Winner combinations

winners=((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))

# Table

tab=range(1,10)

def print_board():

x=1

11
for i in board:

end = ' | '

if x%3 == 0:

end = ' \n'

if i != 1: end+='---------\n';

char=' '

if i in ('X','O'): char=i;

x+=1

print(char,end=end)

def select_char():

chars=('X','O')

if random.randint(0,1) == 0:

return chars[::-1]

12
return chars

def can_move(brd, player, move):

if move in tab and brd[move-1] == move-1:

return True

return False

def can_win(brd, player, move):

places=[ ]

x=0

for i in brd:

if i == player: places.append(x);

x+=1

win=True

for tup in winners:

13
win=True

for ix in tup:

if brd[ix] != player:

win=False

break

if win == True:

break

return win

def make_move(brd, player, move, undo=False):

if can_move(brd, player, move):

brd[move-1] = player

win=can_win(brd, player, move)

if undo:

14
brd[move-1] = move-1

return (True, win)

return (False, False)

# AI goes here

def computer_move():

move=-1

# If I can win, others do not matter.

for i in range(1,10):

if make_move(board, computer, i, True)[1]:

move=i

break

if move == -1:

# If player can win, block him.

15
for i in range(1,10):

if make_move(board, player, i, True)[1]:

move=i

break

if move == -1:

# Otherwise, try to take one of desired places.

for tup in moves:

for mv in tup:

if move == -1 and can_move(board, computer, mv):

move=mv

break

return make_move(board, computer, move)

def space_exist():

16
return board.count('X') + board.count('O') != 9

player, computer = select_char()

print('Player is [%s] and computer is [%s]' % (player, computer))

result='%%% Deuce ! %%%'

while space_exist():

print_board()

print('#Make your move ! [1-9] : ', end='')

move = int(input())

moved, won = make_move(board, player, move)

if not moved:

print(' >> Invalid number ! Try again !')

continue

# if won:

17
result='*** Congratulations ! You won ! ***'

break

elif computer_move()[1]:

result='=== You lose ! =='

break;

print_board()

print(result)

18
OUTPUT

**********************

19
Reference
Informatics Practices -: Sultan Chand [ Preeti Arora]

Informatics Practices -: Sumita Arora

Web support - : Wikipedia and Other web blogs to create and this
project.

20

You might also like