QWebchess work
[progcomp2013.git] / qchess / src / game.py
index 1aabe4a..15c53b1 100644 (file)
@@ -15,11 +15,10 @@ class GameThread(StoppableThread):
                self.cond = threading.Condition() # conditional for some reason, I forgot
                self.final_result = ""
                self.server = server
+               self.retry_illegal = False
                
                
-                       
-               
-               
+       
 
        # Run the game (run in new thread with start(), run in current thread with run())
        def run(self):
@@ -29,10 +28,11 @@ class GameThread(StoppableThread):
                        for p in self.players:
                                with self.lock:
                                        self.state["turn"] = p.base_player()
-                               #try:
-                               if True:
+                               try:
+                               #if True:
                                        [x,y] = p.select() # Player selects a square
                                        if self.stopped():
+                                               #debug("Quitting in select")
                                                break
                                                
                                        if isinstance(p, NetworkPlayer):
@@ -44,10 +44,18 @@ class GameThread(StoppableThread):
                                        else:
                                                result = self.board.select(x, y, colour = p.colour)
                                        
-                                       result = p.update(result)                                       
+                                       result = p.update(result)
+                                       if self.stopped():
+                                               break
                                        for p2 in self.players:
-                                               if p2 != p:
-                                                       p2.update(result) # Inform players of what happened
+                                               if p2 == p:
+                                                       continue
+                                               p2.update(result) # Inform players of what happened
+                                               if self.stopped():
+                                                       break
+                                       
+                                       if self.stopped():
+                                               break
 
 
                                        log(result)
@@ -71,12 +79,13 @@ class GameThread(StoppableThread):
                                                                graphics.state["dest"] = None
                                                continue
 
-                                       try:
-                                               [x2,y2] = p.get_move() # Player selects a destination
-                                       except:
-                                               self.stop()
+                                       #try:
+                                       [x2,y2] = p.get_move() # Player selects a destination
+                                       #except:
+                                       #       self.stop()
 
                                        if self.stopped():
+                                               #debug("Quitting in get_move")
                                                break
                                        
                                        if isinstance(p, NetworkPlayer):
@@ -90,10 +99,20 @@ class GameThread(StoppableThread):
                                                result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
                                                self.board.update_move(x, y, x2, y2)
                                        
-                                       result = p.update(result)                               
+                                       result = p.update(result)
+                                       if self.stopped():
+                                               break
                                        for p2 in self.players:
-                                               if p2 != p:
-                                                       p2.update(result) # Inform players of what happened
+                                               if p2 == p:
+                                                       continue
+                                               p2.update(result) # Inform players of what happened
+                                               if self.stopped():
+                                                       break
+                                       
+                                       if self.stopped():
+                                               break
+                                       
+                                       
                                                                                        
                                        log(result)
 
@@ -112,26 +131,32 @@ class GameThread(StoppableThread):
                                                        graphics.state["dest"] = None
                                                        graphics.state["moves"] = None
 
-                       # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
-                       #       except Exception,e:
-                       #               result = e.message
-                       #               #sys.stderr.write(result + "\n")
-                       #               
-                       #               self.stop()
-                       #               with self.lock:
-                       #                       self.final_result = self.state["turn"].colour + " " + e.message
-
-                               end = self.board.end_condition()
-                               if end != None:         
-                                       with self.lock:
-                                               if end == "DRAW":
-                                                       self.final_result = self.state["turn"].colour + " " + end
-                                               else:
-                                                       self.final_result = end
-                                       self.stop()
+                       
+                                       end = self.board.end_condition()
+                                       if end != None:         
+                                               with self.lock:
+                                                       if end == "DRAW":
+                                                               self.final_result = self.state["turn"].colour + " " + end
+                                                       else:
+                                                               self.final_result = end
+                                               self.stop()
                                
-                               if self.stopped():
-                                       break
+                                       if self.stopped():
+                                               break
+                               except Exception,e:
+                               #if False:
+
+                                       
+                                       result = e.message
+                                       if self.retry_illegal:
+                                               self.state["turn"].update(result);
+                                       else:
+                                               sys.stderr.write("qchess.py exception: "+result + "\n")
+                                               self.stop()
+                                               with self.lock:
+                                                       self.final_result = self.state["turn"].colour + " " + e.message
+                                               break
+
 
 
                for p2 in self.players:

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