From 7339935ce6f21349df4ec906ec463de335536440 Mon Sep 17 00:00:00 2001 From: Dolphus2 Date: Mon, 17 Jun 2024 08:57:09 +0200 Subject: [PATCH] Mirror the board instead of rotating when transitioning to black. --- sunfish.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/sunfish.py b/sunfish.py index 0c43388..1e3efc6 100755 --- a/sunfish.py +++ b/sunfish.py @@ -190,12 +190,25 @@ def gen_moves(self): if i == H1 and self.board[j + W] == "K" and self.wc[1]: yield Move(j + W, j + E, "") + @staticmethod + def mirror_square(p): + row = p // 10 + col = p % 10 + return 110 - row*10 + col + + @staticmethod + def mirror_board(board): + # Split the board into 10 rows and reverse their order to mirror the board + rows = [board[i:i+10] for i in range(0, len(board), 10)] + return ''.join(rows[::-1]) + def rotate(self, nullmove=False): """Rotates the board, preserving enpassant, unless nullmove""" return Position( - self.board[::-1].swapcase(), -self.score, self.bc, self.wc, - 119 - self.ep if self.ep and not nullmove else 0, - 119 - self.kp if self.kp and not nullmove else 0, + self.mirror_board(self.board).swapcase(), + -self.score, self.bc, self.wc, + self.mirror_square(self.ep) if self.ep and not nullmove else 0, + self.mirror_square(self.kp) if self.kp and not nullmove else 0, ) def move(self, move): @@ -239,10 +252,10 @@ def value(self, move): score = pst[p][j] - pst[p][i] # Capture if q.islower(): - score += pst[q.upper()][119 - j] + score += pst[q.upper()][self.mirror_square(j)] # Castling check detection if abs(j - self.kp) < 2: - score += pst["K"][119 - j] + score += pst["K"][self.mirror_square(j)] # Castling if p == "K" and abs(i - j) == 2: score += pst["R"][(i + j) // 2] @@ -252,7 +265,7 @@ def value(self, move): if A8 <= j <= H8: score += pst[prom][j] - pst["P"][j] if j == self.ep: - score += pst["P"][119 - (j + S)] + score += pst["P"][self.mirror_square(j + S)] return score @@ -473,7 +486,7 @@ def render(i): for ply, move in enumerate(args[3:]): i, j, prom = parse(move[:2]), parse(move[2:4]), move[4:].upper() if ply % 2 == 1: - i, j = 119 - i, 119 - j + i, j = Position.mirror_square(i), Position.mirror_square(j) hist.append(hist[-1].move(Move(i, j, prom))) elif args[0] == "go": @@ -490,7 +503,7 @@ def render(i): if score >= gamma: i, j = move.i, move.j if len(hist) % 2 == 0: - i, j = 119 - i, 119 - j + i, j = Position.mirror_square(i), Position.mirror_square(j) move_str = render(i) + render(j) + move.prom.lower() print("info depth", depth, "score cp", score, "pv", move_str) if move_str and time.time() - start > think * 0.8: