chess playing implemented, minus game over checks

This commit is contained in:
Rudis Muiznieks 2022-04-25 08:27:32 -05:00
parent 38a2854378
commit 6620385196
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
3 changed files with 8 additions and 13 deletions

View File

@ -34,7 +34,7 @@ def execute(cinput: ControlInput, graphics: Graphics, _):
sunfish.initial, 0, (True,True), (True,True), 0, 0)] sunfish.initial, 0, (True,True), (True,True), 0, 0)]
player_color = 'wb'[get_color(hist[-1])] player_color = 'wb'[get_color(hist[-1])]
draw.draw_board(hist[-1] if player_color == "w" else hist[-1].rotate(), player_color) draw.draw_board(hist[-1], player_color)
state = GameState.MAIN_MENU state = GameState.MAIN_MENU
menu_index = 0 menu_index = 0
@ -58,12 +58,8 @@ def execute(cinput: ControlInput, graphics: Graphics, _):
if time.time() - start > MAX_THINKING_SECONDS: if time.time() - start > MAX_THINKING_SECONDS:
break break
hist.append(hist[-1].move(move)) hist.append(hist[-1].move(move))
draw.draw_board(hist[-1].rotate(), player_color) draw.draw_board(hist[-1], player_color)
last_move = move_str(move, player_color == "w") last_move = move_str(move, player_color == "w")
if score == sunfish.MATE_UPPER:
draw.draw_checkmate(last_move, False)
state = GameState.GAME_OVER
else:
all_moves = dict() all_moves = dict()
state = GameState.CHOOSE_SRC state = GameState.CHOOSE_SRC
elif state == GameState.CHOOSE_SRC: elif state == GameState.CHOOSE_SRC:
@ -105,7 +101,7 @@ def execute(cinput: ControlInput, graphics: Graphics, _):
player_color = "w" player_color = "w"
state = GameState.CHOOSE_SRC state = GameState.CHOOSE_SRC
hist = [pos] hist = [pos]
draw.draw_board(hist[-1], player_color) draw.draw_board(hist[-1] if player_color == "w" else hist[-1].rotate(), player_color)
elif state == GameState.CHOOSE_SRC: elif state == GameState.CHOOSE_SRC:
if key == Button.DIR_D or key == Button.DIR_R: if key == Button.DIR_D or key == Button.DIR_R:
src_idx += 1 src_idx += 1
@ -138,7 +134,7 @@ def execute(cinput: ControlInput, graphics: Graphics, _):
draw.draw_board(hist[-1].rotate(), player_color) draw.draw_board(hist[-1].rotate(), player_color)
state = GameState.THINKING state = GameState.THINKING
else: else:
if key == Button.BTN_A: if key == Button.BTN_A or key == Button.BTN_B:
state = GameState.MAIN_MENU state = GameState.MAIN_MENU
def get_all_moves(pos: sunfish.Position): def get_all_moves(pos: sunfish.Position):

View File

@ -81,8 +81,7 @@ class Draw:
def draw_board(self, pos: Position, player_color: str): def draw_board(self, pos: Position, player_color: str):
self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0)
nb = "".join(pos.board.split()) nb = "".join(pos.board.split()) if player_color == "w" else "".join(pos.rotate().board.split())[::-1]
nb = nb[::-1] if player_color == "b" else nb
c = True c = True
for row in range(8): for row in range(8):
for col in range(8): for col in range(8):

View File

@ -295,7 +295,7 @@ class Searcher:
# Note, we don't have to check for legality, since we've already done it # Note, we don't have to check for legality, since we've already done it
# before. Also note that in QS the killer must be a capture, otherwise we # before. Also note that in QS the killer must be a capture, otherwise we
# will be non deterministic. # will be non deterministic.
killer = selfctp_move.get(pos) killer = self.tp_move.get(pos)
if killer and (depth > 0 or pos.value(killer) >= QS_LIMIT): if killer and (depth > 0 or pos.value(killer) >= QS_LIMIT):
yield killer, -self.bound(pos.move(killer), 1-gamma, depth-1, root=False) yield killer, -self.bound(pos.move(killer), 1-gamma, depth-1, root=False)
# Then all the other moves # Then all the other moves