-
Notifications
You must be signed in to change notification settings - Fork 0
/
regui.py
110 lines (93 loc) · 3.44 KB
/
regui.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import tkinter as tk
from tkinter import messagebox
import chess
import numpy as np
# Variable to keep track of the last clicked square
last_clicked_square = None
last_square_was_toggled = False
def how_the_knight_move(square1):
distances = np.zeros((8, 8))
for square2 in chess.SQUARES:
distances[
7 - chess.square_rank(square2), chess.square_file(square2)
] = chess.square_knight_distance(square1, square2)
return distances
def color_chessboard(canvas, distances):
square_size = 400 // 8
max_distance = np.max(distances)
for row in range(8):
for col in range(8):
distance = distances[row, col]
color = calculate_color(distance, max_distance)
canvas.create_rectangle(
col * square_size,
row * square_size,
(col + 1) * square_size,
(row + 1) * square_size,
fill=color,
)
canvas.create_text(
col * square_size + square_size // 2,
row * square_size + square_size // 2,
text=str(int(distance)),
fill="black" if color == "white" else "black",
font=("Arial", 12),
)
def calculate_color(distance, max_distance):
# Calculate the color based on distance
normalized_distance = min(distance / max_distance, 1.0)
r = int(normalized_distance * 255)
color = "#{:02X}{:02X}{:02X}".format(r, r, 255)
return color
def on_square_click(event):
global last_clicked_square, last_square_was_toggled
col = event.x // square_size
row = 7 - (event.y // square_size)
square = chess.square(col, row)
if 0 <= col < 8 and 0 <= row < 8:
if square == last_clicked_square and last_square_was_toggled:
# If the same square is clicked again and it was toggled, reset to black and white
for widget in canvas.find_all():
canvas.delete(widget)
for row in range(8):
for col in range(8):
color = "white" if (row + col) % 2 == 0 else "black"
canvas.create_rectangle(
col * square_size,
row * square_size,
(col + 1) * square_size,
(row + 1) * square_size,
fill=color,
)
last_square_was_toggled = False
else:
distances_from_square = how_the_knight_move(square)
color_chessboard(canvas, distances_from_square)
last_clicked_square = square
last_square_was_toggled = True
else:
messagebox.showinfo(
"Invalid Square", "Please click inside the chessboard."
)
# Create a Tkinter window
root = tk.Tk()
root.title("Chess Knight's Distance")
# Create a canvas for the chessboard
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()
# Draw the initial chessboard (start with black and white squares)
square_size = 400 // 8
for row in range(8):
for col in range(8):
color = "white" if (row + col) % 2 == 0 else "black"
canvas.create_rectangle(
col * square_size,
row * square_size,
(col + 1) * square_size,
(row + 1) * square_size,
fill=color,
)
# Bind the click event to the canvas
canvas.bind("<Button-1>", on_square_click)
# Start the Tkinter main loop
root.mainloop()