Python Gro
Python Gro
AKOLA
Semester VI
Submitted By
1
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
CERTIFICATE
2
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
CERTIFICATE
3
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
CERTIFICATE
4
INDEX
Sr. Topic page
NO No
1 6
Introduction To
Tic-Tac-Toe
Game
2 7
Abstract
3 Rationale 8
4 8
Aim of the project
6 Program code 9
7 Output 14
8 Skill Developed 16
10 Conclusion 17
11 References 17
5
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.
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.
6
Abstract
7
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.
8
Program
import tkinter as tk
from itertools import cycle
from tkinter import font
from typing import NamedTuple
class Player(NamedTuple):
label: str
color: str
class Move(NamedTuple):
row: int
col: int
label: str = ""
BOARD_SIZE = 3
DEFAULT_PLAYERS = (
Player(label="X", color="blue"),
Player(label="O", color="green"),
)
class TicTacToeGame:
def __init__(self, players=DEFAULT_PLAYERS, board_size=BOARD_SIZE):
self._players = cycle(players)
self.board_size = board_size
self.current_player = next(self._players)
self.winner_combo = []
self._current_moves = []
self._has_winner = False
self._winning_combos = []
self._setup_board()
def _setup_board(self):
self._current_moves = [
[Move(row, col) for col in range(self.board_size)]
9
for row in range(self.board_size)
]
self._winning_combos = self._get_winning_combos()
def _get_winning_combos(self):
rows = [
[(move.row, move.col) for move in row]
for row in self._current_moves
]
columns = [list(col) for col in zip(*rows)]
first_diagonal = [row[i] for i, row in enumerate(rows)]
second_diagonal = [col[j] for j, col in enumerate(reversed(columns))]
return rows + columns + [first_diagonal, second_diagonal]
def toggle_player(self):
"""Return a toggled player."""
self.current_player = next(self._players)
def has_winner(self):
"""Return True if the game has a winner, and False otherwise."""
10
return self._has_winner
def is_tied(self):
"""Return True if the game is tied, and False otherwise."""
no_winner = not self._has_winner
played_moves = (
move.label for row in self._current_moves for move in row
)
return no_winner and all(played_moves)
def reset_game(self):
"""Reset the game state to play again."""
for row, row_content in enumerate(self._current_moves):
for col, _ in enumerate(row_content):
row_content[col] = Move(row, col)
self._has_winner = False
self.winner_combo = []
class TicTacToeBoard(tk.Tk):
def __init__(self, game):
super().__init__()
self.title("Tic-Tac-Toe Game")
self._cells = {}
self._game = game
self._create_menu()
self._create_board_display()
self._create_board_grid()
def _create_menu(self):
menu_bar = tk.Menu(master=self)
self.config(menu=menu_bar)
file_menu = tk.Menu(master=menu_bar)
file_menu.add_command(label="Play Again", command=self.reset_board)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit)
menu_bar.add_cascade(label="File", menu=file_menu)
def _create_board_display(self):
display_frame = tk.Frame(master=self)
11
display_frame.pack(fill=tk.X)
self.display = tk.Label(
master=display_frame,
text="Ready?",
font=font.Font(size=28, weight="bold"),
)
self.display.pack()
def _create_board_grid(self):
grid_frame = tk.Frame(master=self)
grid_frame.pack()
for row in range(self._game.board_size):
self.rowconfigure(row, weight=1, minsize=50)
self.columnconfigure(row, weight=1, minsize=75)
for col in range(self._game.board_size):
button = tk.Button(
master=grid_frame,
text="",
font=font.Font(size=36, weight="bold"),
fg="black",
width=3,
height=2,
highlightbackground="lightblue",
)
self._cells[button] = (row, col)
button.bind("<ButtonPress-1>", self.play)
button.grid(row=row, column=col, padx=5, pady=5, sticky="nsew")
12
self._highlight_cells()
msg = f'Player "{self._game.current_player.label}" won!'
color = self._game.current_player.color
self._update_display(msg, color)
else:
self._game.toggle_player()
msg = f"{self._game.current_player.label}'s turn"
self._update_display(msg)
def _highlight_cells(self):
for button, coordinates in self._cells.items():
if coordinates in self._game.winner_combo:
button.config(highlightbackground="red")
def reset_board(self):
"""Reset the game's board to play again."""
self._game.reset_game()
self._update_display(msg="Ready?")
for button in self._cells.keys():
button.config(highlightbackground="lightblue")
button.config(text="")
button.config(fg="black")
def main():
"""Create the game's board and run its main loop."""
game = TicTacToeGame()
board = TicTacToeBoard(game)
board.mainloop()
if __name__ == "__main__":
main()
13
14
15
Skill Developed/ learning out of this Micro-Project We learnt,
1. This project 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
16
Conclusion :-
We used this paper to learn different features available in python and its
applications. We used this book to learn the basic concepts of python
programming. We used this book to learn how to builds efficient, flexible and well
integrated programs and system.
References :-
YouTube
i.
17