Make sure the board reflects the state BEFORE the move is made
[progcomp2013.git] / qchess / src / game.py
index e3f1c7c..b3c7dd3 100644 (file)
@@ -1,12 +1,5 @@
 
 
-log_file = None
-
-def log(s):
-       if log_file != None:
-               import datetime
-               log_file.write(str(datetime.datetime.now()) + " : " + s + "\n")
-
 
        
 
@@ -21,6 +14,8 @@ class GameThread(StoppableThread):
                self.lock = threading.RLock() #lock for access of self.state
                self.cond = threading.Condition() # conditional for some reason, I forgot
                self.final_result = ""
+               
+               
 
        # Run the game (run in new thread with start(), run in current thread with run())
        def run(self):
@@ -132,20 +127,58 @@ class GameThread(StoppableThread):
        
 # A thread that replays a log file
 class ReplayThread(GameThread):
-       def __init__(self, players, src):
-               self.board = Board(style="agent")
+       def __init__(self, players, src, end=False,max_lines=None):
+               self.board = Board(style="empty")
                GameThread.__init__(self, self.board, players)
                self.src = src
+               self.max_lines = max_lines
+               self.line_number = 0
+               self.end = end
+
+               self.setup()
 
-               self.ended = False
+       def setup(self):
+               sys.stderr.write("setup called for ReplayThread\n")
+               if True:
+                       while self.src.readline().strip(" \r\n") != "# Initial board":
+                               self.line_number += 1
+               
+                       line = self.src.readline().strip(" \r\n")
+                       
+                       while line != "# Start game":
+                               #print "Reading line " + str(line)
+                               self.line_number += 1
+                               [x,y] = map(int, line.split("at")[1].strip(" \r\n").split(","))
+                               colour = line.split(" ")[0]
+                               current_type = line.split(" ")[1]
+                               types = map(lambda e : e.strip(" [],'"), line.split(" ")[2:4])
+                               p = Piece(colour, x, y, types)
+                               if current_type != "unknown":
+                                       p.current_type = current_type
+                                       p.choice = types.index(current_type)
+
+                               self.board.pieces[colour].append(p)
+                               self.board.grid[x][y] = p
+                               if current_type == "king":
+                                       self.board.king[colour] = p
+
+                               line = self.src.readline().strip(" \r\n")
+                               
+               #except Exception, e:
+               #       raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message)
        
        def run(self):
                i = 0
                phase = 0
-               for line in self.src:
+               count = 0
+               line = self.src.readline().strip(" \r\n")
+               while line != "# EOF":
+                       sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " read: " + str(line) + "\n")
+                       count += 1
+                       if self.max_lines != None and count > self.max_lines:
+                               self.stop()
 
                        if self.stopped():
-                               self.ended = True
                                break
 
                        with self.lock:
@@ -153,17 +186,18 @@ class ReplayThread(GameThread):
 
                        line = line.split(":")
                        result = line[len(line)-1].strip(" \r\n")
-                       log(result)
+                       
 
                        try:
                                self.board.update(result)
-                       except:
-                               self.ended = True
+                       except Exception, e:
+                               sys.stderr.write("Exception! " + str(e.message) + "\n")
                                self.final_result = result
-                               if isinstance(graphics, GraphicsThread):
-                                       graphics.stop()
+                               self.stop()
                                break
 
+                       log(result)
+
                        [x,y] = map(int, result.split(" ")[0:2])
                        target = self.board.grid[x][y]
 
@@ -173,14 +207,16 @@ class ReplayThread(GameThread):
                                                graphics.state["moves"] = self.board.possible_moves(target)
                                                graphics.state["select"] = target
 
-                                       time.sleep(turn_delay)
+                                       if self.end:
+                                               time.sleep(turn_delay)
 
                                elif phase == 1:
                                        [x2,y2] = map(int, result.split(" ")[3:5])
                                        with graphics.lock:
                                                graphics.state["moves"] = [[x2,y2]]
 
-                                       time.sleep(turn_delay)
+                                       if self.end:
+                                               time.sleep(turn_delay)
 
                                        with graphics.lock:
                                                graphics.state["select"] = None
@@ -197,6 +233,23 @@ class ReplayThread(GameThread):
                        phase = (phase + 1) % 2
                        if phase == 0:
                                i = (i + 1) % 2
+                       
+                       line = self.src.readline().strip(" \r\n")
+
+               sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " finished...\n")
+
+               if self.max_lines != None and self.max_lines > count:
+                       sys.stderr.write(sys.argv[0] + " : Replaying from file; stopping at last line (" + str(count) + ")\n")
+                       sys.stderr.write(sys.argv[0] + " : (You requested line " + str(self.max_lines) + ")\n")
+
+               if self.end and isinstance(graphics, GraphicsThread):
+                       #graphics.stop()
+                       pass # Let the user stop the display
+               elif not self.end:
+                       global game
+                       game = GameThread(self.board, self.players)
+                       game.run()
+               
 
                
 

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