Got cgi script to work
[progcomp2013.git] / qchess / src / board.py
index a9825d0..adcf429 100644 (file)
@@ -15,6 +15,7 @@ class Board():
                self.king = {"white" : None, "black" : None} # We need to keep track of the king, because he is important
                self.max_moves = None
                self.moves = 0
+               self.move_stack = []
                for c in ["black", "white"]:
                        del self.unrevealed_types[c]["unknown"]
 
@@ -156,7 +157,7 @@ class Board():
        # Select a piece on the board (colour is the colour of whoever is doing the selecting)
        def select(self, x,y, colour=None):
                if not self.on_board(x, y): # Get on board everyone!
-                       raise Exception("BOUNDS")
+                       raise Exception("BOUNDS " + str(x) + ","+str(y))
 
                piece = self.grid[x][y]
                if piece == None:
@@ -171,10 +172,11 @@ class Board():
 
        # Update the board when a piece has been selected
        # "type" is apparently reserved, so I'll use "state"
-       def update_select(self, x, y, type_index, state):
+       def update_select(self, x, y, type_index, state, sanity=True, deselect=True):
+               #debug(str(self) + " update_select called")
                piece = self.grid[x][y]
                if piece.types[type_index] == "unknown":
-                       if not state in self.unrevealed_types[piece.colour].keys():
+                       if not state in self.unrevealed_types[piece.colour].keys() and sanity == True:
                                raise Exception("SANITY: Too many " + piece.colour + " " + state + "s")
                        self.unrevealed_types[piece.colour][state] -= 1
                        if self.unrevealed_types[piece.colour][state] <= 0:
@@ -183,12 +185,21 @@ class Board():
                piece.types[type_index] = state
                piece.current_type = state
 
-               if len(self.possible_moves(piece)) <= 0:
+               if deselect == True and len(self.possible_moves(piece)) <= 0:
                        piece.deselect() # Piece can't move; deselect it
+                       
+               # Piece needs to recalculate moves
+               piece.possible_moves = None
                
        # Update the board when a piece has been moved
-       def update_move(self, x, y, x2, y2):
+       def update_move(self, x, y, x2, y2, sanity=True):
+               #debug(str(self) + " update_move called \""+str(x)+ " " + str(y) + " -> " + str(x2) + " " + str(y2) + "\"")     
                piece = self.grid[x][y]
+               #print "Moving " + str(x) + "," + str(y) + " to " + str(x2) + "," + str(y2) + "; possible_moves are " + str(self.possible_moves(piece))
+               
+               if not [x2,y2] in self.possible_moves(piece) and sanity == True:
+                       raise Exception("ILLEGAL move " + str(x2)+","+str(y2))
+               
                self.grid[x][y] = None
                taken = self.grid[x2][y2]
                if taken != None:
@@ -212,12 +223,17 @@ class Board():
 
                piece.deselect() # Uncollapse (?) the wavefunction!
                self.moves += 1
+               
+               # All other pieces need to recalculate moves
+               for p in self.pieces["white"] + self.pieces["black"]:
+                       p.possible_moves = None
+               
                #self.verify()  
 
        # Update the board from a string
        # Guesses what to do based on the format of the string
-       def update(self, result):
-               #print "Update called with \"" + str(result) + "\""
+       def update(self, result, sanity=True, deselect=True):
+               #debug(str(self) + " update called \""+str(result)+"\"")
                # String always starts with 'x y'
                try:
                        s = result.split(" ")
@@ -226,8 +242,8 @@ class Board():
                        raise Exception("GIBBERISH \""+ str(result) + "\"") # Raise expectations
 
                piece = self.grid[x][y]
-               if piece == None:
-                       raise Exception("EMPTY")
+               if piece == None and sanity == True:
+                       raise Exception("EMPTY " + str(x) + " " + str(y))
 
                # If a piece is being moved, the third token is '->'
                # We could get away with just using four integers, but that wouldn't look as cool
@@ -239,7 +255,7 @@ class Board():
                                raise Exception("GIBBERISH \"" + str(result) + "\"") # Raise the alarm
 
                        # Move the piece (take opponent if possible)
-                       self.update_move(x, y, x2, y2)
+                       self.update_move(x, y, x2, y2, sanity)
                        
                else:
                        # Otherwise we will just assume a piece has been selected
@@ -249,8 +265,9 @@ class Board():
                        except:
                                raise Exception("GIBBERISH \"" + result + "\"") # Throw a hissy fit
 
+
                        # Select the piece
-                       self.update_select(x, y, type_index, state)
+                       self.update_select(x, y, type_index, state, sanity=sanity, deselect=deselect)
 
                return result
 
@@ -269,7 +286,7 @@ class Board():
                        if prob > 0:
                                result.update({p : prob})
                
-               self.verify()
+               #self.verify()
                return result
 
 
@@ -293,7 +310,7 @@ class Board():
                
                for i in range(len(p.types)):
                        t = p.types[i]
-                       prob = 0.5
+                       prob = 1.0 / float(len(p.types))
                        if t == "unknown" or p.types[i][0] == '?':
                                total_types = 0
                                for t2 in self.unrevealed_types[p.colour].keys():
@@ -301,20 +318,26 @@ class Board():
                                
                                for t2 in self.unrevealed_types[p.colour].keys():
                                        prob2 = float(self.unrevealed_types[p.colour][t2]) / float(total_types)
-                                       p.current_type = t2
-                                       for point in self.possible_moves(p, reject_allied):
+                                       #p.current_type = t2
+                                       for point in self.possible_moves(p, reject_allied, state=t2):
                                                result[point[0]][point[1]] += prob2 * prob
                                
                        else:
-                               p.current_type = t
-                               for point in self.possible_moves(p, reject_allied):
-                                       result[point[0]][point[1]] += prob
+                               #p.current_type = t
+                               for point in self.possible_moves(p, reject_allied, state=t):
+                                               result[point[0]][point[1]] += prob
                
-               self.verify()
-               p.current_type = "unknown"
+               #self.verify()
+               #p.current_type = "unknown"
                return result
 
        def prob_is_type(self, p, state):
+               if p.current_type != 0:
+                       if state == p.current_type:
+                               return 1.0
+                       else:
+                               return 0.0
+               
                prob = 0.5
                result = 0
                for i in range(len(p.types)):
@@ -336,14 +359,28 @@ class Board():
        # This is probably inefficient, but I looked at some sample chess games and they seem to actually do things this way
        # reject_allied indicates whether squares occupied by allied pieces will be removed
        # (set to false to check for defense)
-       def possible_moves(self, p, reject_allied = True):
-               result = []
+       def possible_moves(self, p, reject_allied = True, state=None):
                if p == None:
+                       raise Exception("SANITY: No piece")
+               
+               
+               
+               if state != None and state != p.current_type:
+                       old_type = p.current_type
+                       p.current_type = state
+                       result = self.possible_moves(p, reject_allied, state=None)
+                       p.current_type = old_type
                        return result
+               
+               
+               
+               
+               result = []
+               
 
                
                if p.current_type == "unknown":
-                       raise Exception("SANITY: Piece state unknown")
+                       raise Exception("SANITY: Unknown state for piece: "+str(p))
                        # The below commented out code causes things to break badly
                        #for t in p.types:
                        #       if t == "unknown":
@@ -411,7 +448,9 @@ class Board():
                        if g != None and (g.colour == p.colour and reject_allied == True):
                                result.remove(point) # Remove allied pieces
                
-               self.verify()
+               #self.verify()
+               
+               p.possible_moves = result
                return result
 
 
@@ -453,3 +492,33 @@ class Board():
        # I typed the full statement about 30 times before writing this function...
        def on_board(self, x, y):
                return (x >= 0 and x < w) and (y >= 0 and y < h)
+       
+       
+       
+       # Pushes a move temporarily
+       def push_move(self, piece, x, y):
+               target = self.grid[x][y]
+               self.move_stack.append([piece, target, piece.x, piece.y, x, y])
+               [piece.x, piece.y] = [x, y]
+               self.grid[x][y] = piece
+               self.grid[piece.x][piece.y] = None
+               
+               for p in self.pieces["white"] + self.pieces["black"]:
+                       p.possible_moves = None
+               
+       # Restore move
+       def pop_move(self):
+               #print str(self.move_stack)
+               [piece, target, x1, y1, x2, y2] = self.move_stack[len(self.move_stack)-1]
+               self.move_stack = self.move_stack[:-1]
+               piece.x = x1
+               piece.y = y1
+               self.grid[x1][y1] = piece
+               if target != None:
+                       target.x = x2
+                       target.y = y2
+               self.grid[x2][y2] = target
+               
+               for p in self.pieces["white"] + self.pieces["black"]:
+                       p.possible_moves = None
+               

UCC git Repository :: git.ucc.asn.au