Question: This program plays the classic tile matching game. The object of the game is to find all the matching tiles by turning over two tiles
This program plays the classic tile matching game. The object of the game is to find all the matching tiles by turning over two tiles at a time. If the colors match, the tiles are removed. If not, then the tiles are replaced and two more tiles are turned over. The game is over once all the matching colors have been found.
This implementation of the game allows the player to select the number of rows and columns of tiles. Unfortunately, this sometimes causes problems. The game currently runs without issue for the default number of rows and columns, however if the player chooses an odd number of rows and an odd number of columns, the game will crash.
Your task is to identify the cause of the crash and modify the game to both:
Prevent the game from crashing regardless of what values for rows and columns are entered you may assume that both values will always be greater than
Allow the game to be played successfully regardless of what values for rows and columns are selected.
The number of rows and columns may be selected by starting the game and selecting File Options
import time
from tkinter import
from random import randint
class ButtonWrapper:
def initself id row col c:
self.ID id
self.ROW row
self.COL col
self.COLOR c
self.BUTTONOBJ None
def checkmatch:
global buttons
clicked
for b in buttons.values:
if bBUTTONOBJrelief "sunken":
clicked.appendb
if lenclicked:
if clickedBUTTONOBJbg clickedBUTTONOBJbg:
clickedBUTTONOBJ.configurefg'black', bg'black', relief'raised'
clickedBUTTONOBJ.configurefg'black', bg'black', relief'raised'
else:
clickedBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
clickedBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
def buttonpushedpushedid:
global buttons
buttonspushedidBUTTONOBJ.configurebgbuttonspushedidCOLOR, relief"sunken"
buttonspushedidBUTTONOBJ.after checkmatch
def closeoptionstop:
top.destroy
def saveoptionsr c:
global rows, cols
rows r
cols c
resetgame
def options:
top Toplevelroot
top.geometryx
rowval StringVar
colval StringVar
row Entrytop width textvariablerowval
row.pack
col Entrytop width textvariablecolval
col.pack
save Buttontop text"Save", commandlambda: saveoptionsintrowval.get intcolval.get
close Buttontop text"Close", commandlambda: closeoptionstop
save.packpady sideTOP
close.packpady sideTOP
def resetgame:
global buttons
for b in buttons.values:
bBUTTONOBJ.destroy
buttons # Clear the button dictionary
for i in rangerows:
for j in rangecols:
id i cols j # Calculate the button id
b ButtonWrapperidstrid rowi colj # Create the button meta data object
bBUTTONOBJ Buttonroot text commandlambda bidbID: buttonpushedbid height width
buttonsbID b # Save the button meta data object in the dictionary
ids listrangerows cols # Make a list of all button IDs
while lenids: # While there are still IDs in the list
a idsrandint lenids # Pick a random button ID
ids.removea # Remove it from the list
b idsrandint lenids # Pick a second random button ID
ids.removeb # Remove it from the list as well
color colorsrandint lencolors # Pick a random color
buttonsstraCOLOR color # Set both buttons to the same color
buttonsstrbCOLOR color
for b in buttons.values:
bBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
bBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
f in buttons.values:
bBUTTONOBJ.gridrowbROW, columnbCOL
rows
cols
buttons
root Tk
menubar Menuroot # Creating Menubar
colors red 'green', 'blue', 'cyan', 'yellow', 'magenta'
file Menumenubar tearoff
menubar.addcascadelabel'File', menufile
file.addcommandlabel'New Game', commandresetgame
file.addcommandlabel'Options', commandoptions
file.addseparator
file.addcommandlabel'Exit', commandroot.destroy
resetgame
root.gridrowconfigure minsize
root.configmenumenubar
root.mainloop
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
