-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaturf.py
209 lines (176 loc) · 8.04 KB
/
caturf.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import tkinter as tk
import chess
import subprocess
import threading
import queue
import time
class StockfishProcess:
def __init__(self, path):
print("Inisialisasi Stockfish...")
self.process = subprocess.Popen(
path,
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
bufsize=1
)
# Initialize engine with default parameters
self.send_command("uci")
self.send_command("setoption name Threads value 4")
self.send_command("setoption name Skill Level value 20")
self.send_command("setoption name Move Overhead value 10")
self.send_command("setoption name MultiPV value 1")
self.send_command(f"setoption name SyzygyPath value D:\\3-4-5")
self.send_command("isready")
self.wait_for_response("readyok")
def send_command(self, cmd):
print(f"Sending command: {cmd}") # Debugging output
self.process.stdin.write(f"{cmd}\n")
self.process.stdin.flush()
def get_best_move(self, fen, depth=20):
print(f"Mengambil best move untuk FEN: {fen} dengan kedalaman {depth}") # Debugging output
self.send_command(f"position fen {fen}")
self.send_command(f"go depth {depth}")
while True:
response = self.process.stdout.readline().strip()
print(f"Response dari Stockfish: {response}") # Debugging output
if response.startswith("bestmove"):
best_move = response.split()[1]
self.save_best_move_to_file(best_move) # Save best move to file
return best_move
def wait_for_response(self, expected):
print(f"Menunggu response untuk: {expected}") # Debugging output
while True:
response = self.process.stdout.readline().strip()
print(response) # Debugging output
if response == expected:
return
def set_position(self, fen):
print(f"Set posisi ke FEN: {fen}") # Debugging output
self.send_command(f"position fen {fen}")
def make_moves_from_position(self, moves):
moves_str = " ".join(moves)
print(f"Memainkan langkah: {moves_str}") # Debugging output
self.send_command(f"position fen {self.current_fen} moves {moves_str}")
def quit(self):
print("Menghentikan Stockfish...") # Debugging output
self.send_command("quit")
self.process.terminate()
def save_best_move_to_file(self, best_move):
print(f"Menyimpan best move ke file: {best_move}") # Debugging output
with open("best.txt", "a") as file: # Open file in append mode
file.write(best_move + "\n") # Write best move followed by a newline
def save_fen_to_file(fen):
"""
Menyimpan FEN yang diinput user ke dalam file fen.txt
"""
print(f"Menyimpan FEN ke file: {fen}") # Debugging output
with open("fen.txt", "a") as file: # Open file in append mode
file.write(fen + "\n") # Write FEN followed by a newline
# Inisialisasi Stockfish dan Board
stockfish_path = "C:\\Users\\LENOVO\\Documents\\Developing\\python\\stockfish\\stockfish-windows-x86-64-avx2.exe"
print("Memulai proses Stockfish...")
stockfish = StockfishProcess(stockfish_path)
board = chess.Board()
def start_from_fen():
try:
fen = fen_entry.get().strip()
print(f"Memulai dari FEN: {fen}") # Debugging output
# Validasi FEN sebelum menyimpan
board.set_fen(fen) # This will raise an exception if FEN is invalid
# Simpan FEN yang valid ke file
save_fen_to_file(fen)
stockfish.set_position(fen)
update_fen_display()
if board.turn == chess.WHITE:
white_best_move = stockfish.get_best_move(fen)
if white_best_move:
board.push_uci(white_best_move)
stockfish.set_position(board.fen())
white_move_label.config(text=f"Langkah Pertama: {white_best_move}")
update_fen_display()
black_move_label.config(text="Masukkan Langkah Selanjutnya")
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, "Silakan masukkan Langkah Selanjutnya.\n")
else:
black_best_move = stockfish.get_best_move(fen)
if black_best_move:
board.push_uci(black_best_move)
stockfish.set_position(board.fen())
black_move_label.config(text=f"Langkah Black: {black_best_move}")
update_fen_display()
white_move_label.config(text="Giliran Putih, silakan masukkan langkah Putih.")
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, "Silakan masukkan langkah Putih.\n")
except Exception as e:
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, f"Error: {str(e)}\n")
print(f"Error: {str(e)}") # Debugging output
def add_white_move(event=None):
try:
white_move_san = white_move_entry.get().strip()
print(f"Menambahkan langkah Putih: {white_move_san}") # Debugging output
white_move = board.parse_san(white_move_san)
if not board.is_legal(white_move):
raise ValueError(f"Langkah '{white_move_san}' tidak sah di papan ini.")
board.push(white_move)
stockfish.set_position(board.fen())
black_best_move = stockfish.get_best_move(board.fen())
if black_best_move:
board.push_uci(black_best_move)
stockfish.set_position(board.fen())
black_move_label.config(text=f"Langkah Black: {black_best_move}")
update_fen_display()
white_move_entry.delete(0, tk.END)
except ValueError as e:
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, f"Error: {str(e)}\n")
print(f"ValueError: {str(e)}") # Debugging output
except Exception as e:
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, f"Unexpected error: {str(e)}\n")
print(f"Unexpected error: {str(e)}") # Debugging output
def update_fen_display():
current_fen = board.fen()
print(f"Memperbarui FEN: {current_fen}") # Debugging output
fen_display.config(text=f"FEN saat ini: {current_fen}")
def on_closing():
print("Menutup aplikasi...") # Debugging output
stockfish.quit()
root.destroy()
# Membuat window utama GUI
root = tk.Tk()
root.title("Chess FEN Analyzer")
root.geometry("500x500")
root.protocol("WM_DELETE_WINDOW", on_closing)
# Menambahkan opsi untuk menampilkan window di atas
root.attributes('-topmost', True)
# Input untuk posisi awal FEN
fen_label = tk.Label(root, text="Masukkan FEN:")
fen_label.pack(pady=5)
fen_entry = tk.Entry(root, width=50)
fen_entry.pack(pady=5)
# Tombol untuk memulai dari FEN yang dimasukkan
start_button = tk.Button(root, text="Mulai dari FEN", command=start_from_fen)
start_button.pack(pady=10)
# Display untuk FEN saat ini
fen_display = tk.Label(root, text="FEN saat ini: ")
fen_display.pack(pady=5)
# Entry untuk langkah Putih
white_move_label = tk.Label(root, text="Masukkan langkah Putih (SAN):")
white_move_label.pack(pady=5)
white_move_entry = tk.Entry(root, width=10)
white_move_entry.pack(pady=5)
# Bind tombol Enter ke fungsi add_white_move
white_move_entry.bind("<Return>", add_white_move)
# Tombol untuk menambahkan langkah Putih dan memicu langkah Hitam otomatis
white_move_button = tk.Button(root, text="Tambah Langkah Putih", command=add_white_move)
white_move_button.pack(pady=10)
# Display untuk langkah Hitam
black_move_label = tk.Label(root, text="Best Move: ")
black_move_label.pack(pady=5)
# Text widget untuk output hasil analisis
output_text = tk.Text(root, height=5, width=50)
output_text.pack(pady=10)
# Menjalankan loop utama tkinter
root.mainloop()