Question: What are some ideas I can use to modify this Tic Tac Toe game according to these instructions: Your program must incorporate the following: a
What are some ideas I can use to modify this Tic Tac Toe game according to these instructions:
Your program must incorporate the following:
a Use of loops and conditionals while loops and if then for example
b Use of exception handling when getting user input where applicable.
c Ability to save and retrieve data using either files andor databases. For example: if you make a game, save the high score.
d Ability to get input from the user, either through a GUI, the console or other method.
e Ability to display information, either through a GUI, the console or other method.
Tic Tac Toe Game in Python:
import tkinter as tk
from itertools import cycle
from tkinter import font
from typing import NamedTuple
Define Players and Moves'
class PlayerNamedTuple:
label: str
color: str
class MoveNamedTuple:
row: int
col: int
label: str
Set the board size and assign the X & O marks to each player'
BOARDSIZE
DEFAULTPLAYERS
PlayerlabelX color"black"
PlayerlabelO color"red"
Initialize
class TicTacToeGame:
def initself playersDEFAULTPLAYERS, boardsizeBOARDSIZE:
self.players cycleplayers
self.boardsize boardsize
self.currentplayer nextselfplayers
self.winnercombo
self.currentmoves
self.haswinner False
self.winningcombos
self.setupboard
def setupboardself:
self.currentmoves
Moverow col for col in rangeselfboardsize
for row in rangeselfboardsize
self.winningcombos self.getwinningcombos
def getwinningcombosself:
rows
moverow, move.col for move in row
for row in self.currentmoves
columns listcol for col in ziprows
firstdiagonal rowi for i row in enumeraterows
seconddiagonal colj for j col in enumeratereversedcolumns
return rows columns firstdiagonal, seconddiagonal
def toggleplayerself:
Return a toggled player."""
self.currentplayer nextselfplayers
def isvalidmoveself move:
Return True if move is valid, and False otherwise."""
row, col move.row, move.col
movewasnotplayed self.currentmovesrowcollabel
nowinner not self.haswinner
return nowinner and movewasnotplayed
def processmoveself move:
Process the current move and check if it's a win."""
row, col move.row, move.col
self.currentmovesrowcol move
for combo in self.winningcombos:
results setselfcurrentmovesnmlabel for n m in combo
iswin lenresults and not in results
if iswin:
self.haswinner True
self.winnercombo combo
break
def haswinnerself:
Return True if the game has a winner, and False otherwise."""
return self.haswinner
def istiedself:
Return True if the game is tied, and False otherwise."""
nowinner not self.haswinner
playedmoves
move.label for row in self.currentmoves for move in row
return nowinner and allplayedmoves
def resetgameself:
Reset the game state to play again."""
for row, rowcontent in enumerateselfcurrentmoves:
for col, in enumeraterowcontent:
rowcontentcol Moverow col
self.haswinner False
self.winnercombo
class TicTacToeBoardtkTk:
def initself game:
superinit
self.titleTicTacToe Game"
self.cells
self.game game
self.createmenu
self.createboarddisplay
self.createboardgrid
def createmenuself:
menubar tkMenumasterself
self.configmenumenubar
filemenu tkMenumastermenubar
filemenu.addcommandlabel"Play Again", commandself.resetboard
filemenu.addseparator
filemenu.addcommandlabel"Exit", commandquit
menubar.addcascadelabel"File", menufilemenu
def createboarddisplayself:
displayframe tkFramemasterself
displayframe.packfilltkX
self.display tkLabel
masterdisplayframe,
text"Ready?",
fontfont.Fontsize weight"bold"
self.display.pack
def createboardgridself:
gridframe tkFramemasterself
gridframe.pack
for row in rangeselfgame.boardsize:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
