Skip to content

Commit 7b54642

Browse files
committed
Update python_easy_chess_gui.py
* Use pathlib standard python module to handle directories, files and paths. * Save engine id name whenever a new opponent engine is defined. * Remove unused methods.
1 parent d87c8ba commit 7b54642

1 file changed

Lines changed: 24 additions & 28 deletions

File tree

python_easy_chess_gui.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
"""
23
python_easy_chess_gui.py
34
@@ -36,6 +37,7 @@
3637
import sys
3738
import subprocess
3839
import threading
40+
from pathlib import Path # Python 3.4 and up
3941
import queue
4042
import copy
4143
import time
@@ -53,7 +55,7 @@
5355

5456

5557
APP_NAME = 'Python Easy Chess GUI'
56-
APP_VERSION = 'v0.77'
58+
APP_VERSION = 'v0.78'
5759
BOX_TITLE = '{} {}'.format(APP_NAME, APP_VERSION)
5860

5961

@@ -310,10 +312,10 @@ def run(self):
310312
is_compile_to_exe = False
311313
if is_compile_to_exe:
312314
self.engine = chess.engine.SimpleEngine.popen_uci(
313-
self.engine_path,
315+
str(self.engine_path),
314316
creationflags=subprocess.CREATE_NO_WINDOW)
315317
else:
316-
self.engine = chess.engine.SimpleEngine.popen_uci(self.engine_path)
318+
self.engine = chess.engine.SimpleEngine.popen_uci(str(self.engine_path))
317319

318320
try:
319321
self.engine.configure({'Threads': self.threads})
@@ -420,6 +422,7 @@ def __init__(self, gui_book_file, computer_book_file, human_book_file,
420422
self.hash = memory_mb
421423
self.engine_path_and_name = None
422424
self.engine_file = None
425+
self.opp_eng_id_name = None
423426
self.adviser_engine = None
424427
self.adviser_hash = 128
425428
self.adviser_threads = 1
@@ -435,14 +438,6 @@ def __init__(self, gui_book_file, computer_book_file, human_book_file,
435438
self.engine_list = self.get_engines()
436439
self.menu_elem = None
437440

438-
def update_engine_selection(self, engine_filename):
439-
""" """
440-
self.engine_file = engine_filename
441-
engine_id_name = self.get_engine_id_name()
442-
self.update_labels_and_game_tags(human='Human',
443-
engine_id=engine_id_name)
444-
self.update_engine_list()
445-
446441
def get_time_mm_ss_ms(self, time_ms):
447442
""" Returns time in min:sec:millisec given time in millisec """
448443
s, ms = divmod(int(time_ms), 1000)
@@ -542,8 +537,9 @@ def clear_text_color(self):
542537
self.window.Element('book2_k').Update(text_color='black')
543538
self.window.Element('search_info_k').Update(text_color='black')
544539

545-
def update_labels_and_game_tags(self, human='Human', engine_id='engine id name'):
540+
def update_labels_and_game_tags(self, human='Human'):
546541
""" Update player names """
542+
engine_id = self.opp_eng_id_name
547543
if self.is_user_white:
548544
self.window.FindElement('_White_').Update(human)
549545
self.window.FindElement('_Black_').Update(engine_id)
@@ -1068,8 +1064,7 @@ def play_game(self, engine_id_name, board):
10681064
self.window.Enable()
10691065
w.Close()
10701066

1071-
self.update_labels_and_game_tags(human='Human',
1072-
engine_id=self.get_engine_id_name())
1067+
self.update_labels_and_game_tags(human='Human')
10731068
continue
10741069

10751070
if button == 'About':
@@ -1238,8 +1233,7 @@ def play_game(self, engine_id_name, board):
12381233
self.window.Enable()
12391234
w.Close()
12401235

1241-
self.update_labels_and_game_tags(human='Human',
1242-
engine_id=self.get_engine_id_name())
1236+
self.update_labels_and_game_tags(human='Human')
12431237
continue
12441238

12451239
# Allow user to change book settings when user is to move in Play mode
@@ -1731,18 +1725,22 @@ def save_game(self):
17311725

17321726
def get_engine_id_name(self):
17331727
""" Set the engine path and return id name """
1734-
engine_path_and_name = './Engines/' + self.engine_file
1735-
self.engine_path_and_name = engine_path_and_name
1736-
if engine_path_and_name is None:
1737-
logging.info('Failed to load engine')
1728+
cur_dir = Path.cwd()
1729+
eng_path = Path(cur_dir, 'Engines', self.engine_file)
1730+
self.engine_path_and_name = eng_path
1731+
logging.info('engine path: {}'.format(eng_path))
1732+
1733+
# Exit app if we fail to load the engine
1734+
if not eng_path.is_file():
1735+
logging.info('Quit app, failed to load engine.')
17381736
sys.exit(0)
17391737

17401738
# Start the engine and get its id name for update to GUI
1741-
engine = chess.engine.SimpleEngine.popen_uci(engine_path_and_name)
1742-
engine_id_name = engine.id['name']
1739+
engine = chess.engine.SimpleEngine.popen_uci(str(self.engine_path_and_name))
1740+
self.opp_eng_id_name = engine.id['name']
17431741
engine.quit()
17441742

1745-
return engine_id_name
1743+
return self.opp_eng_id_name
17461744

17471745
def get_engines(self):
17481746
""" Returns a list of engines located in Engines dir """
@@ -1922,7 +1920,7 @@ def main_loop(self):
19221920
# Initialize White and black boxes
19231921
while True:
19241922
button, value = self.window.Read(timeout=50)
1925-
self.update_labels_and_game_tags(human='Human', engine_id=engine_id_name)
1923+
self.update_labels_and_game_tags(human='Human')
19261924
break
19271925

19281926
while True:
@@ -2008,8 +2006,7 @@ def main_loop(self):
20082006
w.Close()
20092007

20102008
# Update the player box in main window
2011-
self.update_labels_and_game_tags(human='Human',
2012-
engine_id=self.get_engine_id_name())
2009+
self.update_labels_and_game_tags(human='Human')
20132010
continue
20142011

20152012
# Mode: Neutral, Set Adviser engine
@@ -2152,8 +2149,7 @@ def main_loop(self):
21522149
auto_size_buttons=False, location=(loc[0], loc[1]), icon='')
21532150
self.is_user_white = not self.is_user_white
21542151

2155-
self.update_labels_and_game_tags(human='Human',
2156-
engine_id=engine_id_name)
2152+
self.update_labels_and_game_tags(human='Human')
21572153
self.psg_board = copy.deepcopy(initial_board)
21582154
board = chess.Board()
21592155
self.window.Refresh()

0 commit comments

Comments
 (0)