It works!
[progcomp2013.git] / qchess / src / game.py
index 934c043..de2439b 100644 (file)
@@ -1,4 +1,8 @@
 
+
+
+       
+
 # A thread that runs the game
 class GameThread(StoppableThread):
        def __init__(self, board, players):
@@ -10,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):
@@ -36,6 +42,7 @@ class GameThread(StoppableThread):
                                                p2.update(result) # Inform players of what happened
 
 
+                                       log(result)
 
                                        target = self.board.grid[x][y]
                                        if isinstance(graphics, GraphicsThread):
@@ -64,9 +71,15 @@ class GameThread(StoppableThread):
                                        if self.stopped():
                                                break
 
-                                       result = self.board.update_move(x, y, x2, y2)
+                                       result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
+                                       log(result)
+
+                                       self.board.update_move(x, y, x2, y2)
+                                       
                                        for p2 in self.players:
-                                               p2.update(str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)) # Inform players of what happened
+                                               p2.update(result) # Inform players of what happened
+
+                                                                               
 
                                        if isinstance(graphics, GraphicsThread):
                                                with graphics.lock:
@@ -110,10 +123,164 @@ class GameThread(StoppableThread):
                for p2 in self.players:
                        p2.quit(self.final_result)
 
-               graphics.stop()
+               log(self.final_result)
+
+               if isinstance(graphics, GraphicsThread):
+                       graphics.stop()
+
+       
+# A thread that replays a log file
+class ReplayThread(GameThread):
+       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.reset_board(self.src.readline())
+
+       def reset_board(self, line):
+               pieces = {"white" : [], "black" : []}
+               king = {"white" : None, "black" : None}
+               grid = [[None] * w for _ in range(h)]
+               for x in range(w):
+                       for y in range(h):
+                               self.board.grid[x][y] = None
+               while line != "# Start game":
+                       if line[0] == "#":
+                               line = self.src.readline().strip(" \r\n")
+                               continue
+
+                       tokens = line.split(" ")
+                       [x, y] = map(int, tokens[len(tokens)-1].split(","))
+                       current_type = tokens[1]
+                       types = map(lambda e : e.strip("'[], "), (tokens[2]+tokens[3]).split(","))
+                       
+                       target = Piece(tokens[0], x, y, types)
+                       target.current_type = current_type
+                       
+                       try:
+                               target.choice = types.index(current_type)
+                       except:
+                               target.choice = -1
+
+                       pieces[tokens[0]].append(target)
+                       if target.current_type == "king":
+                               king[tokens[0]] = target
+                       grid[x][y] = target
+               
+                       line = self.src.readline().strip(" \r\n")
+
+               self.board.pieces = pieces
+               self.board.king = king
+               self.board.grid = grid
 
+               # Update the player's boards
        
+       def run(self):
+               move_count = 0
+               line = self.src.readline().strip(" \r\n")
+               while line != "# EOF":
+                       if self.stopped():
+                               break
+
+                                       
+
+                       if line[0] == '#':
+                               line = self.src.readline().strip(" \r\n")
+                               continue
+
+                       tokens = line.split(" ")
+                       if tokens[0] == "white" or tokens[0] == "black":
+                               self.reset_board(line)
+                               line = self.src.readline().strip(" \r\n")
+                               continue
+
+                       move = line.split(":")
+                       move = move[len(move)-1].strip(" \r\n")
+                       tokens = move.split(" ")
+                       
+                       
+                       try:
+                               [x,y] = map(int, tokens[0:2])
+                       except:
+                               self.stop()
+                               break
+
+                       log(move)
+
+                       target = self.board.grid[x][y]
+                       with self.lock:
+                               if target.colour == "white":
+                                       self.state["turn"] = self.players[0]
+                               else:
+                                       self.state["turn"] = self.players[1]
+                       
+                       move_piece = (tokens[2] == "->")
+                       if move_piece:
+                               [x2,y2] = map(int, tokens[len(tokens)-2:])
+
+                       if isinstance(graphics, GraphicsThread):
+                               with graphics.lock:
+                                       graphics.state["select"] = target
+                                       
+                       if not move_piece:
+                               self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
+                               if isinstance(graphics, GraphicsThread):
+                                       with graphics.lock:
+                                               graphics.state["moves"] = self.board.possible_moves(target)
+                                       time.sleep(turn_delay)
+                       else:
+                               self.board.update_move(x, y, x2, y2)
+                               if isinstance(graphics, GraphicsThread):
+                                       with graphics.lock:
+                                               graphics.state["moves"] = [[x2,y2]]
+                                       time.sleep(turn_delay)
+                                       with graphics.lock:
+                                               graphics.state["select"] = None
+                                               graphics.state["moves"] = None
+                                               graphics.state["dest"] = None
+                       
+
+                       
+                       
+                       
+                       for p in self.players:
+                               p.update(move)
+
+                       line = self.src.readline().strip(" \r\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()
+               
 
+               
 
 def opponent(colour):
        if colour == "white":

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