Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror the board instead of rotating when transitioning to black. #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions sunfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand All @@ -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


Expand Down Expand Up @@ -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":
Expand All @@ -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:
Expand Down