You are on page 1of 27

2024

PROJECT REPORT

GAME USING PYTHON (COMMAND LINE)

GUIDED BY : PROF. M.M.GOSWAMI SIR


Program name and code: CO6I Academic Year: 2023-2024

Course name and code: PWP (22616) Semester: 6th

A PROJECT REPORT ON
Game: tic tac toe
Sr NO. Name of Students Seat No. Roll No. Enrollment No.

1) Purwa Narayan Chaudhari 481014 3101 2114320035

2) Rajnandeeni Rajendra 481027 3112 2114320053


Patil
3) Chetana Abhiman Bedse 481040 3154 2114320075

4) Shambhavi Anil Bedse 4810 3143 2114320041

5) Vrushika Hemraj More 481059 3136 2214320305

Submitted on 02 / 04 /2024 by the group of 6 students


Under the guided by
Prof. M.M.GOSWAMI SIR
Third year Diploma in Computer Engineering & Technology of
Maharashtra State Board of Technical Education, Mumbai (Autonomous)

1|P age
MAHARASHTRA STATE BOARD OF TECHNICAL

EDUCATION, MUMBAI

Certificate
This is to certify that, Purwa Chaudhari, Rajnandeeni Patil ,Chetana
Bedse ,Shambhavi Bedse, Vrushika More of Sixth Semester of Computer
Engineering Diploma Program at (1432) Government Polytechnic
Nandurbar, has completed the satisfactorily in Subject PWP(22616 ) in the
academic year 2023 - 2024 as prescribed in the MSBTE curriculum of I
Scheme.

Place: Nandurbar

Date: 02/04/2024

Enrolment No:
1) 2114320035
2) 2114320053
3) 2114320075
4) 2114320041
5) 2214320305

Project Guide :- Head of Department :- Principal :-

M.M.Goswami Sir S.B.Thakre Sir Dr. S.B.Wesley

2|P age
Sr no Content Page no

1 Introduction of 4
python
2 Objectives 5

3 Scope of Project 6

4 Literature 8
Review
5 Implementation 10

6 Usermanual 13

7 Source Code 14

8 Output 15

9 Future work 16

Conclusion
10

3|P age
Introduction of python :-

Tac Tic Toe is traditionally played on a 3*3 grid. Players take turns placing a mark in one
of the cells of the grid. The goal of the game is for players to position their marks so that
they make a continuous line of three cells vertically , horizontally, or diagonally.The
game ends either when one player achieves this goal or when all spaces on the board are
filled, resulting in a draw. It’s a simple yet classic game often used to introduce basic
strategic thinking.

Tic-tac-toe, also known as noughts and crosses or Xs and Os, is a classic paper-and-pencil
game for two players. The players take turns marking spaces in a 3x3 grid with their
respective symbols, typically X and O, with the goal of getting three of their marks in a
row horizontally, vertically, or diagonally before their opponent does. It's a game of
strategy and often ends in a draw if both players play optimally.

The origins of tic-tac-toe trace back centuries, with some theories suggesting it
originated in ancient Egypt or Rome. However, the modern version of the game likely
evolved in Europe during the 18th or 19th centuries. It gained popularity as a pencil-and-
paper game in the 20th century, especially among schoolchildren.

The game's simplicity and accessibility contributed to its widespread adoption. Tic-tac-
toe's straightforward rules and minimal equipment requirements made it a popular
pastime in various cultures around the world. Over time, it has been adapted into digital
formats, including computer and mobile applications, further cementing its status as a
timeless classic.

Tic-tac-toe is primarily used as a recreational game, often played for entertainment or


as a quick diversion. While it may not have direct practical applications in the same way
as other games or activities, it does offer benefits such as:
4|P age
1. *Strategy development:* Despite its simplicity, tic-tac-toe requires players to think
strategically to win or at least avoid losing. It can help develop basic strategic thinking
skills, such as recognizing patterns and anticipating opponents' moves.

2. *Critical thinking:* Playing tic-tac-toe involves analyzing the game board and making
decisions based on current positions and potential future outcomes. This can enhance
critical thinking abilities and decision-making skills.

3. *Pattern recognition:* Tic-tac-toe involves identifying patterns and sequences, which


can be useful in various contexts, such as mathematics and problem-solving.

4. *Teaching tool:* Tic-tac-toe is often used as an educational tool for teaching concepts
like game theory, logic, and even programming basics in a simple and accessible manner.

While tic-tac-toe may not have direct practical applications in professional or academic
settings, its simplicity and accessibility make it a valuable tool for developing various
cognitive

5|P age
Objectives :-

The primary objectives of the Tic Tac Toe project are as follows:

The game is played on a grid that’s 3 squares by 3 squares

1. You are X, your friend (or the computer in this case) is O. Players take turns
putting their marks in empty squares

2. The first player to get 3 of her marks in a row (up, down ,across, or diagonally is
the winner

3. When all 9 squares are full, the game is over, if no player has 3 marks in a row the
game ends in a tie .d

6|P age
 Scope of the Project;-

The Tic tac toe project will cover the following key functionalities:

- Tic tac toe is a game which can be played by people of all ages, anytime used in
enhancing the players thinking by teaching the concepts of good sportsmanship and the
branch of artificial intelligence that deals with the searching of game trees.

- The goal of the game is for players to position their marks so that they make a
continous line of three cells vertically, horizontally, or diagonally . An opponent can
prevent a win by blocking the completion of the opponent’s line. In our variant of the
game, players placed object on a board .

7|P age
 Literature Review;-
The square block (3*3) can be filled with a cross(x) or a circle(o) in the Tic Tac Toe game
which can be played by two players. By allowing each player to mark their move, the game
will switch between the vertical or diagonal line with three identical markers the program
displays which plater has won whether X or O The game is constructed such that two players
can play tic tac toe by entering the position to place their marks on the board. The software
will have a display function and a select function for placing the symbol as well as a toggle
function for switching between the symbol as well as a toggle function for switching between
the symbols, allowing each participant to take a turn playing the game. After each player
makes a move, the computer will update and check for game circumstances as the game
progresses.

8|P age
 Implementation;-

Here is the basic implementation of a two player Tic Tac Toe in python. For each
player (1 or 2) , calls the random_place() function to randomly choose a location on a
board and mark that location with the player number.To understand the program of tic
tac toe you should have a basic idea about the following python concepts :

1. Python Functions
2. Python Loops

9|P age
 User Manual;-

Step 1

The following line initializes a list called board with 9 spaces, representing the 9
spaces on the Tic Tac Toe board.

Step 2
The following function print_board() will print the Tic Tac Toe board. It creates 3
strings, row1, row2, and row3, to represent each row of the board. The spaces in the
board are filled with the values from the board list. The function then prints each row
with separators to create the appearance of a 3×3 grid.

Step 3
The following function then prints each row with separators to create the appearance
of a 3×3 grid. The player_move() function allows a player to make a move by marking
a space on the board with their icon (X or O). It takes the player’s icon as an argument
and asks the player to enter their move (1-9) in the terminal. If the space the player
chooses is blank, the function will place the player’s icon in that space. Otherwise, it
will inform the player that the space is full.

Step 4
This function is_victory() checks if a player has won the game. It takes the player’s
icon as an argument and checks if there are three of that icon in a row on the board. It
returns True if a player has won, otherwise it returns False.

Step 5

This function is_draw() checks if the game is a draw by checking if all spaces on the
board are filled. When all spaces are filled and no player has won, it returns True,
otherwise it returns False.

Step 6
The while loop continues the game until a player has won or the game is a draw.
Within the loop, the board is printed. Then, the player_move() function calls and
allows the first player to make a move. The board prints again to show the updated
state. Then, calling the is_victory() function will check if the first player has won.
When they win, the game ends and a victory message shows If not, the is_draw()
10 | P a g e
function is called to check if the game is a draw.

If the game is a draw, a message is displayed and the game ends. If neither the first
player nor the game has won or ended in a draw, the second player makes a move and
the process repeats.

11 | P a g e
Source code;-

from color import Color

def make_new_board(rows, cols):


return [([0]*cols) for row in range(rows)]

def get_rows(board):
return len(board)

def get_cols(board):
return len(board[0])

def get_piece(board, row, col):


return board[row][col]

def set_piece(board, row, col, value):


board[row][col] = value
return board

def is_empty(board, row, col):


return (get_piece(board, row, col) == 0)

def is_on_board(board, row, col):


rows = get_rows(board)
cols = get_cols(board)
return ((row >= 0) and (row < rows) and
(col >= 0) and (col < cols))

def get_piece_label(piece):
if piece == 1:
return "|" + Color.PURPLE + Color.BOLD + "X" + Color.END +
"|"
elif piece == 2:
return "|" + Color.BLUE + Color.BOLD + "O" + Color.END + "|"
12 | P a g e
else:
return Color.BOLD + "|_|" + Color.END

def print_board(board):
print("\n---------------------------------------------------------------------------
--------------------------------")
rows = get_rows(board)
cols = get_cols(board)
for row in range(rows):
for col in range(cols):
piece = get_piece(board, row, col)
label = get_piece_label(piece)
print(label, end="")
print()

def did_win(board, player, win_length):


rows = get_rows(board)
cols = get_cols(board)
for start_row in range(rows):
for start_col in range(cols):
if did_win_1(board, player, win_length, start_row, start_col):
return True
return False

def did_win_1(board, player, win_length, start_row, start_col):


for drow in range(-1, +2):
for dcol in range(-1, +2):
if (drow != 0 or dcol != 0):
if did_win_2(board, player, win_length,
start_row, start_col, drow, dcol):
return True
return False

def did_win_2(board, player, win_length,


start_row, start_col, drow, dcol):
rows = get_rows(board)
cols = get_cols(board)
for step in range(win_length):
13 | P a g e
row = start_row + step*drow
col = start_col + step*dcol
if not is_on_board(board, row, col):
return False
elif get_piece(board, row, col) != player:
return False
return True

def other_player(player):
return 1 if player == 2 else 2

def oops(msg):
print(" ", msg, Color.RED + Color.BOLD + "Try again." +
Color.END)

def read_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
oops(Color.RED + Color.BOLD + "Input must be an integer." +
Color.END)

def get_move(board, player):


while True:
print_board(board)
print("Enter move for player:" + get_piece_label(player))
row = read_int(" Row --> ") - 1
col = read_int(" Col --> ") - 1
if not is_on_board(board, row, col):
oops(Color.RED + Color.BOLD + """\n \n Out of range (not on
the board)!""" + Color.END)
elif not is_empty(board, row, col):
oops(Color.RED + Color.BOLD + """\n Already occupied!""" +
Color.END)
else:
return (row, col)

14 | P a g e
def finish():
answer = input(Color.PURPLE + Color.BOLD + """Would you like to
play again?! (press 'y' for yes and 'n' for no):""" + Color.END)
if answer.lower() == "n":
print("")
print(Color.BLUE + Color.BOLD + "Bye Bye! See you next time!
:)" + Color.END)
return True
if answer.lower() == "y":
print(Color.BLUE + Color.BOLD + """\nGreat choice! The game
will start again""" + Color.END)
return False

def play_tic_tac_toe():
rows, cols = 3, 3
win_length = 3
board = make_new_board(rows, cols)
moves = 0
player = 1

while moves < rows * cols:


row, col = get_move(board, player)
board = set_piece(board, row, col, player)
if did_win(board, player, win_length):
print_board(board)
print("")
print(Color.RED + Color.BOLD + "The Player",
get_piece_label(player), "IS THE WINNER OF THE GAME!!! " +
Color.END)
return
player = other_player(player)
moves += 1
print(Color.RED + Color.BOLD + "TIE GAME!" + Color.END)

def main():
while True:
play_tic_tac_toe()
if finish():
15 | P a g e
break

if _name_ == '_main_':
main()

 Module;-

# color.py

class Color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'

16 | P a g e
 Output;-

17 | P a g e
 future work:-
Future work includesclassic game contributes to children's developmental
growth in numerous ways including their understanding of predictability,
problem solving, spatial reasoning, hand-eye coordination, turn taking, and
strategizing.

18 | P a g e
 Conclusion;-
In conclusion, The player who succeeded in placing three respective marks in a
horizontal, vertical, or diagonal row wins the game. The Tic Tac Toe is a great way to
pass your free time whether you're standing in a line or spending time





19 | P a g e
 Weekly Work / Progress Report
Details of 16 Engagement Hours of the Student Regarding
Completion of the Project
No. Date Timing Week Work Or Activity Sign Of
Performed The
Guide
From To Duration In
Hours
1 02/01/2024 2:00 5:00 3 Discussion and
Finalization of the
Project Title
2 09/01/2024 2:00 3:00 1 Preparation and
submission of
Abstract
3 16/01/2024 2:00 3:00 1 Literature review

4 23/01/2024 2:00 5:00 3 Collection of Data


5 30/01/2024 2:00 3:00 1 Sorting of Data
6 06/02/2024 2:00 3:00 1 Discussion and
Outline of contents
7 20/02/2024 2:00 5:00 3 Rough Writing of the
Projects Content
8 05/03/2024 2:00 3:00 1 Editing and Proof
Reading of the
contents
9 19/03/2024 2:00 3:00 1 Final completion of
the project
10 02/04/2024 2:00 3:00 1 Seminar Presentation,
viva-vice, Assessment
and Submission of
Report

20 | P a g e
Annexure:-

Academic Name of Guider:


Year: -2023-2024 Prof. M.M. Goswami Sir

Semester: - sixth Program Name: Computer Engineering

Subject Subject Code: 22616


Name: -PWP
1. Purwa Narayan Chaudhari
2. Rajnandeeni Rajendra Patil
Name of 3. Chetana Abhiman Bedse
Students: - 4. Shambhavi Anil Bedse
5. Vrushika Hemraj More

Title of the Game :- TIC TAC TOE


Project: -

21 | P a g e
Remark :-

Marks out Marks out of 4


of for performance
Roll Students 6 for in Total Remark
NO: Name performance oral/presentation
in activities

3101 Purwa Narayan


Chaudhari

3112 Rajnandeeni
Rajendra Pati

3175 Chetana Abhiman


Bedse

3141 Shambhavi Anil


Bedse
3136 Vrushika
Hemraj
More

22 | P a g e
Reference:-

 https:/ /en.m.wikipedia.org
 https://www.wikihow.com
 Chat GPT

23 | P a g e
24 | P a g e
25 | P a g e
26 | P a g e

You might also like