improved hysteresis

This commit is contained in:
Thomas Lindner 2022-12-30 14:17:19 +01:00
parent 5047c13452
commit 362837997e
3 changed files with 35 additions and 21 deletions

View file

@ -28,8 +28,10 @@ install_requires =
where = src where = src
[options.data_files] [options.data_files]
share/blunderboard/sounds/blunder = sounds/blunder/tts_what_a_blunder.mp3 share/blunderboard/sounds/blunder =
share/blunderboard/sounds/illegal = sounds/illegal/alarm.mp3 sounds/blunder/tts_what_a_blunder.mp3
share/blunderboard/sounds/illegal =
sounds/illegal/alarm.mp3
[options.entry_points] [options.entry_points]
console_scripts = console_scripts =
@ -58,6 +60,7 @@ commands =
[flake8] [flake8]
max_line_length = 88 max_line_length = 88
extend_ignore = E203
[mypy] [mypy]
check_untyped_defs = True check_untyped_defs = True

View file

@ -12,4 +12,4 @@ def main():
reader.print() reader.print()
while True: while True:
reader.scan() reader.scan()
sleep(0.1) sleep(1)

View file

@ -3,7 +3,7 @@ import RPi.GPIO as gpio
class BoardReader: class BoardReader:
hysteresis = 10 hysteresis = 16
default_gpio_mode = gpio.BCM default_gpio_mode = gpio.BCM
default_row_gpios = [4, 5, 6, 12, 13, 16, 17, 19] default_row_gpios = [4, 5, 6, 12, 13, 16, 17, 19]
default_column_gpios = [20, 21, 22, 23, 24, 25, 26, 27] default_column_gpios = [20, 21, 22, 23, 24, 25, 26, 27]
@ -60,39 +60,50 @@ class BoardReader:
if gpio.input(column_gpio): if gpio.input(column_gpio):
next_board[i][j] = "x" next_board[i][j] = "x"
gpio.output(row_gpio, gpio.LOW) gpio.output(row_gpio, gpio.LOW)
prev_board = self.board_history[-1]
self.board_history = [next_board] + self.board_history[:-1] self.board_history = [next_board] + self.board_history[:-1]
# if the oldest board is not in inital position but all newer boards are, reset # if the oldest half of the board history is not in inital position but all
# game state # newer boards are, reset game state
if not self._is_initial_board(prev_board): for board in self.board_history[self.hysteresis // 2 :]:
for board in self.board_history: if self._is_initial_board(board):
break
else:
for board in self.board_history[: self.hysteresis // 2]:
if not self._is_initial_board(board): if not self._is_initial_board(board):
break break
else: else:
self.move_generator.reset() self.move_generator.reset()
self.print()
return return
for i, row in enumerate(prev_board): for i in range(8):
for j, field in enumerate(row): for j in range(8):
# if the oldest board has a piece but no newer boards have it, the # if the oldest half of the board history has a piece but no newer
# piece was removed # boards have it, the piece was removed
if field == "x": for board in self.board_history[self.hysteresis // 2 :]:
for board in self.board_history: if board[i][j] == " ":
break
else:
for board in self.board_history[: self.hysteresis // 2]:
if board[i][j] == "x": if board[i][j] == "x":
break break
else: else:
self.move_generator.take(i, j) self.move_generator.take(i, j)
for i, row in enumerate(prev_board): self.print()
for j, field in enumerate(row): for i in range(8):
# if the oldest board doesn't have a piece but all newer boards have for j in range(8):
# it, the piece was placed # if the oldest half of the board history doesn't have a piece but all
if field == " ": # newer boards have it, the piece was placed
for board in self.board_history: for board in self.board_history[self.hysteresis // 2 :]:
if board[i][j] == "x":
break
else:
for board in self.board_history[: self.hysteresis // 2]:
if board[i][j] == " ": if board[i][j] == " ":
break break
else: else:
self.move_generator.put(i, j) self.move_generator.put(i, j)
self.print()
def _print(self, board) -> None: def _print(self, board) -> None:
print(" a b c d e f g h") print(" a b c d e f g h")