Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- import random
- class TKGUI:
- def __init__(self):
- self.root= Tk()
- self.quit = False
- self.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
- self.score = 0
- self.compsscore = 0
- def reset(self):
- win, team = self.win()
- if win:
- if team == 1:
- self.score = self.score+1
- self.log("You Won!")
- elif team == 2:
- self.compsscore = self.compsscore+1
- self.log("Computer Won!")
- self.userscore.configure(text=("Your Score: %s" %self.score))
- self.compscore.configure(text=("Comp Score: %s" %self.compsscore))
- self.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
- self.p0.configure(text="-")
- self.p1.configure(text="-")
- self.p2.configure(text="-")
- self.p3.configure(text="-")
- self.p4.configure(text="-")
- self.p5.configure(text="-")
- self.p6.configure(text="-")
- self.p7.configure(text="-")
- self.p8.configure(text="-")
- def win(self):
- if self.board[0][0] == self.board[0][1] and self.board[0][0] == self.board[0][2] and self.board[0][0] != 0: #top
- return True, self.board[0][0]
- elif self.board[1][0] == self.board[1][1] and self.board[1][0] == self.board[1][2] and self.board[1][0] != 0: #middle
- return True, self.board[1][0]
- elif self.board[2][0] == self.board[2][1] and self.board[2][0] == self.board[2][2] and self.board[2][0] != 0: #down
- return True, self.board[2][0]
- elif self.board[0][0] == self.board[1][0] and self.board[0][0] == self.board[2][0] and self.board[0][0] != 0: #left down
- return True,self.board[0][0]
- elif self.board[0][1] == self.board[1][1] and self.board[0][1] == self.board[2][1] and self.board[0][1] != 0: #middle down
- return True, self.board[0][1]
- elif self.board[0][2] == self.board[1][2] and self.board[0][2] == self.board[2][2] and self.board[0][2] != 0: #right down
- return True, self.board[0][2]
- elif self.board[0][0] == self.board[1][1] and self.board[0][0] == self.board[2][2] and self.board[0][0] != 0: #diaganal left
- return True, self.board[0][0]
- elif self.board[0][2] == self.board[1][1] and self.board[0][2] == self.board[2][0] and self.board[0][2] != 0: #diagonal right
- return True, self.board[0][2]
- else:
- return False, 0
- def check_moves(self):
- st = ""
- for i in range(len(self.board)):
- st += str("%s%s%s" %(self.board[i][0], self.board[i][1],self.board[i][2]))
- if not "0" in st:
- self.log("No Moves Remaining")
- return False
- else:
- return True
- def computer_turn(self):
- x = random.randint(0, 8)
- if not self.check_moves():
- self.reset()
- else:
- print(x)
- if x == 0:
- if self.board[0][0] != 1 and self.board[0][0] != 2:
- self.p0.configure(text="O")
- self.board[0][0] = 2
- else:
- self.computer_turn()
- elif x == 1:
- if self.board[0][1] != 1 and self.board[0][1] != 2:
- self.p1.configure(text="O")
- self.board[0][1] = 2
- else:
- self.computer_turn()
- elif x == 2:
- if self.board[0][2] != 1 and self.board[0][1] != 2:
- self.p2.configure(text="O")
- self.board[0][2] = 2
- else:
- self.computer_turn()
- elif x == 3:
- if self.board[1][0] != 1 and self.board[1][0] != 2:
- self.p3.configure(text="O")
- self.board[1][0] = 2
- else:
- self.computer_turn()
- elif x == 4:
- if self.board[1][1] != 1 and self.board[1][1] != 2:
- self.p4.configure(text="O")
- self.board[1][1] = 2
- else:
- self.computer_turn()
- elif x == 5:
- if self.board[1][2] != 1 and self.board[1][2] != 2:
- self.p5.configure(text="O")
- self.board[1][2] = 2
- else:
- self.computer_turn()
- elif x == 6:
- if self.board[2][0] != 1 and self.board[2][0] != 2:
- self.p6.configure(text="O")
- self.board[2][0] = 2
- else:
- self.computer_turn()
- elif x == 7:
- if self.board[2][1] != 1 and self.board[2][1] != 2:
- self.p7.configure(text="O")
- self.board[2][1] = 2
- else:
- self.computer_turn()
- elif x == 8:
- if self.board[2][2] != 1 and self.board[2][2] != 2:
- self.p8.configure(text="O")
- self.board[2][2] = 2
- else:
- self.computer_turn()
- win, team = self.win()
- if win:
- print("%s won" %team)
- self.reset()
- def log(self, message):
- self.note.configure(text=message)
- def click(self, pos):
- if not self.check_moves():
- self.reset()
- else:
- print(pos)
- valid = False
- if pos == 0:
- if self.board[0][0] != 1 and self.board[0][0] != 2:
- self.p0.configure(text="X")
- self.board[0][0] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 1:
- if self.board[0][1] != 1 and self.board[0][1] != 2:
- self.p1.configure(text="X")
- self.board[0][1] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 2:
- if self.board[0][2] != 1 and self.board[0][2] != 2:
- self.p2.configure(text="X")
- self.board[0][2] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 3:
- if self.board[1][0] != 1 and self.board[1][0] != 2:
- self.p3.configure(text="X")
- self.board[1][0] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 4:
- if self.board[1][1] != 1 and self.board[1][1] != 2:
- self.p4.configure(text="X")
- self.board[1][1] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 5:
- if self.board[1][2] != 1 and self.board[1][2] != 2:
- self.p5.configure(text="X")
- self.board[1][2] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 6:
- if self.board[2][0] != 1 and self.board[2][0] != 2:
- self.p6.configure(text="X")
- self.board[2][0] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 7:
- if self.board[2][1] != 1 and self.board[2][1] != 2:
- self.p7.configure(text="X")
- self.board[2][1] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- elif pos == 8:
- if self.board[2][2] != 1 and self.board[2][2] != 2:
- self.p8.configure(text="X")
- self.board[2][2] = 1
- valid = True
- else:
- self.log("That is not a valid move")
- if valid:
- win, team = self.win()
- if win:
- print("%s won" %team)
- self.reset()
- self.computer_turn()
- def run(self): ##main part of the application
- self.root.configure(bg="white") #sets the background to white rather than default gray.
- self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##changes the X (close) Button to run a function instead.
- try:
- self.root.iconbitmap("fav.ico") ##sets the application Icon
- except:
- pass
- self.root.title("MyApp")
- self.root.geometry("800x600")
- labelfont = ('times', 20, 'bold')
- overwidth = 16
- sframe = Frame(self.root)
- sframe.pack(fill=X)
- self.userscore = Label(sframe, text="Your Score: 0")
- self.userscore.pack(side=LEFT, fill=X, expand=1)
- self.compscore = Label(sframe, text="Comp Score: 0")
- self.compscore.pack(side=RIGHT, fill=X, expand=1)
- l1 = Frame(self.root)
- l1.pack(side=TOP, fill=BOTH, expand=1)
- self.p0 = Button(l1, command=lambda :self.click(0), text="-", font=labelfont, width=overwidth)
- self.p0.pack(side=LEFT, fill=BOTH)
- packer = Frame(l1, width=10, bg="black")
- packer.pack(side=LEFT, fill=Y)
- self.p1 = Button(l1, command=lambda :self.click(1), text="-", font=labelfont, width=overwidth)
- self.p1.pack(side=LEFT, fill=BOTH)
- packer = Frame(l1, width=10, bg="black")
- packer.pack(side=LEFT, fill=Y)
- self.p2 = Button(l1, command=lambda :self.click(2), text="-", font=labelfont, width=overwidth)
- self.p2.pack(side=LEFT, fill=BOTH)
- packer = Frame(self.root, height=10, bg="black")
- packer.pack(side=TOP, fill=X)
- l2 = Frame(self.root)
- l2.pack(side=TOP, fill=BOTH, expand=1)
- self.p3 = Button(l2, command=lambda :self.click(3), text="-", font=labelfont, width=overwidth)
- self.p3.pack(side=LEFT, fill=BOTH)
- packer = Frame(l2, width=10, bg="black")
- packer.pack(side=LEFT, fill=Y)
- self.p4 = Button(l2, command=lambda :self.click(4), text="-", font=labelfont, width=overwidth)
- self.p4.pack(side=LEFT, fill=BOTH)
- packer = Frame(l2, width=10, bg="black")
- packer.pack(side=LEFT, fill=Y)
- self.p5 = Button(l2, command=lambda :self.click(5), text="-", font=labelfont, width=overwidth)
- self.p5.pack(side=LEFT, fill=BOTH)
- packer = Frame(self.root, height=10, bg="black")
- packer.pack(side=TOP, fill=X)
- l3 = Frame(self.root)
- l3.pack(side=TOP, fill=BOTH, expand=1)
- self.p6 = Button(l3, command=lambda :self.click(6), text="-", font=labelfont, width=overwidth)
- self.p6.pack(side=LEFT, fill=BOTH)
- packer = Frame(l3, width=10, bg="black")
- packer.pack(side=LEFT, fill=Y)
- self.p7 = Button(l3, command=lambda :self.click(7), text="-", font=labelfont, width=overwidth)
- self.p7.pack(side=LEFT, fill=BOTH)
- packer = Frame(l3, width=10, bg="black")
- packer.pack(side=LEFT, fill=Y)
- self.p8 = Button(l3, command=lambda :self.click(8), text="-", font=labelfont, width=overwidth)
- self.p8.pack(side=LEFT, fill=BOTH)
- self.note = Label(self.root, text="")
- self.note.pack(fill=X)
- while not self.quit:
- self.root.update_idletasks()
- self.root.update()
- def quitting(self):
- self.quit = True
- if __name__ == "__main__":
- app = TKGUI()
- try:
- app.run()
- except KeyboardInterrupt:
- app.quitting()
- except:
- raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement