-
Notifications
You must be signed in to change notification settings - Fork 0
/
pieces.py
162 lines (134 loc) · 3.77 KB
/
pieces.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
# -*- coding: utf-8 -*-
# TO DO: make Pieces an abstract class
class PiecesError(Exception): pass
class Pieces(object):
'''Preside of Pieces Objects'''
piece_chars = ['P', 'N', 'B', 'R', 'Q', 'K']
@staticmethod
def create(char, color):
'''Given a char representation of a piece, and a color (w,b)
Return instantiated Piece object of that type
It will not yet have a postion
'''
char = char
if char == 'P':
return Pawn(color)
elif char == 'N':
return Knight(color)
elif char == 'B':
return Bishop(color)
elif char == 'R':
return Rook(color)
elif char == 'Q':
return Queen(color)
elif char == 'K':
return King(color)
else:
raise PiecesError('Unrecognzied piece abbreviation: %s' % char)
class PieceError(Exception): pass
class Piece(object):
'''Super class for all pieces
'''
def __init__(self, color):
if color not in ('w', 'b'):
raise PiecesError('Unrecognized color: %s' % color)
self.color = color
self.postion = None
self.moved = False
def __repr__(self):
return '%s:%s%s' %(self.color, self.char, self.position)
@property
def x(self):
if self.position == 'x':
return None
return ord(self.position[0])-97
@property
def y(self):
if self.position == 'x':
return None
return int(self.position[1])-1
@property
def opposite_color(self):
return 'w' if self.color == 'b' else 'b'
@property
def char_glyph(self):
return self.char.lower() if self.color == 'w' else self.char
@property
def glyph(self):
if self.color == 'w':
return self.glyphs[0]
else:
return self.glyphs[1]
@property
def data(self):
glyph = self.glyphs[0] if self.color == 'w' else self.glyphs[1]
return {'color': self.color,
'position': self.position,
'x': self.x,
'y': self.y,
'char': self.char,
'glyph': glyph,
}
class Pawn(Piece):
# d f e
# .
char = 'P'
value = 1
glyphs = ['♙', '♟']
@property
def move_ops(self):
'''Can move one move or two depending starting pos.
Can move diagnal forward to capture
'''
_move_ops = ['d1', 'e1', 'f1']
if (self.color == 'w' and self.position[1] == '2') or \
(self.color == 'b' and self.position[1] == '7'):
_move_ops[-1] = 'f2'
return _move_ops
def __repr__(self):
return '%s:%s' %(self.color, self.position)
class Knight(Piece):
# j k
# i m
# .
# q n
# p o
char = 'N'
value = 3
glyphs = ['♘', '♞']
move_ops = ['i1','j1','k1','m1','n1','o1','p1','q1']
class Bishop(Piece):
# d e
# .
# g h
char = 'B'
value = 3
glyphs = ['♗', '♝']
move_ops = ['d*', 'e*', 'g*', 'h*']
class Rook(Piece):
# f
# r . l
# b
char = 'R'
value = 5
glyphs = ['♖', '♜']
move_ops =['f*', 'b*', 'l*', 'r*']
class Queen(Piece):
# d f e
# r . l
# g b h
char = 'Q'
value = 9
glyphs = ['♕', '♛']
move_ops = ['f*', 'b*', 'l*', 'r*',
'd*', 'e*', 'g*', 'h*']
class King(Piece):
# d f e
# r . l
# g b h and y, z -> castle queen side, king side
char = 'K'
value = 0
glyphs = ['♔', '♚']
move_ops = ['f1', 'b1', 'l1', 'r1', # orthogonal
'd1', 'e1', 'g1', 'h1', # diaganal
'y1', 'z1'] # castle