-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmove_on_board.py
155 lines (118 loc) · 4.02 KB
/
move_on_board.py
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
from chess import *
allow_functions = {
"K": k_allowed,
"N": n_allowed,
"R": r_allowed,
"P": p_allowed,
"Q": q_allowed,
"B": b_allowed,
"PE": pe_allowed,
}
def attacked_1_8(cell, board, by_color):
return attacked(coord(cell), board, by_color)
def attacked(cell, board, by_color):
x_to,y_to = uncoord(cell)
for i in range(len(board)):
for j in range(len(board)):
color, fig = board[i][j]
x,y = uncoord((i,j))
if color == by_color:
if fig == "P":
fig = "PE"
if allow_functions[fig]((x,y),(x_to,y_to),by_color):
if middle_empty((i, j), (cell[0], cell[1]), board):
return True
return False
def middle_attacked(cell1, cell2, board, by_color):
for cell in middle(cell1, cell2):
if attacked(cell, board, by_color):
return True
return False
def middle_empty(cell1, cell2, board):
i,j = cell1
i_to, j_to = cell2
for cell in middle((i,j),(i_to,j_to)):
if not empty(board[cell[0]][cell[1]]):
return False
return True
def middle(cell1, cell2):
i,j = cell1
i_to,j_to = cell2
di, dj = 0, 0
if i_to > i:
di = 1
elif i_to < i:
di = -1
if j_to > j:
dj = 1
elif j_to < j:
dj = -1
cells = []
counter = 0
while (i, j) != (i_to, j_to) and counter < 100:
counter += 1
i += di
j += dj
if (i, j) != (i_to, j_to):
cells.append((i,j))
return cells
def coord(cell1):
x,y = cell1
i,j = 8-y, x-1
return i,j
def uncoord(cell1):
i,j = cell1
x,y = j+1, 8-i
return x,y
def empty(cell_content):
return cell_content == " "
def board_move_ok(cell_from, cell_to, board):
if out_of_range(cell_from, cell_to):
return False
i, j = coord(cell_from)
i_to, j_to = coord(cell_to)
color, figure = board[i][j]
color_to, figure_to = board[i_to][j_to]
if color == "W":
by_color = "B"
else:
by_color = "W"
if figure == "K" and not attacked(coord(cell_from), board, by_color):
# Check castling
castling = False
# White
if color == "W" and coord(cell_from) == (7,4) and coord(cell_to) == (7,2) and board[7][0] == "WR":
if middle_empty((7, 4), (7, 0), board) and not middle_attacked((7, 4), (7, 0), board, "B"):
return True
if color == "W" and coord(cell_from) == (7,4) and coord(cell_to) == (7,6) and board[7][7] == "WR":
if middle_empty((7,4),(7,7), board) and not middle_attacked((7, 4), (7, 7), board, "B"):
# Check Schach later
return True
# Black
if color == "B" and coord(cell_from) == (0,4) and coord(cell_to) == (0,2) and board[0][0] == "BR":
if middle_empty((0,4),(0,0), board) and not middle_attacked((0, 4), (0, 0), board, "W"):
# Check Schach later
return True
if color == "B" and coord(cell_from) == (0,4) and coord(cell_to) == (0,6) and board[0][7] == "BR":
if middle_empty((0,4),(0,7), board) and not middle_attacked((0, 4), (0, 7), board, "W"):
# Check Schach later
return True
# Middle_empty and Middle_not_under_attack
# check if Schach and figure == king
eating = not empty(board[i_to][j_to])
if color == color_to:
return False
if figure != "P" or empty(board[i_to][j_to]):
if not allow_functions[figure](cell_from, cell_to, color == "W"):
return False
if figure == "N":
return True
if figure == "P" and eating:
if not allow_functions["PE"](cell_from, cell_to, color == "W"):
return False
if not middle_empty((i,j),(i_to,j_to), board):
return False
# check if Schach
return True
# TODO: If King and currently under attack, cannot castle
# TODO: If King, after move, should not be under attack