Files
004_comission/jktjoeaj703/task1/_from_client/_src/user vs user final.py
louiscklaw 0c89a0db81 update,
2025-01-31 21:31:32 +08:00

111 lines
3.0 KiB
Python

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()