Created
December 31, 2022 03:13
-
-
Save dntf-studio/091e5de7ff82f4071e3318f2e47c9228 to your computer and use it in GitHub Desktop.
Tic-Tac-Toe with TKinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from tkinter import messagebox | |
from functools import partial | |
import random | |
import sys | |
screenHeight = 390 | |
screenWidth = 390 | |
class Application(tk.Frame): | |
def __init__(self,master = None): | |
super().__init__(master) | |
self.mainframe = tk.Frame(self.master) | |
self.mainframe.pack() | |
self.frame1 = tk.Frame(self.master) | |
self.frame1.grid(row=0,column=0,in_=self.mainframe,pady=50) | |
self.cells = [[tk.StringVar() for i in range(3)] for j in range(3)] | |
self.createButtonsWithGrid(cellSize=9,gridSize=3,fontSize=10) | |
def createButtonsWithGrid(self,cellSize,gridSize:int,fontSize=0,cellSpace=0): | |
for i in range(0,gridSize): | |
for j in range(0,gridSize): | |
button = tk.Button( | |
self.frame1, | |
relief="groove", | |
width=cellSize, | |
height=int(cellSize/2), | |
#text=text, | |
textvariable=self.cells[i][j], | |
font=("System",fontSize), | |
command=partial(self.clicked,(i,j)) | |
) | |
button.grid(column=j,row=i,padx=cellSpace,pady=cellSpace) | |
def clicked(self,p): | |
if (self.checkCell(p,"x") | |
or self.checkCell(p,"o")): | |
pass | |
else: | |
self.changeCell(p,"o") | |
if self.judgeGame(p,"o"): | |
ans = messagebox.askyesno("勝利","プレイヤーの勝利です。\nもう一度プレイしますか?") | |
if ans: | |
self.resetGame() | |
else: | |
sys.exit() | |
else: | |
enemyPos = self.setEnemy() | |
if enemyPos[0] < 0: | |
ans = messagebox.askyesno("引き分け","引き分けました。\nもう一度プレイしますか?") | |
if ans: | |
self.resetGame() | |
else: | |
sys.exit() | |
if self.judgeGame(enemyPos,"x"): | |
ans = messagebox.askyesno("敗北","あなたは敗北しました。\nもう一度プレイしますか?") | |
if ans: | |
self.resetGame() | |
else: | |
sys.exit() | |
def changeCell(self,p,msg): | |
print(p) | |
self.cells[p[0]][p[1]].set(msg) | |
def setEnemy(self): | |
if self.checkWholeBoard(): | |
return (-1,-1) | |
i = random.randint(0,len(self.cells)-1) | |
j = random.randint(0,len(self.cells)-1) | |
while (self.checkCell((i,j),"o") | |
or self.checkCell((i,j),"x")): | |
i = random.randint(0,len(self.cells)-1) | |
j = random.randint(0,len(self.cells)-1) | |
self.changeCell((i,j),"x") | |
return (i,j) | |
def judgeGame(self,p:tuple,c): | |
maxIndex = len(self.cells)-1 | |
if p[0] == maxIndex: | |
if p[1] == maxIndex: | |
if (self.checkCell((p[0],p[1]-2),c) | |
and self.checkCell((p[0],p[1]-1),c)): | |
return True | |
elif (self.checkCell((p[0]-2,p[1]),c) | |
and self.checkCell((p[0]-1,p[1]),c)): | |
return True | |
elif (self.checkCell((p[0]-2,p[1]-2),c) | |
and self.checkCell((p[0]-1,p[1]-1),c)): | |
return True | |
elif p[1] == 0: | |
if (self.checkCell((p[0]-2,p[1]),c) | |
and self.checkCell((p[0]-1,p[1]),c)): | |
return True | |
elif (self.checkCell((p[0],p[1]+2),c) | |
and self.checkCell((p[0],p[1]+1),c)): | |
return True | |
elif (self.checkCell((p[0]-2,p[1]+2),c) | |
and self.checkCell((p[0]-1,p[1]+1),c)): | |
return True | |
else: | |
if (self.checkCell((p[0],p[1]-1),c) | |
and self.checkCell((p[0],p[1]+1),c)): | |
return True | |
elif (self.checkCell((p[0]-1,p[1]),c) | |
and self.checkCell((p[0]-2,p[1]),c)): | |
return True | |
return False | |
elif p[0] == 0: | |
if p[1] == maxIndex: | |
if (self.checkCell((p[0],p[1]-2),c) | |
and self.checkCell((p[0],p[1]-1),c)): | |
return True | |
elif (self.checkCell((p[0]+2,p[1]),c) | |
and self.checkCell((p[0]+1,p[1]),c)): | |
return True | |
elif (self.checkCell((p[0]+2,p[1]-2),c) | |
and self.checkCell((p[0]+1,p[1]-1),c)): | |
return True | |
elif p[1] == 0: | |
if (self.checkCell((p[0]+2,p[1]),c) | |
and self.checkCell((p[0]+1,p[1]),c)): | |
return True | |
elif (self.checkCell((p[0],p[1]+2),c) | |
and self.checkCell((p[0],p[1]+1),c)): | |
return True | |
elif (self.checkCell((p[0]+2,p[1]+2),c) | |
and self.checkCell((p[0]+1,p[1]+1),c)): | |
return True | |
else: | |
if (self.checkCell((p[0],p[1]-1),c) | |
and self.checkCell((p[0],p[1]+1),c)): | |
return True | |
elif (self.checkCell((p[0]+1,p[1]),c) | |
and self.checkCell((p[0]+2,p[1]),c)): | |
return True | |
return False | |
else: | |
if p[1] == 0: | |
if (self.checkCell((p[0]-1,p[1]),c) | |
and self.checkCell((p[0]+1,p[1]),c)): | |
return True | |
elif (self.checkCell((p[0],p[1]+1),c) | |
and self.checkCell((p[0],p[1]+2),c)): | |
return True | |
elif p[1] == maxIndex: | |
if (self.checkCell((p[0]-1,p[1]),c) | |
and self.checkCell((p[0]+1,p[1]),c)): | |
return True | |
elif (self.checkCell((p[0],p[1]-1),c) | |
and self.checkCell((p[0],p[1]-2),c)): | |
return True | |
else: | |
if (self.checkCell((p[0]-1,p[1]),c) | |
and self.checkCell((p[0]+1,p[1]),c)): | |
return True | |
elif (self.checkCell((p[0],p[1]+1),c) | |
and self.checkCell((p[0],p[1]+1),c)): | |
return True | |
if (self.checkCell((p[0]-1,p[1]-1),c) | |
and self.checkCell((p[0]+1,p[1]+1),c)): | |
return True | |
elif (self.checkCell((p[0]+1,p[1]-1),c) | |
and self.checkCell((p[0]-1,p[1]+1),c)): | |
return True | |
return False | |
def checkCell(self,p,c): | |
return self.cells[p[0]][p[1]].get() == c | |
def checkWholeBoard(self): | |
for i in self.cells: | |
for j in i: | |
if (j.get() == ""): | |
return False | |
return True | |
def resetGame(self): | |
for i in self.cells: | |
for j in i: | |
j.set("") | |
if __name__ == "__main__": | |
root = tk.Tk() | |
root.title("○×ゲーム") | |
# Screen size | |
root.geometry(str(screenWidth)+"x"+str(screenHeight)) | |
app = Application(master = root) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment