0% found this document useful (0 votes)
17 views14 pages

Tic-Tac-Toe Game Project in Python

The document describes a Python programming project to create a Tic-Tac-Toe game. It includes an introduction to the classic Tic-Tac-Toe game and the project's aim to develop the two-player board game. The program code uses a dictionary to represent the game board and tracks turns with variables. It prints the board after each move, checks for a win condition, and switches players. The project helped students learn Python functions, data structures, and teamwork skills.

Uploaded by

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

Tic-Tac-Toe Game Project in Python

The document describes a Python programming project to create a Tic-Tac-Toe game. It includes an introduction to the classic Tic-Tac-Toe game and the project's aim to develop the two-player board game. The program code uses a dictionary to represent the game board and tracks turns with variables. It prints the board after each move, checks for a win condition, and switches players. The project helped students learn Python functions, data structures, and teamwork skills.

Uploaded by

Shivam Jawarkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MANAV SCHOOL OF POLYTECHNIC VYALA,

AKOLA

DEPARTMENT OF COMPUTER ENGINEERING

Academic Year 2023-24

Semester VI

Programming with Python [22616]


Submitted To M.S.B.T.E. In Partial Fulfilment of the Requirement forthe
Diploma in Computer Engineering.

Project on: - “ Tic-Tac-Toe Game”

Submitted By

Mr. Mohammad umair abdul aziz


Mr. Shivam Pandhari Jawarkar
Mr. Mohammed jawwad baig

Project Guide- [Link] kod e

1
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
CERTIFICATE

This is to certify that Mr./Ms. …………………………………………................


Roll No………, of six Semester of Diploma in …………......………………….

………………………………….of Institute, ……………………………………………..…….

………………………… (Code:1668) has completed the Micro-Project satisfactorily


in Programming with Python (22616) for the academic year2023 to 2024 as
prescribed in the curriculum.

Place:……………………………… Enrolment No:…………………………….

Date:………………………………. Exam Seat No:……………………………..

Subject Teacher Head of the Department Principal

2
INDEX
Sr. Topic page
NO No
1 4
Introduction To
Tic-Tac-Toe
Game

2 5
Abstract

3 Rationale 6

4 6
Aim of the project

5 Course Outcomes Achieved 6

6 Program code 7

7 Output 11

8 Skill Developed 13

9 Applications of the Project 13

10 Refernces 14

3
Introduction To Tic-Tac-Toe
Game

Tic tac toe Python, also known as Noughts and Crosses or Xs and Os, is
a very simple two-player game where both the player get to choose any of the
symbols between X and O. This game is played on a 3X3 grid board and one by
one each player gets a chance to mark its respective symbol on the empty
spaces of the grid.

Once a player is successful in marking a strike of the same symbol either


in the horizontal, vertical or diagonal way as shown in the picture below is
created that player wins the game else the game goes on a draw if all the spots
are filled.

In the tic tac toe Python game that we shall be building, we require two
players. These two players will be selecting their respective two signs which
are generally used in the game that is, X and O. The two players draw
the X and O on an alternative basis on the 3x3 grid having 9 empty boxes.

4
Abstract

This paper describes the main features and application of python


[Link] book contains a complete learning of python programming.
This book covers game programming in three gifferent scripting lanuages i.e
python. Lua and ruby.

5
1.0 Rationale
The objective of this project is to develop the well-known
board game Tic-Tac- Toe for two players.
Generally, this is a two-player strategy board game. The Tic-
Tac-Toe game is based on having a game board (2D array) of size 3 x
3. The players alternate placing Xs and Os on the board until either
one has placed three Xs or Os in a row horizontally, vertically, or
diagonally; or all nine board squares are filled. The player wins if
s/he draws three Xs or three Os in a row. Otherwise, the game is
draw.
Initially the board grid squares are initialized to zeros. Xs and
Os might be denoted by numbers inside the board grid by ones and
twos respectively. I.e. if player one chooses X, the location of that
choice is registered as 1 and when player two chooses O the location
of that choice in your array is registered as 2. At the end if a row of 1s
is registered then player one won the game. Or if a row of 2s is
registered thus player two won the game. If not, the game is draw.
The game ends when there is no more empty fields in the array
(board) to fill or if one of the players wins the game.

2.0 Aim of the project


1. To develop an interesting game for children to pass their free time.
2. To develop the well-known board game Tic-Tac-Toe for two players.
3. To be one of the players to get three same symbols in a row –
horizontally, vertically or diagonally on a 3 x 3 grid.

3.0 Course Outcomes Achieved


1. Display message on screen using Python script on IDE.
2. Develop python program to demonstrate use of Operators.
3. Perform operations on data structures in Python.

6
4. Develop functions for given problems.

 Program code

#Implementation of Two Player Tic-Tac-Toe game in

Python. theBoard = {'1': ' ' , '2': ' ' , '3': ' ' ,
'4': ' ' , '5': ' ' , '6': ' ' ,
'7': ' ' , '8': ' ' , '9': ' ' }

board_keys = []

for key in theBoard:


board_keys.append(key)

def printBoard(board):
print("\n")
print(' '+board['1'] + ' | ' + board['2'] + ' | ' +
board['3']) print(' ---+---+---')
print(' '+board['4'] + ' | ' + board['5'] + ' | ' +
board['6']) print(' ---+---+---')
print(' '+board['7'] + ' | ' + board['8'] + ' | ' +
board['9']) print("\n")

# Now we'll write the main function which has all the gameplay
functionality. def game():

turn = 'X'
count = 0

for i in range(10):
printBoard(theBoard)

7
move=input("It's your turn," + turn + ". Move to which

place? ") #move = input()

if theBoard[move] == ' ':


theBoard[move] = turn
count += 1
else:
print("THAT PLACE IS ALREADY FILLED.\nMove to which place?")
continue

# Now we will check if player X or O has won,for every move


after 5 moves. if count >= 5:
if theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the top
printBoard(theBoard)
print("\nGAME OVER.\n\n")
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break
elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle
printBoard(theBoard)
print("\nGAME OVER.\n\n")
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break
elif theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the bottom
printBoard(theBoard)
print("\nGAME OVER.\n\n")
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break
elif theBoard['7'] == theBoard['4'] == theBoard['1'] != ' ': # down the left side
printBoard(theBoard)
print("\nGAME OVER.\n\n")

8
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break
elif theBoard['8'] == theBoard['5'] == theBoard['2'] != ' ': # down the
middle printBoard(theBoard)
print("\nGAME OVER.\n\n")
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break
elif theBoard['9'] == theBoard['6'] == theBoard['3'] != ' ': # down the right side
printBoard(theBoard)
print("\nGAME OVER.\n\n")
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break
elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': #
diagonal printBoard(theBoard)
print("\nGAME OVER.\n\n")
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break
elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': #
diagonal printBoard(theBoard)
print("\nGAME OVER.\n\n")
print(" ****** CONGRATULATIONS! " +turn + " YOU WON THE GAME
******\n\n")
break

# If neither X nor O wins and the board is full, we'll declare the
result as 'tie'. if count == 9:
print("\nGAME OVER.\n\n")
print("*** IT'S A TIE!!
***\n\n")
break

9
# Now we have to change the player after
every move. if turn =='X':
turn = 'O'
else:
turn = 'X'

# Now we will ask if player wants to restart the game


or not. restart = input("Do want to play Again?(y/n)")
if restart == "y" or restart ==
"Y": for key in board_keys:
theBoard[key] =

" " game()

if name == " main ":


game()

10
Output:-

11
12
Skill Developed/ learning out of this Micro-Project We learnt,

1. To demonstrate the use of Operators.


2. To perform operations on data structures in Python.
3. To develop functions for given problems.
4. Efficient communication skills.
5. Working as a team member for developing c program.
6. Developing leadership qualities.

9.0 Applications of the Project

1. Thisproject can be used as an interesting game for children to pass their free
time.
2. The project can be also used to understand the.
3. The project can be used in learning the

13
References :-

i.

14

Common questions

Powered by AI

The primary learning objectives achieved include the ability to display messages on screen using Python scripts in an IDE, developing Python programs to demonstrate the use of operators, performing operations on data structures, and developing functions for given problems. Additionally, the project emphasizes efficient communication skills, teamwork, and developing leadership qualities .

The project provides a hands-on experience in applying Python programming concepts such as data structures, control flow, and functions. It facilitates students' understanding of developing logical solutions and coding standard practices. However, the project might not cover advanced topics like error handling, optimization, or integrating with more complex libraries, creating potential learning gaps that could be addressed by expanding the scope to include these elements .

Python's simplicity and readability make it an ideal educational tool for demonstrating fundamental programming concepts through projects like Tic-Tac-Toe. Its syntax closely mimics natural language, easing the learning curve for beginners. Additionally, Python is widely used in academia and industry, providing students with practical skills applicable beyond the educational context .

The Tic-Tac-Toe game utilizes Python data structures like dictionaries to hold the state of the board. Functions are created to print the board, manage game flow, handle player moves, and check for win or draw conditions. This separation of concerns allows organized and reusable code, demonstrating functional programming principles by encapsulating logic and tasks into discrete, manageable functions .

The project's rationale centers on applying and understanding Python through developing a Tic-Tac-Toe game, which aligns with the learning outcomes like using Python to perform operations and develop functions. The execution of the project through practical coding exercises ensures that students meet the defined educational objectives, providing a strong foundation in basic programming principles. However, for complete alignment, including more complex scenarios could enhance learning depth .

The Tic-Tac-Toe board is initialized as a dictionary where each key represents a grid position labeled from '1' to '9', with each initially set to a space character to indicate an empty spot. The two players alternate placing 'X' or 'O', changing the board's state. The board is visually represented and updated with each player's move, allowing the game to track state changes and determine winners by analyzing row, column, and diagonal configurations .

The project uses a simple prompt at the end of each game asking players if they wish to restart the game. By continuously resetting the board and allowing players to start a new game upon completion, it encourages replayability by keeping the engagement active and giving users control over the continuation of gameplay .

The report emphasizes teamwork as crucial for successfully executing the project. Collaboration among members aids in brainstorming and problem-solving, resulting in more efficient workflow and improving the project's quality. Working as a team also helps develop interpersonal skills like communication and leadership, which are invaluable in both academic and professional settings .

A winner in the Tic-Tac-Toe project is determined if a player manages to place three identical symbols in a row either horizontally, vertically, or diagonally on the 3x3 grid. The logic implemented in the Python program checks for this condition after every move starting from the fifth move. If the condition is met, the game declares the respective player as the winner. If the grid is fully filled and no player has met the winning condition, the game ends in a draw .

The game prompts players for position inputs, and before updating the board, it checks whether the chosen spot is already occupied. This prevents overwriting moves and maintains game integrity. Additional loops request re-input if an invalid or occupied position is selected, ensuring users only make valid moves, which is crucial for maintaining fair and expected gameplay dynamics .

You might also like