This commit is contained in:
louiscklaw
2025-01-31 21:31:32 +08:00
parent c8209d847a
commit 0c89a0db81
145 changed files with 6826 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
import tkinter
import random
import time
masu = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
shirushi = 0
kachi = 0
FNT = ("Times New Roman", 60)
def masume():
cvs.delete("all")
for i in range(1, 3):
cvs.create_line(200*i, 0, 200*i, 600, fill="gray", width=8)
cvs.create_line(0, i*200, 600, i*200, fill="gray", width=8)
for y in range(3):
for x in range(3):
X = x * 200
Y = y * 200
if masu[y][x] == 1:
cvs.create_oval(X+20, Y+20, X+180, Y+180, outline="blue", width=12)
if masu[y][x] == 2:
cvs.create_line(X+20, Y+20, X+180, Y+180, fill="red", width=12)
cvs.create_line(X+180, Y+20, X+20, Y+180, fill="red", width=12)
if shirushi == 0:
cvs.create_text(300, 300, text="遊戲開始!", fill="navy", font=FNT)
cvs.update()
def click(e):
global shirushi
if shirushi == 9:
replay()
return
if shirushi==1 or shirushi==3 or shirushi==5 or shirushi==7:
return
mx = int(e.x/200)
my = int(e.y/200)
if mx>2: mx = 2
if my>2: my = 2
if masu[my][mx] == 0:
masu[my][mx] = 1
shirushi = shirushi + 1
masume()
time.sleep(0.5)
hantei()
syouhai()
if shirushi < 9:
computer()
masume()
time.sleep(0.5)
hantei()
syouhai()
def computer():
global shirushi
#有沒有連成一線的符號
for y in range(3):
for x in range(3):
if masu[y][x] == 0:
masu[y][x] = 2
hantei()
if kachi==2:
shirushi = shirushi + 1
return
masu[y][x] = 0
#阻止玩家連成一線
for y in range(3):
for x in range(3):
if masu[y][x] == 0:
masu[y][x] = 1
hantei()
if kachi==1:
masu[y][x] = 2
shirushi = shirushi + 1
return
masu[y][x] = 0
while True:
x = random.randint(0, 2)
y = random.randint(0, 2)
if masu[y][x] == 0:
masu[y][x] = 2
shirushi = shirushi + 1
break
def hantei():
global kachi
kachi = 0
for n in range(1, 3):
#判斷垂直方向是否連成一線
if masu[0][0]==n and masu[1][0]==n and masu[2][0]==n:
kachi = n
if masu[0][1]==n and masu[1][1]==n and masu[2][1]==n:
kachi = n
if masu[0][2]==n and masu[1][2]==n and masu[2][2]==n:
kachi = n
#判斷水平方向是否連成一線
if masu[0][0]==n and masu[0][1]==n and masu[0][2]==n:
kachi = n
if masu[1][0]==n and masu[1][1]==n and masu[1][2]==n:
kachi = n
if masu[2][0]==n and masu[2][1]==n and masu[2][2]==n:
kachi = n
#判斷傾斜方向是否連成一線
if masu[0][0]==n and masu[1][1]==n and masu[2][2]==n:
kachi = n
if masu[0][2]==n and masu[1][1]==n and masu[2][0]==n:
kachi = n
def syouhai():
global shirushi
if kachi == 1:
cvs.create_text(300, 300, text="玩家獲勝!", font=FNT, fill="cyan")
shirushi = 9
if kachi == 2:
cvs.create_text(300, 300, text="電腦\n獲勝!", font=FNT, fill="gold")
shirushi = 9
if kachi == 0 and shirushi == 9:
cvs.create_text(300, 300, text="平手", font=FNT, fill="lime")
def replay():
global shirushi
shirushi = 0
for y in range(3):
for x in range(3):
masu[y][x] = 0
masume()
root = tkinter.Tk()
root.title("井字遊戲")
root.resizable(False, False)
root.bind("<Button>", click)
cvs = tkinter.Canvas(width=600, height=600, bg="white")
cvs.pack()
masume()
root.mainloop()

View File

@@ -0,0 +1,110 @@
import tkinter
player1 = 1
player2 = 2
current_player = player1
shirushi = 0
masu = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
def masume():
global cvs
cvs.delete("all")
cvs.create_line(200, 0, 200, 600, fill="black", width=8)
cvs.create_line(400, 0, 400, 600, fill="black", width=8)
cvs.create_line(0, 200, 600, 200, fill="black", width=8)
cvs.create_line(0, 400, 600, 400, fill="black", width=8)
for y in range(3):
for x in range(3):
X = x * 200
Y = y * 200
if masu[y][x] == 1:
cvs.create_oval(X + 20, Y + 20, X + 180, Y + 180, outline="blue", width=12)
if masu[y][x] == 2:
cvs.create_line(X + 20, Y + 20, X + 180, Y + 180, fill="red", width=12)
cvs.create_line(X + 180, Y + 20, X + 20, Y + 180, fill="red", width=12)
cvs.update()
def click(e):
global current_player, shirushi
if shirushi == 9:
replay()
return
if shirushi == 1 or shirushi == 3 or shirushi == 5 or shirushi == 7:
return
mx = int(e.x / 200)
my = int(e.y / 200)
if mx > 2:
mx = 2
if my > 2:
my = 2
if masu[my][mx] == 0:
masu[my][mx] = current_player
winner = check_winner()
if winner != 0 or shirushi == 8:
shirushi = 9
else:
# Switch players
if current_player == player1:
current_player = player2
else:
current_player = player1
masume()
syouhai()
def check_winner():
# Check rows
for row in masu:
if row[0] == row[1] == row[2] != 0:
return row[0]
# Check columns
for col in range(3):
if masu[0][col] == masu[1][col] == masu[2][col] != 0:
return masu[0][col]
# Check diagonals
if masu[0][0] == masu[1][1] == masu[2][2] != 0:
return masu[0][0]
if masu[0][2] == masu[1][1] == masu[2][0] != 0:
return masu[0][2]
# Check if all cells are filled (tie)
if all(row.count(0) == 0 for row in masu):
return -1
return 0
def syouhai():
global shirushi
winner = check_winner()
if winner == player1:
cvs.create_text(300, 300, text="Player 1 wins!", font=("Arial", 36), fill="cyan")
shirushi = 9
elif winner == player2:
cvs.create_text(300, 300, text="Player 2 wins!", font=("Arial", 36), fill="gold")
shirushi = 9
elif winner == -1:
cvs.create_text(300, 300, text="Tie", font=("Arial", 36), fill="lime")
shirushi = 9
def replay():
global shirushi
shirushi = 0
for y in range(3):
for x in range(3):
masu[y][x] = 0
masume()
def User_UserMode():
root_UserUser = tkinter.Toplevel(root)
root_UserUser.title("sba tac tae toe user vs user")
root_UserUser.resizable(False, False)
root_UserUser.bind("<Button>", click)
cvs = tkinter.Canvas(width=600, height=600, bg="white")
cvs.pack()
masume()
root_UserUser.mainloop()