Skip to content

Commit

Permalink
Refactor Timer initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmosca committed Oct 12, 2019
1 parent 2a9f775 commit af00357
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions python_easy_chess_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,25 @@ def set_depth_limit(self):
logging.exception('Failed to get user depth.')

self.max_depth = min(MAX_DEPTH, max(MIN_DEPTH, user_depth))

def define_timer(self, window, name='human'):
"""
Returns Timer object for either human or engine.
"""
if name == 'human':
timer = Timer(self.human_tc_type, self.human_base_time_ms,
self.human_inc_time_ms, self.human_period_moves)
else:
timer = Timer(self.engine_tc_type, self.engine_base_time_ms,
self.engine_inc_time_ms, self.engine_period_moves)

elapse_str = self.get_time_h_mm_ss(timer.base)
is_white_base = self.is_user_white and name == 'human' or \
not self.is_user_white and name != 'human'
window.Element('w_base_time_k' if is_white_base else 'b_base_time_k').Update(
elapse_str)

return timer

def play_game(self, window, engine_id_name, board):
"""
Expand Down Expand Up @@ -1633,31 +1652,8 @@ def play_game(self, window, engine_id_name, board):
is_hide_search_info = True

# Init timer
if self.is_user_white:
human_timer = Timer(self.human_tc_type, self.human_base_time_ms,
self.human_inc_time_ms, self.human_period_moves)
elapse_str = self.get_time_h_mm_ss(human_timer.base)
window.Element('w_base_time_k').Update(elapse_str)

engine_timer = Timer(self.engine_tc_type,
self.engine_base_time_ms,
self.engine_inc_time_ms,
self.engine_period_moves)
elapse_str = self.get_time_h_mm_ss(engine_timer.base)
window.Element('b_base_time_k').Update(elapse_str)
else:
engine_timer = Timer(self.engine_tc_type,
self.engine_base_time_ms,
self.engine_inc_time_ms,
self.engine_period_moves)
elapse_str = self.get_time_h_mm_ss(engine_timer.base)
window.Element('w_base_time_k').Update(elapse_str)

human_timer = Timer(self.human_tc_type, self.human_base_time_ms,
self.human_inc_time_ms,
self.human_period_moves)
elapse_str = self.get_time_h_mm_ss(human_timer.base)
window.Element('b_base_time_k').Update(elapse_str)
human_timer = self.define_timer(window)
engine_timer = self.define_timer(window, 'engine')

# Game loop
while not board.is_game_over(claim_draw=True):
Expand Down

0 comments on commit af00357

Please sign in to comment.