Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have an issue with this code it doesn't have error but after I run it it will be hangs and turn off .. this

I have an issue with this code it doesn't have error but after I run it it will be hangs and turn off ..

this code for tic tac game (X O) and make it for 2 types for game with pc or with player 2

pls help me to run code fine without errors ..

Note : stage of code is : 1- menue 2- play with PC with two stage of Hard & easy 3- play with another player

I want to modify it and stage of hard level of play with PC

Code :

from tkinter import * import random player = 1 gt=0 gg=0 retry=0 Win = 1 Running = 0 Game = Running Mark = 'X' board={1:" ",2:" ",3:" ",4:" ",5:" ",6:" ",7:" ",8:" ",9:" "} def CheckPosition(x): if (board[x] == ' '): return True else: return False def CheckWin(): global Game # Horizontal winning condition if (board[1] == board[2] and board[2] == board[3] and board[1] != ' '): Game = Win elif (board[4] == board[5] and board[5] == board[6] and board[4] != ' '): Game = Win elif (board[7] == board[8] and board[8] == board[9] and board[7] != ' '): Game = Win # Vertical Winning Condition elif (board[1] == board[4] and board[4] == board[7] and board[1] != ' '): Game = Win elif (board[2] == board[5] and board[5] == board[8] and board[2] != ' '): Game = Win elif (board[3] == board[6] and board[6] == board[9] and board[3] != ' '): Game = Win # Diagonal Winning Condition elif (board[1] == board[5] and board[5] == board[9] and board[5] != ' '): Game = Win elif (board[3] == board[5] and board[5] == board[7] and board[5] != ' '): Game = Win # Match Tie or Draw Condition elif (board[1] != ' ' and board[2] != ' ' and board[3] != ' ' and board[4] != ' ' and board[5] != ' ' and board[ 6] != ' ' and board[7] != ' ' and board[8] != ' ' and board[9] != ' '): Game = retry else: Game = Running # gg (0)=with pc , gg(1)=with player2 # gt (0)= easy , gt(1)=hard # player(1)= player first, player(2)=pc first # postion= button postion def gameplay(choice): global gg,gt,player,p1,p2 if gg==0: p1="User" p2="PC" elif gg==1: p1="Player 1 " p2="Player 2 " while (Game == Running): if (player % 2 != 0 and gg==0): turnlabel.config(text=f'{p1} chance') Mark = 'X' if(p1=="PC"and gt==0): choice = random.randrange(1, 9) # elif(p1=="PC"and gt==1): # pass if (CheckPosition(choice)): board[choice] = Mark player += 1 CheckWin() elif (player % 2 == 0 and gg==0): turnlabel.config(text=f'{p2} chance') Mark = 'O' if (p1 == "PC" and gt == 0): choice = random.randrange(1, 9) # elif(p1=="PC"and gt==1): # pass if (CheckPosition(choice)): board[choice] = Mark player += 1 CheckWin() if (Game == retry): pass elif (Game == Win): player -= 1 if (player % 2 != 0): print(f'{p1} Won') turnlabel.config(text=f'{p1} Won') else: print(f'{p2} Won' ) turnlabel.config(text=f'{p2} Won') def raise_frame(frame): frame.tkraise() root = Tk() f1 = Frame(root) f2 = Frame(root) f3 = Frame(root) f4 = Frame(root) for frame in (f1, f2, f3, f4): frame.grid(row=0, column=0, sticky='news') root.geometry("350x350") root.title("X O Game") #frame 1 welcome_label = Label(f1, text = "Welcome to X O Game", font=20,padx=10,pady=10).grid(row=0,column=0,columnspan=4,) playlabel = Label(f1, text = "Play with : ", font=20,padx=10,pady=10).grid(row=1,column=0,rowspan=2) PCg=Button(f1,text="PC",command=lambda gg=0 :raise_frame(f2)).grid(row=1,column=1,padx=5,pady=5,ipadx=10,ipady=10) Pl2g=Button(f1,text="Player 2",command=lambda gg=1 :[raise_frame(f4)]).grid(row=2,column=1,padx=5,pady=5,ipadx=10,ipady=10) exbut=Button(f1,text="exit",command=lambda: exit()).grid(row=3,column=1,ipadx=5,ipady=5,sticky="nsew") # frame 2 xolabel1 = Label(f2, text = "X O Game", font=30,padx=10,pady=10).grid(row=0,column=0,columnspan=4,) fturnlabel = Label(f2, text = "Who want to play first ? ", font=20,padx=10,pady=10).grid(row=1,column=0,columnspan=2) PCtbut=Button(f2,text="PC",command=lambda player =2:raise_frame(f3)).grid(row=2,column=0,padx=5,pady=5,ipadx=10,ipady=10) YOUtbut=Button(f2,text="You",command=lambda player=1 :raise_frame(f3)).grid(row=2,column=1,padx=5,pady=5,ipadx=10,ipady=10) backbut1=Button(f2,text="back to menue",command=lambda:raise_frame(f1)).grid(row=3,column=0,ipadx=5,ipady=5,columnspan=2) # FRAME 3 xolabel2 = Label(f3, text = "X O Game", font=30,padx=10,pady=10).grid(row=0,column=0,columnspan=4,) typelabel = Label(f3, text = "Choose game type ", font=20,padx=10,pady=10).grid(row=1,column=0,columnspan=2) Hbut=Button(f3,text="Hard",command=lambda gt=1:raise_frame(f4)).grid(row=2,column=0,padx=5,pady=5,ipadx=10,ipady=10) #gt means game type Ebut=Button(f3,text="Easy",command=lambda gt=0 :raise_frame(f4)).grid(row=2,column=1,padx=5,pady=5,ipadx=10,ipady=10) backbut2=Button(f3,text="back to menue",command=lambda:raise_frame(f1)).grid(row=3,column=0,ipadx=5,ipady=5,columnspan=2) # frame 4 toplabel = Label(f4, text = "X O Game", font=20,padx=10,pady=10).grid(row=0,column=0,columnspan=3) turnlabel = Label(f4, text=" " ,font=16) turnlabel.grid(row=1,column=0,columnspan=2) button1=Button(f4,text=board[1],command=lambda:gameplay(1)).grid(row=2,column=0,padx=5,pady=5,ipadx=10,ipady=10) button2=Button(f4,text=board[2],command=lambda:gameplay(2)).grid(row=2,column=1,padx=5,pady=5,ipadx=10,ipady=10) button3=Button(f4,text=board[3],command=lambda:gameplay(3)).grid(row=2,column=2,padx=5,pady=5,ipadx=10,ipady=10) button4=Button(f4,text=board[4],command=lambda:gameplay(4)).grid(row=3,column=0,padx=5,pady=5,ipadx=10,ipady=10) button5=Button(f4,text=board[5],command=lambda:gameplay(5)).grid(row=3,column=1,padx=5,pady=5,ipadx=10,ipady=10) button6=Button(f4,text=board[6],command=lambda:gameplay(6)).grid(row=3,column=2,padx=5,pady=5,ipadx=10,ipady=10) button7=Button(f4,text=board[7],command=lambda:gameplay(7)).grid(row=4,column=0,padx=5,pady=5,ipadx=10,ipady=10) button8=Button(f4,text=board[8],command=lambda:gameplay(8)).grid(row=4,column=1,padx=5,pady=5,ipadx=10,ipady=10) button9=Button(f4,text=board[9],command=lambda:gameplay(9)).grid(row=4,column=2,padx=5,pady=5,ipadx=10,ipady=10) buttonretry=Button(f4,text="Retry").grid(row=5,column=0,padx=5,pady=5,ipadx=10,ipady=10,columnspan=3) backbut=Button(f4,text="back to menue",command=lambda:raise_frame(f1)).grid(row=6,column=0,ipadx=5,ipady=5,columnspan=3) raise_frame(f1) root.mainloop()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions