oggy python
oggy python
PROJECT REPORT
ON
“TIC TAC TOE GAME USING PYTHON”
SUBMITTED BY
Mst. Mahajan Atharva Vasant (145)
Under The Guidance Of
MR. M.V.Khasne
2024-2025
1
Sanjivani Rural Education Society’s
SANJIVANI K.B.P. POLYTECHNIC
Department of Computer Technology
SUBMITTED BY
Mst. Mahajan Atharva Vasant(145)
Under our supervision and guidance for partial fulfilment of the requirement for
DIPLOMA IN COMPUTER TECHNOLOGY affiliated to
Maharashtra State Board of Technical Education, Mumbai
For academic year
2024-2025
2
ACKNOWLEDGEMENT
3
Table of Contents
1. 5
ABSTRACT
2. INTRODUCTION 6
3. CODE 7-8
4. OUTPUT 9
5. CONCLUSION 10
6. REFERENCE 11
4
ABSTRACT
This Python program is designed to automate the classic two-player Tic Tac Toe
game using a structured, text-based interface. The core functionality of the
program revolves around checking valid moves, alternating player turns, and
determining game outcomes based on predefined win conditions. The program
utilizes a 3x3 grid and maps user input to board positions, ensuring data validity
by restricting entries to numeric values between 1 and 9 and preventing duplicate
or invalid moves. The game logic dynamically updates the board and evaluates
winning combinations across rows, columns, and diagonals after each turn. Upon
completion, the result is clearly presented, indicating whether a player has won
or if the game ended in a draw. This program enhances interactivity, minimizes
gameplay errors, and serves as a valuable tool for beginners learning Python
through game development. It can be further extended to include features such as
a graphical user interface (GUI), AI opponent, or score tracking for multiple
rounds.
5
INTRODUCTION
Throughout the game, the program continuously updates and displays the
current state of the board, enabling both players to track progress in real time.
After each move, the game logic checks for a winning combination by
evaluating rows, columns, and diagonals. If a winning condition is detected, the
program announces the winner and concludes the match. In cases where all nine
blocks are filled without a winner, the program declares a draw. This real-time
evaluation ensures accurate and fair results with every round.
The Tic Tac Toe program is particularly useful for beginners in Python
programming, educators, and anyone interested in building logical thinking
through interactive applications. It simplifies the complexities of game design
by demonstrating core programming concepts such as input handling,
conditionals, loops, and list manipulation. The program can also be extended or
customized with additional features, such as a graphical user interface (GUI), an
AI opponent, or score tracking across multiple rounds. Overall, this Python-
based game is a fun yet practical tool that introduces foundational programming
skills in an engaging and user-friendly format
6
CODE
def print_board(board):
for i in range(3):
print(" | ".join(board[i]))
if i < 2:
print("-" * 5)
def is_full(board):
return all(cell != " " for row in board for cell in row)
def get_position(choice):
mapping = {
1: (2, 0), 2: (2, 1), 3: (2, 2),
4: (1, 0), 5: (1, 1), 6: (1, 2),
7: (0, 0), 8: (0, 1), 9: (0, 2),
7
}
return mapping.get(choice, (-1, -1))
def main():
board = [[" " for _ in range(3)] for _ in range(3)]
current_player = "X"
while True:
print_board(board)
try:
choice = int(input(f"Player {current_player}, choose a position (1-9): "))
except ValueError:
print("Please enter a number from 1 to 9.")
continue
board[row][col] = current_player
if check_winner(board, current_player):
print_board(board)
print(f" Player {current_player} wins!")
break
elif is_full(board):
print_board(board)
print("It's a tie!")
break
8
current_player = "O" if current_player == "X" else "X"
if __name__ == "__main__":
main()
OUTPUT
9
CONCLUSION
This Python program efficiently automates the classic two-player Tic Tac Toe
game using a simple text-based interface. It ensures valid user inputs, enforces
game rules, and dynamically updates the game board with each turn. The
program includes essential logic for detecting winning conditions, handling
invalid moves, and declaring outcomes such as wins or ties, ensuring a fair and
engaging gameplay experience.
10
REFERENCE
Python Documentation – The official Python documentation provides
comprehensive details on functions, lists, loops, and user input handling that
form the foundation of this game.
• Python Official Documentation
Conditional Statements in Python – The game's logic for checking wins,
handling turns, and validating moves relies heavily on if-elif-else structures.
• Python If-Else Statements
Handling User Input and Exceptions – Ensuring that users enter valid moves
(numbers between 1 and 9) is achieved through input validation and exception
handling using try-except blocks.
• Python Try-Except for Error Handling
Working with Lists and 2D Arrays – The 3x3 game board is implemented
using nested lists (2D arrays), a core concept in Python for managing structured
data.
• Python Lists and Data Structures
Loops in Python – While and for loops are used to control game flow,
repeatedly update the board, and manage user turns until a winner is found or
the board is full.
• Python Loops (While and For)
11