-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess_moves.py
216 lines (148 loc) · 3.85 KB
/
chess_moves.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
import random
SQUARE = 'g7'
FILE = 'chess_moves.txt'
def main():
print ''
sample_moves(SQUARE)
print ''
all_moves()
print ''
def all_moves():
moves = []
s = ''
for y in range(0,8):
for x in range(0,8):
sq = Square(x,y)
m = Queen(sq).get_moves()+Knight(sq).get_moves()+Pawn(sq).get_moves()
moves.extend(m)
s += '%s ' % len(m)
s += '\n'
print s
print 'Total: %s' % len(moves)
print ('Examples: '
+ ','.join([str(random.choice(moves)) for x in range(0,10)])
+',...'
)
with open(FILE,'w') as f:
f.write('\n'.join([str(x) for x in sorted(moves)])+'\n')
def sample_moves(sq):
s = Square(sq)
q = Queen(s).get_moves()
k = Knight(s).get_moves()
p = Pawn(s).get_moves()
moves = q+k+p
print '%s Moves: %s' % (s,len(moves))
show_moves(s,moves)
print 'Knight: '+','.join([str(x) for x in k])
print 'Pawn: '+','.join([str(x) for x in p])
def show_moves(piece,moves):
squares = [move.other(piece) for move in moves]
s = '+-----------------+\n'
for y in range(7,-1,-1):
s += '| '
for x in range(0,8):
sq = Square(x,y)
if sq==piece:
s += 'o '
elif sq in squares:
s += 'x '
else:
s += '. '
s += '|\n'
s += '+-----------------+'
print s
class Square(object):
@staticmethod
def valid(s):
return s.x>=0 and s.x<8 and s.y>=0 and s.y<8
@staticmethod
def a1(x,y):
return '%s%s' % ('abcdefgh'[x],y+1)
@staticmethod
def xy(a1):
return ('abcdefg'.index(a1[0]),int(a1[1])-1)
def __init__(self,*args):
if len(args)==1:
vals = Square.xy(args[0])
else:
vals = args
(self.x,self.y) = vals
def __eq__(self,other):
return self.x==other.x and self.y==other.y
def __str__(self):
return Square.a1(self.x,self.y)
__repr__ = __str__
class Move(object):
@staticmethod
def valid(move):
return Square.valid(move.a) and Square.valid(move.b)
def __init__(self,a,b,promote=None):
self.a = a
self.b = b
self.p = promote
if self.p and self.p not in 'rnbq':
raise ValueError('invalid promotion "%s"' % self.p)
def other(self,sq):
if sq==self.a:
return self.b
elif sq==self.b:
return self.a
def __eq__(self,x):
return self.a==x.a and self.b==x.b and self.p==x.p
def __lt__(self,other):
return str(self)<str(other)
def __contains__(self,x):
return x==self.a or x==self.b
def __str__(self):
return '%s%s%s' % (self.a,self.b,self.p if self.p else '')
__repr__ = __str__
def __hash__(self):
return hash(str(self))
class Piece(object):
def __init__(self,square):
self.s = square
self.x = square.x
self.y = square.y
def get_moves(self):
return [Move(Square(self.x,self.y),s) for s in self.get_squares()]
def get_squares(self):
return [s for s in self.get() if Square.valid(s)]
def get(self):
raise NotImplementedError
class Queen(Piece):
def get(self):
moves = []
for i in range(0,8):
if i!=self.x:
moves.append(Square(i,self.y))
moves.append(Square(i,self.y-self.x+i))
moves.append(Square(i,self.y+self.x-i))
if i!=self.y:
moves.append(Square(self.x,i))
return moves
class Knight(Piece):
MOVES = [(-2,-1),(-2,1),(-1,-2),(-1,2),(1,-2),(1,2),(2,-1),(2,1)]
def get(self):
moves = []
for (x,y) in Knight.MOVES:
moves.append(Square(self.x+x,self.y+y))
return moves
class Pawn(Piece):
def get(self):
if self.y not in [1,6]:
return []
y = {1:0,6:7}[self.y]
moves = []
for i in range(self.x-1,self.x+2):
moves.append(Square(i,y))
return moves
def get_moves(self):
moves = []
for sq in self.get_squares():
for p in 'rnbq':
me = Square(self.x,self.y)
moves.append(Move(me,sq,p))
return moves
if __name__=='__main__':
main()