-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckers.py
More file actions
219 lines (194 loc) · 6.26 KB
/
checkers.py
File metadata and controls
219 lines (194 loc) · 6.26 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
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
217
218
import random
class checkers :
init_board = """xwxwxwxw
wxwxwxwx
xwxwxwxw
oxoxoxox
xoxoxoxo
bxbxbxbx
xbxbxbxb
bxbxbxbx""".split('\n')
# the otherteam variable is an int, a bit in the 4 place means black, a bit in the 2 place means white
def __init__(self) :
self.board = [[0 for x in range(0, 8)] for y in range(0, 8)]
for i in range(0, len(self.init_board)) :
#2 is a white piece, 3 is a white king, 4 is a black piece, 5 is a black king, 0 is open, -1 is invalid
newline = map(lambda c : -1 if c == 'x' else 2 if c == 'w' else 4 if c == 'b' else 0, self.init_board[i])
self.board[i] = newline
self.bsize = len(self.board)
self.over = False
self.p1win = False
self.p2win = False
#self.print_board()
#directions : 1 = up left, 2 = up right, 3 = down left, 4 = down right
#perspective is black on bottom
def get_move_dirs(self, x, y) :
check = self.board[y][x]
if check == 2 :
return [3, 4]
elif check == 3 :
return [1, 2, 3, 4]
elif check == 4 :
return [1, 2]
elif check == 5:
return [1, 2, 3, 4]
def get_valid_moves(self, p1 = True) :
#tuple for valid moves: (x, y, dir)
vmoves = []
vjumps = []
for i in range(0, self.bsize) :
for j in range(0, self.bsize) :
if p1 :
if (self.board[j][i] == 2) :
self.multi_check(i, j, [3, 4], vmoves, vjumps, 4)
elif (self.board[j][i] == 3) :
self.multi_check(i, j, [1, 2, 3, 4], vmoves, vjumps, 4)
else :
if(self.board[j][i] == 4) :
self.multi_check(i, j, [1, 2], vmoves, vjumps, 2)
elif (self.board[j][i] == 5) :
self.multi_check(i, j, [1, 2, 3, 4], vmoves, vjumps, 2)
return (vmoves, vjumps)
def multi_check(self, x, y, dirs, vm, vj, otherteam) :
for d in dirs :
res = self.check_moves(x, y, d, otherteam)
if (res == 1) :
vm.append((x, y, d))
elif (res == 2) :
vj.append((x, y, d))
#directions : 1 = up left, 2 = up right, 3 = down right, 4 = down left
#return -1 if invalid, 1 if valid, 2 if jump
def check_moves(self, x, y, dir, otherteam) :
if dir == 1 :
if (x == 0 or y == 0):
return -1
if (self.board[y - 1][x - 1] == 0) :
return 1
elif (x == 1 or y == 1 or self.board[y - 2][x - 2] != 0) :
return -1
elif (self.board[y - 1][x - 1] & otherteam == otherteam) :
return 2
else :
return -1
elif dir == 2 :
if (x == self.bsize - 1 or y == 0):
return -1
if (self.board[y - 1][x + 1] == 0) :
return 1
elif (x == self.bsize - 2 or y == 1 or self.board[y - 2][x + 2] != 0) :
return -1
elif (self.board[y - 1][x + 1] & otherteam == otherteam) :
return 2
else :
return -1
elif dir == 3 :
if (x == self.bsize - 1 or y == self.bsize - 1):
return -1
if (self.board[y + 1][x + 1] == 0) :
return 1
elif (x == self.bsize - 2 or y == self.bsize - 2 or self.board[y + 2][x + 2] != 0) :
return -1
elif (self.board[y + 1][x + 1] & otherteam == otherteam) :
return 2
else :
return -1
else :
if (x == 0 or y == self.bsize - 1):
return -1
if (self.board[y + 1][x - 1] == 0) :
return 1
elif (x == 1 or y == self.bsize - 2 or self.board[y + 2][x - 2] != 0) :
return -1
elif (self.board[y + 1][x - 1] & otherteam == otherteam) :
return 2
else :
return -1
def do_move(self, x, y, dir, otherteam, jump = False) :
if dir == 1 :
dist = (-1, -1)
elif dir == 2:
dist = (1, -1)
elif dir == 3:
dist = (1, 1)
else :
dist = (-1, 1)
if jump :
jmp = (x + dist[0], y + dist[1])
newpos = (x + dist[0] * 2, y + dist[1] * 2)
if(jmp[0] < 0 or jmp[0] > self.bsize - 1 or jmp[1] < 0 or jmp[1] > self.bsize - 1) :
raise Exception("Jump goes off the board!")
if(self.board[jmp[1]][jmp[0]] & otherteam != otherteam) :
print jmp[1], jmp[0], self.board[jmp[1]][jmp[0]], otherteam
raise Exception("Jump does not have an enemy piece to jump!")
if(newpos[0] < 0 or newpos[0] > self.bsize - 1 or newpos[1] < 0 or newpos[1] > self.bsize - 1) :
raise Exception("Jump move goes off the board!")
if(self.board[newpos[1]][newpos[0]] != 0) :
raise Exception("Can't jump to a space occupied by another piece!")
self.board[newpos[1]][newpos[0]] = self.board[y][x]
self.board[y][x] = 0
self.board[jmp[1]][jmp[0]] = 0
return newpos
else :
newpos = (x + dist[0], y + dist[1])
if(newpos[0] < 0 or newpos[0] > self.bsize - 1 or newpos[1] < 0 or newpos[1] > self.bsize - 1) :
raise Exception("Move goes off the board!")
if self.board[newpos[1]][newpos[0]] != 0 :
raise Exception("Can't move to a space occupied by another piece!")
self.board[newpos[1]][newpos[0]] = self.board[y][x]
self.board[y][x] = 0
return newpos
def king_check(self) :
for x in range(0, self.bsize) :
if(self.board[0][x] == 4) :
self.board[0][x] = 5
if(self.board[self.bsize - 1][x] == 2) :
self.board[self.bsize - 1][x] = 3
def do_random_move(self, p1 = True) :
if(self.over) :
print "Game already over, no further moves"
return
otherteam = 4 if p1 else 2
moves = self.get_valid_moves(p1)
if(len(moves[1]) == 0 and len(moves[0]) == 0) :
self.over = True
if p1 :
self.p2win = True
print "Game over, p2 wins"
else :
self.p1win = True
print "Game over, p1 wins"
return
#no jumps
if (len(moves[1]) == 0) :
todo = moves[0][random.randrange(len(moves[0]))]
self.do_move(todo[0], todo[1], todo[2], otherteam)
else :
todo = moves[1][random.randrange(len(moves[1]))]
canjump = True
#continue jumping if we can
while canjump :
newpos = self.do_move(todo[0], todo[1], todo[2], otherteam, True)
newjumps = []
self.multi_check(newpos[0], newpos[1], self.get_move_dirs(newpos[0], newpos[1]), [], newjumps, otherteam)
canjump = len(newjumps) != 0
if(canjump) :
print "Multijumping!"
todo = newjumps[random.randrange(len(newjumps))]
self.king_check()
def print_board(self, board = None) :
if (board == None) :
board = self.board
print "Checkers Board:"
for i in range(0, len(board)) :
print '\t'.join(map(lambda c : str(c), board[i]))
#testing that a full game of random checkers can be played
if __name__ == "__main__" :
check = checkers()
check.print_board()
while not check.over :
print "Player 1"
check.do_random_move(True)
check.print_board()
print "Player 2"
check.do_random_move(False)
check.print_board()