-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.py
177 lines (123 loc) · 5.49 KB
/
piece.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# piece.py
# ChessPy
#
# Created by Yuval Atir
import defines
# import chessboard as cb
class Piece():
def __init__(self, bwb = defines.piece_color_undefined, pt = defines.nada):
self._bw = bwb
self._ptype = pt
#virtual ~Piece() {};
#virtual bool is_legal(int,int,int,int) const = 0;
#virtual bool is_legal_pawn_kill(Chessboard*,int,int,int,int) const {return true;};
def get_color(self):
return self._bw
def get_type(self):
return self._ptype
def set_color(self, piece_color_in):
self._bw = piece_color_in
def set_type(self,piece_type_in):
self._ptype = piece_type_in
class King(Piece):
def __init__(self,piece_color_in, piece_type_in = defines.king):
self.set_color(piece_color_in)
self.set_type(piece_type_in)
def is_legal(self,from_row, from_col, to_row, to_col): # const
if ( ( to_row >= 0 and to_col >= 0 ) and ( to_row <= 7 and to_col <= 7 ) ): # within bounds
if ( ( ( abs( from_row-to_row ) == 1 and abs( to_col - from_col ) == 1) or # one step diagonal
( abs( from_row - to_row ) == 1 and ( to_col == from_col ) ) or # one step vertical
( ( from_row == to_row ) and abs( to_col - from_col ) == 1 ) ) and # one step horizontal
not ( ( from_col == to_col ) and ( to_row == from_row ) ) ): # not the same location
if ( ( abs( from_row-to_row ) == 1 and abs( to_col - from_col ) == 1) or
( abs( from_row - to_row ) == 1 and ( to_col == from_col ) ) ):
return True
else:
return False
return False
class Pawn(Piece):
def __init__(self,piece_color_in, piece_type_in = defines.pawn):
self.set_color(piece_color_in)
self.set_type(piece_type_in)
def is_legal(self, from_row, from_col, to_row, to_col ): # const
if ( ( to_row >= 0 and to_col >= 0) and ( to_row <= 7 and to_col <= 7) ):
if from_row in (1,6):
if ( ( (abs( from_row - to_row ) == 2 ) or ( abs( from_row - to_row ) == 1 ) )
and ( to_col == from_col ) ):
if ( ( ( self.get_color() == defines.black_piece) and (to_row-from_row > 0) ) or
( ( self.get_color() == defines.white_piece ) and ( to_row-from_row < 0) ) ):
return True
return False
else:
if ( ( abs( from_row - to_row ) == 1 ) and ( to_col == from_col ) ):
if ( ( ( self.get_color() == defines.black_piece ) and ( to_row - from_row > 0 ) ) or
( ( self.get_color() == defines.white_piece ) and ( to_row - from_row < 0) ) ):
return True;
return False
return False
def is_legal_pawn_kill(self, pboard, from_row, from_col, to_row, to_col): # const
if ( pboard.get_piece(from_row,from_col).get_color() == defines.black_piece ):
if ( pboard.get_piece(from_row,from_col).get_type() == defines.pawn ):
if ( ( to_row-from_row == 1 ) and ( abs( to_col - from_col ) == 1 ) ):
return True
if ( pboard.get_piece(from_row,from_col).get_color() == defines.white_piece ):
if ( pboard.get_piece(from_row,from_col).get_type() == defines.pawn ):
if ( ( from_row-to_row == 1 ) and ( abs( to_col-from_col ) == 1 ) ):
return True
return False
class Rook(Piece):
def __init__(self,piece_color_in, piece_type_in = defines.rook):
self.set_color(piece_color_in)
self.set_type(piece_type_in)
def is_legal(self, from_row, from_col, to_row, to_col): # const
if ( ( to_row >= 0 and to_col >= 0 ) and ( to_row <= 7 and to_col <= 7 ) ):
if ( ( ( from_row == to_row ) or ( from_col == to_col ) ) and ( not ( ( from_row == to_row ) and ( from_col == to_col ) ) ) ):
return True
else:
return False
return False
class Bishop(Piece):
def __init__(self,piece_color_in, piece_type_in = defines.bishop):
self.set_color(piece_color_in)
self.set_type(piece_type_in)
def is_legal(self, from_row, from_col, to_row, to_col): #const
if ( ( to_row >= 0 and to_col >= 0) and ( to_row <= 7 and to_col <= 7) ) :
if ( ( abs( to_row - from_row ) == abs( to_col - from_col ) ) and ( to_row - from_row != 0 ) ):
return True
else:
return False
return False
class Queen(Piece):
def __init__(self,piece_color_in, piece_type_in = defines.queen):
self.set_color(piece_color_in)
self.set_type(piece_type_in)
def is_legal(self, from_row, from_col, to_row, to_col): # const
if ( ( to_row >= 0 and to_col >= 0 ) and ( to_row <= 7 and to_col <= 7 ) ):
if ( ( ( (abs( to_row - from_row ) == abs( to_col - from_col ) ) and ( to_row - from_row != 0 ) ) or # bishop condition
( ( from_row == to_row ) or ( from_col == to_col ) ) ) and # rook condition
( not ( ( from_row == to_row ) and ( from_col == to_col ) ) ) ): # same place not allowed
return True
else:
return False
return False
class Knight(Piece):
def __init__(self, piece_color_in, piece_type_in = defines.knight):
self.set_color(piece_color_in)
self.set_type(piece_type_in)
def is_legal(self, from_row, from_col, to_row, to_col): # const
if ( ( to_row >= 0 and to_col >= 0 ) and ( to_row <= 7 and to_col <= 7 ) ) :
if ( ( ( abs( from_row - to_row ) == 1 and abs( to_col - from_col ) == 2 ) or
( abs( from_row - to_row ) == 2 and abs( to_col - from_col ) == 1 ) ) and
not ( (from_col==to_col) and (to_row==from_row) ) ):
return True
else:
return False
return False
class EmptyPiece(Piece):
def __init__(self, piece_color_in = defines.piece_color_undefined, piece_type_in = defines.nada):
self.set_color(piece_color_in)
self.set_type(piece_type_in)
def is_legal(self, from_row, from_col, to_row, to_col): # const
return True
#endif /* piece_hpp */