1 [w,h] = [8,8] # Width and height of board(s)
3 always_reveal_states = False
5 # Class to represent a quantum chess board
7 # Initialise; if master=True then the secondary piece types are assigned
8 # Otherwise, they are left as unknown
9 # So you can use this class in Agent programs, and fill in the types as they are revealed
10 def __init__(self, style="agent"):
12 self.pieces = {"white" : [], "black" : []}
13 self.grid = [[None] * w for _ in range(h)] # 2D List (you can get arrays in python, somehow, but they scare me)
14 self.unrevealed_types = {"white" : piece_types.copy(), "black" : piece_types.copy()}
15 self.king = {"white" : None, "black" : None} # We need to keep track of the king, because he is important
16 for c in ["black", "white"]:
17 del self.unrevealed_types[c]["unknown"]
22 # Add all the pieces with known primary types
25 s = ["black", "white"][i]
29 c.append(Piece(s, 0, y, ["rook"]))
30 c.append(Piece(s, 1, y, ["knight"]))
31 c.append(Piece(s, 2, y, ["bishop"]))
32 k = Piece(s, 3, y, ["king", "king"]) # There can only be one ruler!
33 k.types_revealed[1] = True
34 k.current_type = "king"
37 c.append(Piece(s, 4, y, ["queen"])) # Apparently he may have multiple wives though.
38 c.append(Piece(s, 5, y, ["bishop"]))
39 c.append(Piece(s, 6, y, ["knight"]))
40 c.append(Piece(s, 7, y, ["rook"]))
49 c.append(Piece(s, x, y, ["pawn"]))
52 types_left.update(piece_types)
53 del types_left["king"] # We don't want one of these randomly appearing (although it might make things interesting...)
54 del types_left["unknown"] # We certainly don't want these!
57 self.grid[piece.x][piece.y] = piece
59 if len(piece.types) > 1:
61 if style == "agent": # Assign placeholder "unknown" secondary type
62 piece.types.append("unknown")
65 elif style == "quantum":
66 # The master allocates the secondary types
67 choice = types_left.keys()[random.randint(0, len(types_left.keys())-1)]
68 types_left[choice] -= 1
69 if types_left[choice] <= 0:
70 del types_left[choice]
71 piece.types.append(choice)
72 elif style == "classical":
73 piece.types.append(piece.types[0])
74 piece.current_type = piece.types[0]
75 piece.types_revealed[1] = True
79 newboard = Board(master = False)
80 newpieces = newboard.pieces["white"] + newboard.pieces["black"]
81 mypieces = self.pieces["white"] + self.pieces["black"]
83 for i in range(len(mypieces)):
84 newpieces[i].init_from_copy(mypieces[i])
87 def display_grid(self, window = None, grid_sz = [80,80]):
89 return # I was considering implementing a text only display, then I thought "Fuck that"
91 # The indentation is getting seriously out of hand...
95 c = pygame.Color(200,200,200)
97 c = pygame.Color(64,64,64)
98 pygame.draw.rect(window, c, (x*grid_sz[0], y*grid_sz[1], (x+1)*grid_sz[0], (y+1)*grid_sz[1]))
100 def display_pieces(self, window = None, grid_sz = [80,80]):
103 for p in self.pieces["white"] + self.pieces["black"]:
104 p.draw(window, grid_sz, self.style)
106 # Draw the board in a pygame window
107 def display(self, window = None):
108 self.display_grid(window)
109 self.display_pieces(window)
117 if self.grid[x][y] == None:
119 if (self.grid[x][y].x != x or self.grid[x][y].y != y):
120 raise Exception(sys.argv[0] + ": MISMATCH " + str(self.grid[x][y]) + " should be at " + str(x) + "," + str(y))
122 # Select a piece on the board (colour is the colour of whoever is doing the selecting)
123 def select(self, x,y, colour=None):
124 if not self.on_board(x, y): # Get on board everyone!
125 raise Exception("BOUNDS")
127 piece = self.grid[x][y]
129 raise Exception("EMPTY")
131 if colour != None and piece.colour != colour:
132 raise Exception("COLOUR " + str(piece.colour) + " not " + str(colour))
134 # I'm not quite sure why I made this return a string, but screw logical design
135 return str(x) + " " + str(y) + " " + str(piece.select()) + " " + str(piece.current_type)
138 # Update the board when a piece has been selected
139 # "type" is apparently reserved, so I'll use "state"
140 def update_select(self, x, y, type_index, state):
141 piece = self.grid[x][y]
142 if piece.types[type_index] == "unknown":
143 if not state in self.unrevealed_types[piece.colour].keys():
144 raise Exception("SANITY: Too many " + piece.colour + " " + state + "s")
145 self.unrevealed_types[piece.colour][state] -= 1
146 if self.unrevealed_types[piece.colour][state] <= 0:
147 del self.unrevealed_types[piece.colour][state]
149 piece.types[type_index] = state
150 piece.types_revealed[type_index] = True
151 piece.current_type = state
153 if len(self.possible_moves(piece)) <= 0:
154 piece.deselect() # Piece can't move; deselect it
156 # Update the board when a piece has been moved
157 def update_move(self, x, y, x2, y2):
158 piece = self.grid[x][y]
159 self.grid[x][y] = None
160 taken = self.grid[x2][y2]
162 if taken.current_type == "king":
163 self.king[taken.colour] = None
164 self.pieces[taken.colour].remove(taken)
165 self.grid[x2][y2] = piece
169 # If the piece is a pawn, and it reaches the final row, it becomes a queen
170 # I know you are supposed to get a choice
171 # But that would be effort
172 if piece.current_type == "pawn" and ((piece.colour == "white" and piece.y == 0) or (piece.colour == "black" and piece.y == h-1)):
173 if self.style == "classical":
174 piece.types[0] = "queen"
175 piece.types[1] = "queen"
177 piece.types[piece.choice] = "queen"
178 piece.current_type = "queen"
180 piece.deselect() # Uncollapse (?) the wavefunction!
183 # Update the board from a string
184 # Guesses what to do based on the format of the string
185 def update(self, result):
186 #print "Update called with \"" + str(result) + "\""
187 # String always starts with 'x y'
189 s = result.split(" ")
190 [x,y] = map(int, s[0:2])
192 raise Exception("GIBBERISH \""+ str(result) + "\"") # Raise expectations
194 piece = self.grid[x][y]
196 raise Exception("EMPTY")
198 # If a piece is being moved, the third token is '->'
199 # We could get away with just using four integers, but that wouldn't look as cool
201 # Last two tokens are the destination
203 [x2,y2] = map(int, s[3:])
205 raise Exception("GIBBERISH \"" + str(result) + "\"") # Raise the alarm
207 # Move the piece (take opponent if possible)
208 self.update_move(x, y, x2, y2)
211 # Otherwise we will just assume a piece has been selected
213 type_index = int(s[2]) # We need to know which of the two types the piece is in; that's the third token
214 state = s[3] # The last token is a string identifying the type
216 raise Exception("GIBBERISH \"" + result + "\"") # Throw a hissy fit
219 self.update_select(x, y, type_index, state)
223 # Gets each piece that could reach the given square and the probability that it could reach that square
224 # Will include allied pieces that defend the attacker
225 def coverage(self, x, y, colour = None, reject_allied = True):
229 pieces = self.pieces["white"] + self.pieces["black"]
231 pieces = self.pieces[colour]
234 prob = self.probability_grid(p, reject_allied)[x][y]
236 result.update({p : prob})
245 # Associates each square with a probability that the piece could move into it
246 # Look, I'm doing all the hard work for you here...
247 def probability_grid(self, p, reject_allied = True):
249 result = [[0.0] * w for _ in range(h)]
250 if not isinstance(p, Piece):
253 if p.current_type != "unknown":
254 #sys.stderr.write(sys.argv[0] + ": " + str(p) + " moves " + str(self.possible_moves(p, reject_allied)) + "\n")
255 for point in self.possible_moves(p, reject_allied):
256 result[point[0]][point[1]] = 1.0
260 for i in range(len(p.types)):
263 if t == "unknown" or p.types_revealed[i] == False:
265 for t2 in self.unrevealed_types[p.colour].keys():
266 total_types += self.unrevealed_types[p.colour][t2]
268 for t2 in self.unrevealed_types[p.colour].keys():
269 prob2 = float(self.unrevealed_types[p.colour][t2]) / float(total_types)
271 for point in self.possible_moves(p, reject_allied):
272 result[point[0]][point[1]] += prob2 * prob
276 for point in self.possible_moves(p, reject_allied):
277 result[point[0]][point[1]] += prob
280 p.current_type = "unknown"
283 def prob_is_type(self, p, state):
286 for i in range(len(p.types)):
291 if t == "unknown" or p.types_revealed[i] == False:
293 for t2 in self.unrevealed_types[p.colour].keys():
294 total_prob += self.unrevealed_types[p.colour][t2]
295 for t2 in self.unrevealed_types[p.colour].keys():
297 result += prob * float(self.unrevealed_types[p.colour][t2]) / float(total_prob)
301 # Get all squares that the piece could move into
302 # This is probably inefficient, but I looked at some sample chess games and they seem to actually do things this way
303 # reject_allied indicates whether squares occupied by allied pieces will be removed
304 # (set to false to check for defense)
305 def possible_moves(self, p, reject_allied = True):
311 if p.current_type == "unknown":
312 raise Exception("SANITY: Piece state unknown")
313 # The below commented out code causes things to break badly
318 # result += self.possible_moves(p)
319 #p.current_type = "unknown"
322 if p.current_type == "king":
323 result = [[p.x-1,p.y],[p.x+1,p.y],[p.x,p.y-1],[p.x,p.y+1], [p.x-1,p.y-1],[p.x-1,p.y+1],[p.x+1,p.y-1],[p.x+1,p.y+1]]
324 elif p.current_type == "queen":
325 for d in [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]:
326 result += self.scan(p.x, p.y, d[0], d[1])
327 elif p.current_type == "bishop":
328 for d in [[-1,-1],[-1,1],[1,-1],[1,1]]: # There's a reason why bishops move diagonally
329 result += self.scan(p.x, p.y, d[0], d[1])
330 elif p.current_type == "rook":
331 for d in [[-1,0],[1,0],[0,-1],[0,1]]:
332 result += self.scan(p.x, p.y, d[0], d[1])
333 elif p.current_type == "knight":
334 # I would use two lines, but I'm not sure how python likes that
335 result = [[p.x-2, p.y-1], [p.x-2, p.y+1], [p.x+2, p.y-1], [p.x+2,p.y+1], [p.x-1,p.y-2], [p.x-1, p.y+2],[p.x+1,p.y-2],[p.x+1,p.y+2]]
336 elif p.current_type == "pawn":
337 if p.colour == "white":
339 # Pawn can't move forward into occupied square
340 if self.on_board(p.x, p.y-1) and self.grid[p.x][p.y-1] == None:
341 result = [[p.x,p.y-1]]
342 for f in [[p.x-1,p.y-1],[p.x+1,p.y-1]]:
343 if not self.on_board(f[0], f[1]):
345 if self.grid[f[0]][f[1]] != None: # Pawn can take diagonally
348 # Slightly embarrassing if the pawn jumps over someone on its first move...
349 if self.grid[p.x][p.y-1] == None and self.grid[p.x][p.y-2] == None:
350 result.append([p.x, p.y-2])
352 # Vice versa for the black pawn
353 if self.on_board(p.x, p.y+1) and self.grid[p.x][p.y+1] == None:
354 result = [[p.x,p.y+1]]
356 for f in [[p.x-1,p.y+1],[p.x+1,p.y+1]]:
357 if not self.on_board(f[0], f[1]):
359 if self.grid[f[0]][f[1]] != None:
360 #sys.stderr.write(sys.argv[0] + " : "+str(p) + " can take " + str(self.grid[f[0]][f[1]]) + "\n")
363 if self.grid[p.x][p.y+1] == None and self.grid[p.x][p.y+2] == None:
364 result.append([p.x, p.y+2])
366 #sys.stderr.write(sys.argv[0] + " : possible_moves for " + str(p) + " " + str(result) + "\n")
368 # Remove illegal moves
369 # Note: The result[:] creates a copy of result, so that the result.remove calls don't fuck things up
370 for point in result[:]:
372 if (point[0] < 0 or point[0] >= w) or (point[1] < 0 or point[1] >= h):
373 result.remove(point) # Remove locations outside the board
375 g = self.grid[point[0]][point[1]]
377 if g != None and (g.colour == p.colour and reject_allied == True):
378 result.remove(point) # Remove allied pieces
384 # Scans in a direction until it hits a piece, returns all squares in the line
385 # (includes the final square (which contains a piece), but not the original square)
386 def scan(self, x, y, vx, vy):
394 if not self.on_board(xx, yy):
398 g = self.grid[xx][yy]
406 # I typed the full statement about 30 times before writing this function...
407 def on_board(self, x, y):
408 return (x >= 0 and x < w) and (y >= 0 and y < h)