forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (63 loc) · 2.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
This is the main file of the Gomoku game.
'''
import tkinter as tk
from tkinter import messagebox
from game import Game
class GomokuApp:
def __init__(self, master):
self.master = master
self.master.title("Gomoku")
self.game = Game()
self.canvas = tk.Canvas(self.master, width=500, height=500, bg="white")
self.canvas.pack()
self.canvas.bind("<Button-1>", self.on_click)
self.draw_board()
self.restart_button = tk.Button(self.master, text="Restart", command=self.restart)
self.restart_button.pack()
self.status_bar = tk.Label(self.master, text="Black's Turn", bd=1, relief=tk.SUNKEN, anchor=tk.W)
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
def draw_board(self):
self.canvas.delete("all")
cell_width = 500 / 15
cell_height = 500 / 15
for row in range(15):
self.canvas.create_line(0, row * cell_height, 500, row * cell_height)
for col in range(15):
self.canvas.create_line(col * cell_width, 0, col * cell_width, 500)
def on_click(self, event):
cell_width = 500 / 15
cell_height = 500 / 15
x = int(event.x // cell_width)
y = int(event.y // cell_height)
if self.game.make_move(x, y):
self.draw_piece(x, y)
self.update_status_bar()
if self.game.check_winner(x, y):
self.show_winner()
def draw_piece(self, x, y):
cell_width = 500 / 15
cell_height = 500 / 15
oval_coords = (x * cell_width, y * cell_height, (x + 1) * cell_width, (y + 1) * cell_height)
color = "black" if self.game.current_player == 1 else "white"
self.canvas.create_oval(*oval_coords, fill=color)
def show_winner(self):
winner = "Black" if self.game.current_player == 1 else "White"
messagebox.showinfo("Game Over", f"{winner} wins!")
self.canvas.unbind("<Button-1>")
self.restart_button.pack_forget()
self.canvas.bind("<Button-1>", self.restart)
def restart(self):
self.game.reset()
self.draw_board()
self.canvas.unbind("<Button-1>")
self.canvas.bind("<Button-1>", self.on_click)
self.restart_button.pack()
self.update_status_bar()
def update_status_bar(self):
player = "Black" if self.game.current_player == 1 else "White"
self.status_bar.config(text=f"{player}'s Turn")
if __name__ == "__main__":
root = tk.Tk()
app = GomokuApp(root)
root.mainloop()