-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserFEN.py
More file actions
162 lines (127 loc) · 4.34 KB
/
parserFEN.py
File metadata and controls
162 lines (127 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Castling=[True,True,True,True,True,True,True]
# K k RK RQ rk rq
def ResetCastling():
for i in range(0,7,1):
Castling[i]=True
def matrix_to_fen(chessboard, moves, WhoNext, white_on_bottom, last_move=None):
if not white_on_bottom:
rotateMatrix(chessboard)
fen_rows = []
halfmove_clock = 0
for row in chessboard:
empty = 0
row_str = ""
for square in row:
if square == ".":
empty += 1
else:
if empty > 0:
row_str += str(empty)
empty = 0
row_str += square
if square.lower() == 'p' or last_move and chessboard[last_move[1][0]][last_move[1][1]] != ".":
halfmove_clock = 0
if empty > 0:
row_str += str(empty)
fen_rows.append(row_str)
board_fen = "/".join(fen_rows)
if(WhoNext=="white"):
turn = "w"
else:
turn = "b"
castling = get_castling_rights(chessboard, turn)
en_passant_target = get_en_passant_target(chessboard,last_move)
fullmove_number = moves // 2 + 1
return f"{board_fen} {turn} {castling} {en_passant_target} {halfmove_clock} {fullmove_number}"
def get_castling_rights(chessboard, turn):
rights = ""
if chessboard[7][7] != 'R':
Castling[2]=False
if chessboard[7][0] != 'R':
Castling[3]=False
if chessboard[7][4] != 'K':
Castling[0]=False
if chessboard[0][7] != 'r':
Castling[4]=False
if chessboard[0][0] != 'r':
Castling[5]=False
if chessboard[0][4] != 'k':
Castling[1]=False
if chessboard[7][4] == 'K' and not is_king_attacked(chessboard, "w"):
if chessboard[7][7] == 'R' and Castling[0] and Castling[2]:
rights += 'K'
if chessboard[7][0] == 'R' and Castling[0] and Castling[3]:
rights += 'Q'
if chessboard[0][4] == 'k' and not is_king_attacked(chessboard, "b"):
if chessboard[0][7] == 'r' and Castling[1] and Castling[4]:
rights += 'k'
if chessboard[0][0] == 'r' and Castling[1] and Castling[5]:
rights += 'q'
return rights if rights else "-"
def is_king_attacked(chessboard, color):
king_symbol = 'K' if color == "w" else 'k'
king_position = None
for i in range(8):
for j in range(8):
if chessboard[i][j] == king_symbol:
king_position = (i, j)
break
if king_position:
break
if not king_position:
return False
enemy_color = "b" if color == "w" else "w"
return is_square_attacked(chessboard, king_position, enemy_color)
def is_square_attacked(chessboard, position, attacker_color):
attackers = {
"p": [(1, -1), (1, 1)] if attacker_color == "w" else [(-1, -1), (-1, 1)],
"n": [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)],
"b": [(1, 1), (1, -1), (-1, 1), (-1, -1)],
"r": [(1, 0), (-1, 0), (0, 1), (0, -1)],
"q": [(1, 1), (1, -1), (-1, 1), (-1, -1), (1, 0), (-1, 0), (0, 1), (0, -1)],
"k": [(1, 1), (1, -1), (-1, 1), (-1, -1), (1, 0), (-1, 0), (0, 1), (0, -1)],
}
for dx, dy in attackers["n"]:
px, py = position[0] + dx, position[1] + dy
if 0 <= px < 8 and 0 <= py < 8 and chessboard[px][py].lower() == "n" and chessboard[px][py].islower() == (attacker_color == "b"):
return True
for piece in ["b", "r", "q"]:
for dx, dy in attackers[piece]:
px, py = position
while True:
px += dx
py += dy
if not (0 <= px < 8 and 0 <= py < 8):
break
if chessboard[px][py] != ".":
if chessboard[px][py].lower() == piece and chessboard[px][py].islower() == (attacker_color == "b"):
return True
break
for dx, dy in attackers["p"]:
px, py = position[0] + dx, position[1] + dy
if 0 <= px < 8 and 0 <= py < 8 and chessboard[px][py].lower() == "p" and chessboard[px][py].islower() == (attacker_color == "b"):
return True
for dx, dy in attackers["k"]:
px, py = position[0] + dx, position[1] + dy
if 0 <= px < 8 and 0 <= py < 8 and chessboard[px][py].lower() == "k" and chessboard[px][py].islower() == (attacker_color == "b"):
return True
return False
def get_en_passant_target(chessboard, last_move):
if not last_move:
return "-"
start, end = last_move
start_x, start_y = start
end_x, end_y = end
if abs(start_x - end_x) == 2 and chessboard[end_x][end_y].lower() == 'p':
en_passant_rank = 5 if chessboard[end_x][end_y].isupper() else 2
return f"{chr(end_y + 97)}{en_passant_rank}"
return "-"
def rotateMatrix(mat):
n = len(mat)
res = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
res[i][j] = mat[n - i - 1][n - j - 1]
for i in range(n):
for j in range(n):
mat[i][j] = res[i][j]