-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiece.py
More file actions
154 lines (128 loc) · 4.57 KB
/
Copy pathpiece.py
File metadata and controls
154 lines (128 loc) · 4.57 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
from enum import Enum
import numpy as np
class NameMapping(Enum):
pawn = 1
knight = 2
bishop = 3
rook = 4
queen = 5
king = 6
class Piece:
def __init__(self, x, y) -> None:
self.x = x
self.y = y
def pawn_rule(color, board, x,y,newx,newy):
enemy_indexes = [(i)*color*-1 for i in range(1, 7)]
if x != newx: # attack
if color == 1:
if abs(newx-x) == 1 and y-newy == 1:
if board[newy, newx] in enemy_indexes:
return True
else:
if abs(newx-x) == 1 and newy-y == 1:
if board[newy, newx] in enemy_indexes:
return True
return False
else: # move
if color == 1:
is_first_move = True if y == 6 else False
if is_first_move and y-newy == 2 and board[newy ,newx] == 0 and board[newy+1, newx]== 0:
return True
elif y-newy == 1 and board[newy, newx] == 0:
return True
else:
is_first_move = True if y == 1 else False
if is_first_move and newy-y == 2 and board[newy, newx]==0 and board[newy-1, newx] ==0:
return True
elif newy-y == 1 and board[newy, newx] == 0:
return True
return False
def knight_rule(color, board, x,y,newx,newy):
if not abs(x-newx) + abs(y-newy) == 3:
return False
if abs(x-newx) == 3 or abs(y-newy) == 3:
return False
friend_indexes = [(i)*color for i in range(1, 7)]
if board[newy, newx] not in friend_indexes:
return True
def bishop_rule(color, board, x,y,newx,newy):
if (x == newx or y == newy):
return False
if abs(x-newx) != abs(y-newy):
return False
friend_indexes = [(i)*color for i in range(1, 7)]
enemy_indexes = [(i)*color*-1 for i in range(1, 7)]
x_sign = 1 if x < newx else -1
y_sign = 1 if y < newy else -1
# if y < 0 -> go up
# if y > 0 -> go down
# if x < 0 -> left
# if x > 0 -> right
# x-newx == y-newy
for i in range(0, abs(x-newx), 1):
if board[y+(i*y_sign), x+(i*x_sign)] == 0 or (y+(i*y_sign) == y and x+(i*x_sign) == x):
continue
if board[y+(i*y_sign), x+(i*x_sign)] in enemy_indexes and i != abs(x-newx):
return False
if board[y+(i*y_sign), x+(i*x_sign)] in friend_indexes:
return False
if abs(x-newx) == 1:
if board[newy, newx] in friend_indexes:
return False
else:
return True
return True
def rook_rule(color, board, x,y,newx,newy):
if not ((x == newx and y != newy) or (y == newy and x != newx)):
return False
friend_indexes = [(i)*color for i in range(1, 7)]
enemy_indexes = [(i)*color*-1 for i in range(1, 7)]
is_x_move = True if (y == newy and x != newx) else False
if is_x_move:
is_move_left = True if x - newx > 0 else False
if is_move_left:
for i in range(newx, x+1):
if board[y, i] in friend_indexes and i!=x:
return False
if board[y, i] in enemy_indexes and i!=newx:
return False
else:
for i in range(x, newx+1):
if board[y, i] in friend_indexes and i!=x:
return False
if board[y, i] in enemy_indexes and i!=newx:
return False
return True
else:
is_move_up = True if y - newy > 0 else False
if is_move_up:
for i in range(newy, y+1):
if board[i, x] in friend_indexes and i!=y:
return False
if board[i, x] in enemy_indexes and i!=newy:
return False
else:
for i in range(y, newy+1):
if board[i, x] in friend_indexes and i!=y:
return False
if board[i, x] in enemy_indexes and i!=newy:
return False
return True
def queen_rule(color, board, x,y,newx,newy):
if bishop_rule(color, board, x,y,newx,newy) or rook_rule(color, board, x,y,newx,newy):
return True
return False
def king_rule(color, board, x,y,newx,newy):
if (bishop_rule(color, board, x,y,newx,newy) or rook_rule(color, board, x,y,newx,newy))\
and (abs(newx-x) in (0, 1) and abs(newy-y) in (0, 1)):
return True
return False
RuleMapping = [
True,
pawn_rule,
knight_rule,
bishop_rule,
rook_rule,
queen_rule,
king_rule,
]