Lots of stuff happened
[progcomp2013.git] / qchess / qchess.py
index db73c94..501b3fd 100755 (executable)
@@ -1,4 +1,101 @@
 #!/usr/bin/python -u
+# +++ piece.py +++ #
+import random
+
+# I know using non-abreviated strings is inefficient, but this is python, who cares?
+# Oh, yeah, this stores the number of pieces of each type in a normal chess game
+piece_types = {"pawn" : 8, "bishop" : 2, "knight" : 2, "rook" : 2, "queen" : 1, "king" : 1, "unknown" : 0}
+
+# Class to represent a quantum chess piece
+class Piece():
+       def __init__(self, colour, x, y, types):
+               self.colour = colour # Colour (string) either "white" or "black"
+               self.x = x # x coordinate (0 - 8), none of this fancy 'a', 'b' shit here
+               self.y = y # y coordinate (0 - 8)
+               self.types = types # List of possible types the piece can be (should just be two)
+               self.current_type = "unknown" # Current type
+               self.choice = -1 # Index of the current type in self.types (-1 = unknown type)
+               self.types_revealed = [True, False] # Whether the types are known (by default the first type is always known at game start)
+               
+
+               # 
+               self.last_state = None
+               self.move_pattern = None
+
+               
+
+       def init_from_copy(self, c):
+               self.colour = c.colour
+               self.x = c.x
+               self.y = c.y
+               self.types = c.types[:]
+               self.current_type = c.current_type
+               self.choice = c.choice
+               self.types_revealed = c.types_revealed[:]
+
+               self.last_state = None
+               self.move_pattern = None
+
+       
+
+       # Make a string for the piece (used for debug)
+       def __str__(self):
+               return str(self.current_type) + " " + str(self.types) + " at " + str(self.x) + ","+str(self.y)  
+
+       # Draw the piece in a pygame surface
+       def draw(self, window, grid_sz = [80,80], style="quantum"):
+
+               # First draw the image corresponding to self.current_type
+               img = images[self.colour][self.current_type]
+               rect = img.get_rect()
+               if style == "classical":
+                       offset = [-rect.width/2, -rect.height/2]
+               else:
+                       offset = [-rect.width/2,-3*rect.height/4] 
+               window.blit(img, (self.x * grid_sz[0] + grid_sz[0]/2 + offset[0], self.y * grid_sz[1] + grid_sz[1]/2 + offset[1]))
+               
+               
+               if style == "classical":
+                       return
+
+               # Draw the two possible types underneath the current_type image
+               for i in range(len(self.types)):
+                       if self.types_revealed[i] == True:
+                               img = small_images[self.colour][self.types[i]]
+                       else:
+                               img = small_images[self.colour]["unknown"] # If the type hasn't been revealed, show a placeholder
+
+                       
+                       rect = img.get_rect()
+                       offset = [-rect.width/2,-rect.height/2] 
+                       
+                       if i == 0:
+                               target = (self.x * grid_sz[0] + grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])                             
+                       else:
+                               target = (self.x * grid_sz[0] + 4*grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])                           
+                               
+                       window.blit(img, target) # Blit shit
+       
+       # Collapses the wave function!          
+       def select(self):
+               if self.current_type == "unknown":
+                       self.choice = random.randint(0,1)
+                       self.current_type = self.types[self.choice]
+                       self.types_revealed[self.choice] = True
+               return self.choice
+
+       # Uncollapses (?) the wave function!
+       def deselect(self):
+               #print "Deselect called"
+               if (self.x + self.y) % 2 != 0:
+                       if (self.types[0] != self.types[1]) or (self.types_revealed[0] == False or self.types_revealed[1] == False):
+                               self.current_type = "unknown"
+                               self.choice = -1
+                       else:
+                               self.choice = 0 # Both the two types are the same
+
+       # The sad moment when you realise that you do not understand anything about a subject you studied for 4 years...
+# --- piece.py --- #
 # +++ board.py +++ #
 [w,h] = [8,8] # Width and height of board(s)
 
@@ -98,7 +195,7 @@ class Board():
                if window == None:
                        return
                for p in self.pieces["white"] + self.pieces["black"]:
-                       p.draw(window, grid_sz)
+                       p.draw(window, grid_sz, self.style)
 
        # Draw the board in a pygame window
        def display(self, window = None):
@@ -126,7 +223,7 @@ class Board():
                        raise Exception("EMPTY")
 
                if colour != None and piece.colour != colour:
-                       raise Exception("COLOUR")
+                       raise Exception("COLOUR " + str(piece.colour) + " not " + str(colour))
 
                # I'm not quite sure why I made this return a string, but screw logical design
                return str(x) + " " + str(y) + " " + str(piece.select()) + " " + str(piece.current_type)
@@ -404,107 +501,511 @@ class Board():
        def on_board(self, x, y):
                return (x >= 0 and x < w) and (y >= 0 and y < h)
 # --- board.py --- #
-# +++ game.py +++ #
+# +++ player.py +++ #
+import subprocess
+import select
+import platform
 
-# A thread that runs the game
-class GameThread(StoppableThread):
-       def __init__(self, board, players):
-               StoppableThread.__init__(self)
-               self.board = board
-               self.players = players
-               self.state = {"turn" : None} # The game state
-               self.error = 0 # Whether the thread exits with an error
-               self.lock = threading.RLock() #lock for access of self.state
-               self.cond = threading.Condition() # conditional for some reason, I forgot
-               self.final_result = ""
+agent_timeout = -1.0 # Timeout in seconds for AI players to make moves
+                       # WARNING: Won't work for windows based operating systems
 
-       # Run the game (run in new thread with start(), run in current thread with run())
-       def run(self):
-               result = ""
-               while not self.stopped():
-                       
-                       for p in self.players:
-                               with self.lock:
-                                       self.state["turn"] = p # "turn" contains the player who's turn it is
-                               #try:
-                               if True:
-                                       [x,y] = p.select() # Player selects a square
-                                       if self.stopped():
-                                               break
+if platform.system() == "Windows":
+       agent_timeout = -1 # Hence this
 
-                                       result = self.board.select(x, y, colour = p.colour)                             
-                                       for p2 in self.players:
-                                               p2.update(result) # Inform players of what happened
+# A player who can't play
+class Player():
+       def __init__(self, name, colour):
+               self.name = name
+               self.colour = colour
 
+# Player that runs from another process
+class AgentPlayer(Player):
 
 
-                                       target = self.board.grid[x][y]
-                                       if isinstance(graphics, GraphicsThread):
-                                               with graphics.lock:
-                                                       graphics.state["moves"] = self.board.possible_moves(target)
-                                                       graphics.state["select"] = target
+       def __init__(self, name, colour):
+               Player.__init__(self, name, colour)
+               self.p = subprocess.Popen(name, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
+               
+               self.send_message(colour)
 
-                                       time.sleep(turn_delay)
+       def send_message(self, s):
+               if agent_timeout > 0.0:
+                       ready = select.select([], [self.p.stdin], [], agent_timeout)[1]
+               else:
+                       ready = [self.p.stdin]
+               if self.p.stdin in ready:
+                       #print "Writing to p.stdin"
+                       try:
+                               self.p.stdin.write(s + "\n")
+                       except:
+                               raise Exception("UNRESPONSIVE")
+               else:
+                       raise Exception("UNRESPONSIVE")
 
+       def get_response(self):
+               if agent_timeout > 0.0:
+                       ready = select.select([self.p.stdout], [], [], agent_timeout)[0]
+               else:
+                       ready = [self.p.stdout]
+               if self.p.stdout in ready:
+                       #print "Reading from p.stdout"
+                       try:
+                               return self.p.stdout.readline().strip("\r\n")
+                       except: # Exception, e:
+                               raise Exception("UNRESPONSIVE")
+               else:
+                       raise Exception("UNRESPONSIVE")
 
-                                       if len(self.board.possible_moves(target)) == 0:
-                                               #print "Piece cannot move"
-                                               target.deselect()
-                                               if isinstance(graphics, GraphicsThread):
-                                                       with graphics.lock:
-                                                               graphics.state["moves"] = None
-                                                               graphics.state["select"] = None
-                                                               graphics.state["dest"] = None
-                                               continue
+       def select(self):
 
-                                       try:
-                                               [x2,y2] = p.get_move() # Player selects a destination
-                                       except:
-                                               self.stop()
+               self.send_message("SELECTION?")
+               line = self.get_response()
+               
+               try:
+                       result = map(int, line.split(" "))
+               except:
+                       raise Exception("GIBBERISH \"" + str(line) + "\"")
+               return result
 
-                                       if self.stopped():
-                                               break
+       def update(self, result):
+               #print "Update " + str(result) + " called for AgentPlayer"
+               self.send_message(result)
 
-                                       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
 
-                                       if isinstance(graphics, GraphicsThread):
-                                               with graphics.lock:
-                                                       graphics.state["moves"] = [[x2,y2]]
+       def get_move(self):
+               
+               self.send_message("MOVE?")
+               line = self.get_response()
+               
+               try:
+                       result = map(int, line.split(" "))
+               except:
+                       raise Exception("GIBBERISH \"" + str(line) + "\"")
+               return result
 
-                                       time.sleep(turn_delay)
+       def quit(self, final_result):
+               try:
+                       self.send_message("QUIT " + final_result)
+               except:
+                       self.p.kill()
 
-                                       if isinstance(graphics, GraphicsThread):
-                                               with graphics.lock:
-                                                       graphics.state["select"] = None
-                                                       graphics.state["dest"] = None
-                                                       graphics.state["moves"] = None
+# So you want to be a player here?
+class HumanPlayer(Player):
+       def __init__(self, name, colour):
+               Player.__init__(self, name, colour)
+               
+       # Select your preferred account
+       def select(self):
+               if isinstance(graphics, GraphicsThread):
+                       # Basically, we let the graphics thread do some shit and then return that information to the game thread
+                       graphics.cond.acquire()
+                       # We wait for the graphics thread to select a piece
+                       while graphics.stopped() == False and graphics.state["select"] == None:
+                               graphics.cond.wait() # The difference between humans and machines is that humans sleep
+                       select = graphics.state["select"]
+                       
+                       
+                       graphics.cond.release()
+                       if graphics.stopped():
+                               return [-1,-1]
+                       return [select.x, select.y]
+               else:
+                       # Since I don't display the board in this case, I'm not sure why I filled it in...
+                       while True:
+                               sys.stdout.write("SELECTION?\n")
+                               try:
+                                       p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
+                               except:
+                                       sys.stderr.write("ILLEGAL GIBBERISH\n")
+                                       continue
+       # It's your move captain
+       def get_move(self):
+               if isinstance(graphics, GraphicsThread):
+                       graphics.cond.acquire()
+                       while graphics.stopped() == False and graphics.state["dest"] == None:
+                               graphics.cond.wait()
+                       graphics.cond.release()
+                       
+                       return graphics.state["dest"]
+               else:
 
-                       # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
-                               #except Exception,e:
-                                       #result = "ILLEGAL " + e.message
-                                       #sys.stderr.write(result + "\n")
-                                       
-                                       #self.stop()
-                                       #with self.lock:
-                                       #       self.final_result = self.state["turn"].colour + " " + "ILLEGAL"
+                       while True:
+                               sys.stdout.write("MOVE?\n")
+                               try:
+                                       p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
+                               except:
+                                       sys.stderr.write("ILLEGAL GIBBERISH\n")
+                                       continue
 
-                               if self.board.king["black"] == None:
-                                       if self.board.king["white"] == None:
-                                               with self.lock:
-                                                       self.final_result = "DRAW"
-                                       else:
-                                               with self.lock:
-                                                       self.final_result = "white"
-                                       self.stop()
-                               elif self.board.king["white"] == None:
-                                       with self.lock:
-                                               self.final_result = "black"
-                                       self.stop()
-                                               
+       # Are you sure you want to quit?
+       def quit(self, final_result):
+               if graphics == None:            
+                       sys.stdout.write("QUIT " + final_result + "\n")
 
-                               if self.stopped():
+       # Completely useless function
+       def update(self, result):
+               if isinstance(graphics, GraphicsThread):
+                       pass
+               else:
+                       sys.stdout.write(result + "\n") 
+
+
+# Player that makes random moves
+class AgentRandom(Player):
+       def __init__(self, name, colour):
+               Player.__init__(self, name, colour)
+               self.choice = None
+
+               self.board = Board(style = "agent")
+
+       def select(self):
+               while True:
+                       self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
+                       all_moves = []
+                       # Check that the piece has some possibility to move
+                       tmp = self.choice.current_type
+                       if tmp == "unknown": # For unknown pieces, try both types
+                               for t in self.choice.types:
+                                       if t == "unknown":
+                                               continue
+                                       self.choice.current_type = t
+                                       all_moves += self.board.possible_moves(self.choice)
+                       else:
+                               all_moves = self.board.possible_moves(self.choice)
+                       self.choice.current_type = tmp
+                       if len(all_moves) > 0:
+                               break
+               return [self.choice.x, self.choice.y]
+
+       def get_move(self):
+               moves = self.board.possible_moves(self.choice)
+               move = moves[random.randint(0, len(moves)-1)]
+               return move
+
+       def update(self, result):
+               #sys.stderr.write(sys.argv[0] + " : Update board for AgentRandom\n")
+               self.board.update(result)
+               self.board.verify()
+
+       def quit(self, final_result):
+               pass
+# --- player.py --- #
+# +++ network.py +++ #
+import socket
+import select
+
+network_timeout_start = -1.0 # Timeout in seconds to wait for the start of a message
+network_timeout_delay = 1.0 # Maximum time between two characters being received
+
+class Network():
+       def __init__(self, colour, address = None):
+               self.socket = socket.socket()
+               #self.socket.setblocking(0)
+
+               if colour == "white":
+                       self.port = 4562
+               else:
+                       self.port = 4563
+
+               self.src = None
+
+       #       print str(self) + " listens on port " + str(self.port)
+
+               if address == None:
+                       self.host = socket.gethostname()
+                       self.socket.bind((self.host, self.port))
+                       self.socket.listen(5)   
+
+                       self.src, self.address = self.socket.accept()
+                       self.src.send("ok\n")
+                       if self.get_response() == "QUIT":
+                               self.src.close()
+               else:
+                       self.host = address
+                       self.socket.connect((address, self.port))
+                       self.src = self.socket
+                       self.src.send("ok\n")
+                       if self.get_response() == "QUIT":
+                               self.src.close()
+
+       def get_response(self):
+               # Timeout the start of the message (first character)
+               if network_timeout_start > 0.0:
+                       ready = select.select([self.src], [], [], network_timeout_start)[0]
+               else:
+                       ready = [self.src]
+               if self.src in ready:
+                       s = self.src.recv(1)
+               else:
+                       raise Exception("UNRESPONSIVE")
+
+
+               while s[len(s)-1] != '\n':
+                       # Timeout on each character in the message
+                       if network_timeout_delay > 0.0:
+                               ready = select.select([self.src], [], [], network_timeout_delay)[0]
+                       else:
+                               ready = [self.src]
+                       if self.src in ready:
+                               s += self.src.recv(1) 
+                       else:
+                               raise Exception("UNRESPONSIVE")
+
+               return s.strip(" \r\n")
+
+       def send_message(self,s):
+               if network_timeout_start > 0.0:
+                       ready = select.select([], [self.src], [], network_timeout_start)[1]
+               else:
+                       ready = [self.src]
+
+               if self.src in ready:
+                       self.src.send(s + "\n")
+               else:
+                       raise Exception("UNRESPONSIVE")
+
+       def check_quit(self, s):
+               s = s.split(" ")
+               if s[0] == "QUIT":
+                       with game.lock:
+                               game.final_result = " ".join(s[1:]) + " " + str(opponent(self.colour))
+                       game.stop()
+                       return True
+
+               
+
+class NetworkSender(Player,Network):
+       def __init__(self, base_player, address = None):
+               self.base_player = base_player
+               Player.__init__(self, base_player.name, base_player.colour)
+
+               self.address = address
+
+       def connect(self):
+               Network.__init__(self, self.base_player.colour, self.address)
+
+
+
+       def select(self):
+               [x,y] = self.base_player.select()
+               choice = self.board.grid[x][y]
+               s = str(x) + " " + str(y)
+               #print str(self) + ".select sends " + s
+               self.send_message(s)
+               return [x,y]
+
+       def get_move(self):
+               [x,y] = self.base_player.get_move()
+               s = str(x) + " " + str(y)
+               #print str(self) + ".get_move sends " + s
+               self.send_message(s)
+               return [x,y]
+
+       def update(self, s):
+               self.base_player.update(s)
+               s = s.split(" ")
+               [x,y] = map(int, s[0:2])
+               selected = self.board.grid[x][y]
+               if selected != None and selected.colour == self.colour and len(s) > 2 and not "->" in s:
+                       s = " ".join(s[0:3])
+                       for i in range(2):
+                               if selected.types_revealed[i] == True:
+                                       s += " " + str(selected.types[i])
+                               else:
+                                       s += " unknown"
+                       #print str(self) + ".update sends " + s
+                       self.send_message(s)
+                               
+
+       def quit(self, final_result):
+               self.base_player.quit(final_result)
+               #self.src.send("QUIT " + str(final_result) + "\n")
+               self.src.close()
+
+class NetworkReceiver(Player,Network):
+       def __init__(self, colour, address=None):
+               
+               Player.__init__(self, address, colour)
+
+               self.address = address
+
+               self.board = None
+
+       def connect(self):
+               Network.__init__(self, self.colour, self.address)
+                       
+
+       def select(self):
+               
+               s = self.get_response()
+               #print str(self) + ".select gets " + s
+               [x,y] = map(int,s.split(" "))
+               if x == -1 and y == -1:
+                       #print str(self) + ".select quits the game"
+                       with game.lock:
+                               game.final_state = "network terminated " + self.colour
+                       game.stop()
+               return [x,y]
+       def get_move(self):
+               s = self.get_response()
+               #print str(self) + ".get_move gets " + s
+               [x,y] = map(int,s.split(" "))
+               if x == -1 and y == -1:
+                       #print str(self) + ".get_move quits the game"
+                       with game.lock:
+                               game.final_state = "network terminated " + self.colour
+                       game.stop()
+               return [x,y]
+
+       def update(self, result):
+               
+               result = result.split(" ")
+               [x,y] = map(int, result[0:2])
+               selected = self.board.grid[x][y]
+               if selected != None and selected.colour == self.colour and len(result) > 2 and not "->" in result:
+                       s = self.get_response()
+                       #print str(self) + ".update - receives " + str(s)
+                       s = s.split(" ")
+                       selected.choice = int(s[2])
+                       for i in range(2):
+                               selected.types[i] = str(s[3+i])
+                               if s[3+i] == "unknown":
+                                       selected.types_revealed[i] = False
+                               else:
+                                       selected.types_revealed[i] = True
+                       selected.current_type = selected.types[selected.choice] 
+               else:
+                       pass
+                       #print str(self) + ".update - ignore result " + str(result)                     
+               
+
+       def quit(self, final_result):
+               self.src.close()
+       
+# --- network.py --- #
+# +++ thread_util.py +++ #
+import threading
+
+# A thread that can be stopped!
+# Except it can only be stopped if it checks self.stopped() periodically
+# So it can sort of be stopped
+class StoppableThread(threading.Thread):
+       def __init__(self):
+               threading.Thread.__init__(self)
+               self._stop = threading.Event()
+
+       def stop(self):
+               self._stop.set()
+
+       def stopped(self):
+               return self._stop.isSet()
+# --- thread_util.py --- #
+# +++ game.py +++ #
+
+# A thread that runs the game
+class GameThread(StoppableThread):
+       def __init__(self, board, players):
+               StoppableThread.__init__(self)
+               self.board = board
+               self.players = players
+               self.state = {"turn" : None} # The game state
+               self.error = 0 # Whether the thread exits with an error
+               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):
+               result = ""
+               while not self.stopped():
+                       
+                       for p in self.players:
+                               with self.lock:
+                                       if isinstance(p, NetworkSender):
+                                               self.state["turn"] = p.base_player # "turn" contains the player who's turn it is
+                                       else:
+                                               self.state["turn"] = p
+                               try:
+                                       [x,y] = p.select() # Player selects a square
+                                       if self.stopped():
+                                               break
+
+                                       
+                                               
+
+                                       result = self.board.select(x, y, colour = p.colour)                             
+                                       for p2 in self.players:
+                                               p2.update(result) # Inform players of what happened
+
+
+
+                                       target = self.board.grid[x][y]
+                                       if isinstance(graphics, GraphicsThread):
+                                               with graphics.lock:
+                                                       graphics.state["moves"] = self.board.possible_moves(target)
+                                                       graphics.state["select"] = target
+
+                                       time.sleep(turn_delay)
+
+
+                                       if len(self.board.possible_moves(target)) == 0:
+                                               #print "Piece cannot move"
+                                               target.deselect()
+                                               if isinstance(graphics, GraphicsThread):
+                                                       with graphics.lock:
+                                                               graphics.state["moves"] = None
+                                                               graphics.state["select"] = None
+                                                               graphics.state["dest"] = None
+                                               continue
+
+                                       try:
+                                               [x2,y2] = p.get_move() # Player selects a destination
+                                       except:
+                                               self.stop()
+
+                                       if self.stopped():
+                                               break
+
+                                       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
+
+                                       if isinstance(graphics, GraphicsThread):
+                                               with graphics.lock:
+                                                       graphics.state["moves"] = [[x2,y2]]
+
+                                       time.sleep(turn_delay)
+
+                                       if isinstance(graphics, GraphicsThread):
+                                               with graphics.lock:
+                                                       graphics.state["select"] = None
+                                                       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
+
+                               if self.board.king["black"] == None:
+                                       if self.board.king["white"] == None:
+                                               with self.lock:
+                                                       self.final_result = self.state["turn"].colour + " DRAW"
+                                       else:
+                                               with self.lock:
+                                                       self.final_result = "white"
+                                       self.stop()
+                               elif self.board.king["white"] == None:
+                                       with self.lock:
+                                               self.final_result = "black"
+                                       self.stop()
+                                               
+
+                               if self.stopped():
                                        break
 
 
@@ -596,7 +1097,10 @@ class GraphicsThread(StoppableThread):
                                if event.type == pygame.QUIT:
                                        if isinstance(game, GameThread):
                                                with game.lock:
-                                                       game.final_result = "terminated"
+                                                       game.final_result = ""
+                                                       if game.state["turn"] != None:
+                                                               game.final_result = game.state["turn"].colour + " "
+                                                       game.final_result += "terminated"
                                                game.stop()
                                        self.stop()
                                        break
@@ -797,348 +1301,339 @@ class GraphicsThread(StoppableThread):
                pygame.display.flip()
 
        def getstr(self, prompt = None):
+               s = pygame.Surface((self.window.get_width(), self.window.get_height()))
+               s.blit(self.window, (0,0))
                result = ""
+
                while True:
                        #print "LOOP"
                        if prompt != None:
                                self.message(prompt)
                                self.message(result, pos = (0, 1))
        
+                       pygame.event.pump()
                        for event in pygame.event.get():
+                               if event.type == pygame.QUIT:
+                                       return None
                                if event.type == pygame.KEYDOWN:
-                                       if chr(event.key) == '\r':
-                                               return result
-                                       result += str(chr(event.key))
-# --- graphics.py --- #
-# +++ main.py +++ #
-#!/usr/bin/python -u
-
-# Do you know what the -u does? It unbuffers stdin and stdout
-# I can't remember why, but last year things broke without that
-
-"""
-       UCC::Progcomp 2013 Quantum Chess game
-       @author Sam Moore [SZM] "matches"
-       @copyright The University Computer Club, Incorporated
-               (ie: You can copy it for not for profit purposes)
-"""
+                                       if event.key == pygame.K_BACKSPACE:
+                                               result = result[0:len(result)-1]
+                                               self.window.blit(s, (0,0)) # Revert the display
+                                               continue
+                               
+                                               
+                                       try:
+                                               if event.unicode == '\r':
+                                                       return result
+                                       
+                                               result += str(event.unicode)
+                                       except:
+                                               continue
 
-# system python modules or whatever they are called
-import sys
-import os
-import time
 
-turn_delay = 0.5
-[game, graphics] = [None, None]
+       # Function to pick a button
+       def SelectButton(self, choices, prompt = None, font_size=32):
+               self.board.display_grid(self.window, self.grid_sz)
+               if prompt != None:
+                       self.message(prompt)
+               font = pygame.font.Font(None, font_size)
+               targets = []
+               sz = self.window.get_size()
 
+               
+               for i in range(len(choices)):
+                       c = choices[i]
+                       
+                       text = font.render(c, 1, pygame.Color(0,0,0))
+                       p = (sz[0] / 2 - (1.5*text.get_width())/2, sz[1] / 2 +(i-1)*text.get_height()+(i*2))
+                       targets.append((p[0], p[1], p[0] + 1.5*text.get_width(), p[1] + text.get_height()))
 
-# The main function! It does the main stuff!
-def main(argv):
-
-       # Apparently python will silently treat things as local unless you do this
-       # But (here's the fun part), only if you actually modify the variable.
-       # For example, all those 'if graphics_enabled' conditions work in functions that never say it is global
-       # Anyone who says "You should never use a global variable" can die in a fire
-       global game
-       global graphics
-
-       # Magical argument parsing goes here
-       if len(argv) == 1:
-               players = [HumanPlayer("saruman", "white"), AgentRandom("sabbath", "black")]
-       elif len(argv) == 2:
-               players = [AgentPlayer(argv[1], "white"), HumanPlayer("shadow", "black"), ]
-       elif len(argv) == 3:
-               players = [AgentPlayer(argv[1], "white"), AgentPlayer(argv[2], "black")]
-
-       # Construct the board!
-       board = Board(style = "quantum")
-       game = GameThread(board, players) # Construct a GameThread! Make it global! Damn the consequences!
-       #try:
-       if True:
-               graphics = GraphicsThread(board, grid_sz = [64,64]) # Construct a GraphicsThread! I KNOW WHAT I'M DOING! BEAR WITH ME!
-               game.start() # This runs in a new thread
-       #except NameError:
-       #       print "Run game in main thread"
-       #       game.run() # Run game in the main thread (no need for joining)
-       #       return game.error
-       #except Exception, e:
-       #       raise e
-       #else:
-       #       print "Normal"
-               graphics.run()
-               game.join()
-               return game.error + graphics.error
-
-
-# This is how python does a main() function...
-if __name__ == "__main__":
-       sys.exit(main(sys.argv))
-# --- main.py --- #
-# +++ piece.py +++ #
-import random
-
-# I know using non-abreviated strings is inefficient, but this is python, who cares?
-# Oh, yeah, this stores the number of pieces of each type in a normal chess game
-piece_types = {"pawn" : 8, "bishop" : 2, "knight" : 2, "rook" : 2, "queen" : 1, "king" : 1, "unknown" : 0}
-
-# Class to represent a quantum chess piece
-class Piece():
-       def __init__(self, colour, x, y, types):
-               self.colour = colour # Colour (string) either "white" or "black"
-               self.x = x # x coordinate (0 - 8), none of this fancy 'a', 'b' shit here
-               self.y = y # y coordinate (0 - 8)
-               self.types = types # List of possible types the piece can be (should just be two)
-               self.current_type = "unknown" # Current type
-               self.choice = -1 # Index of the current type in self.types (-1 = unknown type)
-               self.types_revealed = [True, False] # Whether the types are known (by default the first type is always known at game start)
-               
-
-               # 
-               self.last_state = None
-               self.move_pattern = None
+               while True:
+                       mp =pygame.mouse.get_pos()
+                       for i in range(len(choices)):
+                               c = choices[i]
+                               if mp[0] > targets[i][0] and mp[0] < targets[i][2] and mp[1] > targets[i][1] and mp[1] < targets[i][3]:
+                                       font_colour = pygame.Color(255,0,0)
+                                       box_colour = pygame.Color(0,0,255,128)
+                               else:
+                                       font_colour = pygame.Color(0,0,0)
+                                       box_colour = pygame.Color(128,128,128)
+                               
+                               text = font.render(c, 1, font_colour)
+                               s = pygame.Surface((text.get_width()*1.5, text.get_height()), pygame.SRCALPHA)
+                               s.fill(box_colour)
+                               pygame.draw.rect(s, (0,0,0), (0,0,1.5*text.get_width(), text.get_height()), self.grid_sz[0]/10)
+                               s.blit(text, ((text.get_width()*1.5)/2 - text.get_width()/2 ,0))
+                               self.window.blit(s, targets[i][0:2])
+                               
+       
+                       pygame.display.flip()
 
+                       for event in pygame.event.get():
+                               if event.type == pygame.QUIT:
+                                       return None
+                               elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
+                                       for i in range(len(targets)):
+                                               t = targets[i]
+                                               if event.pos[0] > t[0] and event.pos[0] < t[2]:
+                                                       if event.pos[1] > t[1] and event.pos[1] < t[3]:
+                                                               return i
+                                               #print "Reject " + str(i) + str(event.pos) + " vs " + str(t)
                
 
-       def init_from_copy(self, c):
-               self.colour = c.colour
-               self.x = c.x
-               self.y = c.y
-               self.types = c.types[:]
-               self.current_type = c.current_type
-               self.choice = c.choice
-               self.types_revealed = c.types_revealed[:]
+       # Function to pick players in a nice GUI way
+       def SelectPlayers(self, players = []):
 
-               self.last_state = None
-               self.move_pattern = None
-
-       
-
-       # Make a string for the piece (used for debug)
-       def __str__(self):
-               return str(self.current_type) + " " + str(self.types) + " at " + str(self.x) + ","+str(self.y)  
-
-       # Draw the piece in a pygame surface
-       def draw(self, window, grid_sz = [80,80]):
 
-               # First draw the image corresponding to self.current_type
-               img = images[self.colour][self.current_type]
-               rect = img.get_rect()
-               offset = [-rect.width/2,-3*rect.height/4] 
-               window.blit(img, (self.x * grid_sz[0] + grid_sz[0]/2 + offset[0], self.y * grid_sz[1] + grid_sz[1]/2 + offset[1]))
-               
                
-               # Draw the two possible types underneath the current_type image
-               for i in range(len(self.types)):
-                       if self.types_revealed[i] == True:
-                               img = small_images[self.colour][self.types[i]]
-                       else:
-                               img = small_images[self.colour]["unknown"] # If the type hasn't been revealed, show a placeholder
+               missing = ["white", "black"]
+               for p in players:
+                       missing.remove(p.colour)
 
+               for colour in missing:
                        
-                       rect = img.get_rect()
-                       offset = [-rect.width/2,-rect.height/2] 
                        
-                       if i == 0:
-                               target = (self.x * grid_sz[0] + grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])                             
+                       choice = self.SelectButton(["human", "agent", "network"],prompt = "Choose " + str(colour) + " player", font_size=32)
+                       if choice == 0:
+                               players.append(HumanPlayer("human", colour))
+                       elif choice == 1:
+                               try:
+                                       import Tkinter
+                                       from tkFileDialog import askopenfilename
+                                       root = Tkinter.Tk() # Need a root to make Tkinter behave
+                                       root.withdraw() # Some sort of magic incantation
+                                       path = askopenfilename(parent=root, initialdir="../agents",title=
+'Choose an agent.')
+                                       if path == "":
+                                               return self.SelectPlayers()
+                                       players.append(make_player(path, colour))       
+                               except Exception,e:
+                                       print "Exception was " + str(e.message)
+                                       p = None
+                                       while p == None:
+                                               self.board.display_grid(self.window, self.grid_sz)
+                                               pygame.display.flip()
+                                               path = self.getstr(prompt = "Enter path:")
+                                               if path == None:
+                                                       return None
+
+                                               if path == "":
+                                                       return self.SelectPlayers()
+
+                                               try:
+                                                       p = make_player(path, colour)
+                                               except:
+                                                       self.board.display_grid(self.window, self.grid_sz)
+                                                       pygame.display.flip()
+                                                       self.message("Invalid path!")
+                                                       time.sleep(1)
+                                                       p = None
+                                       players.append(p)
+                       elif choice == 2:
+                               address = ""
+                               while address == "":
+                                       self.board.display_grid(self.window, self.grid_sz)
+                                       
+                                       address = self.getstr(prompt = "Address? (leave blank for server)")
+                                       if address == None:
+                                               return None
+                                       if address == "":
+                                               address = None
+                                               continue
+                                       try:
+                                               map(int, address.split("."))
+                                       except:
+                                               self.board.display_grid(self.window, self.grid_sz)
+                                               self.message("Invalid IPv4 address!")
+                                               address = ""
+
+                               players.append(NetworkReceiver(colour, address))
                        else:
-                               target = (self.x * grid_sz[0] + 4*grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])                           
+                               return None
+               #print str(self) + ".SelectPlayers returns " + str(players)
+               return players
+                       
                                
-                       window.blit(img, target) # Blit shit
-       
-       # Collapses the wave function!          
-       def select(self):
-               if self.current_type == "unknown":
-                       self.choice = random.randint(0,1)
-                       self.current_type = self.types[self.choice]
-                       self.types_revealed[self.choice] = True
-               return self.choice
+                       
+# --- graphics.py --- #
+# +++ main.py +++ #
+#!/usr/bin/python -u
 
-       # Uncollapses (?) the wave function!
-       def deselect(self):
-               #print "Deselect called"
-               if (self.x + self.y) % 2 != 0:
-                       if (self.types[0] != self.types[1]) or (self.types_revealed[0] == False or self.types_revealed[1] == False):
-                               self.current_type = "unknown"
-                               self.choice = -1
-                       else:
-                               self.choice = 0 # Both the two types are the same
+# Do you know what the -u does? It unbuffers stdin and stdout
+# I can't remember why, but last year things broke without that
 
-       # The sad moment when you realise that you do not understand anything about a subject you studied for 4 years...
-# --- piece.py --- #
-# +++ player.py +++ #
-import subprocess
+"""
+       UCC::Progcomp 2013 Quantum Chess game
+       @author Sam Moore [SZM] "matches"
+       @copyright The University Computer Club, Incorporated
+               (ie: You can copy it for not for profit purposes)
+"""
 
+# system python modules or whatever they are called
+import sys
+import os
+import time
 
+turn_delay = 0.5
+[game, graphics] = [None, None]
 
-# A player who can't play
-class Player():
-       def __init__(self, name, colour):
-               self.name = name
-               self.colour = colour
+def make_player(name, colour):
+       if name[0] == '@':
+               if name[1:] == "human":
+                       return HumanPlayer(name, colour)
+               s = name[1:].split(":")
+               if s[0] == "network":
+                       address = None
+                       if len(s) > 1:
+                               address = s[1]
+                       return NetworkReceiver(colour, address)
 
-# Player that runs from another process
-class AgentPlayer(Player):
-       def __init__(self, name, colour):
-               Player.__init__(self, name, colour)
-               self.p = subprocess.Popen(name, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=sys.stderr)
-               try:
-                       self.p.stdin.write(colour + "\n")
-               except:
-                       raise Exception("UNRESPONSIVE")
+       else:
+               return AgentPlayer(name, colour)
+                       
 
-       def select(self):
-               
-               #try:
-               self.p.stdin.write("SELECTION?\n")
-               line = self.p.stdout.readline().strip("\r\n ")
-               #except:
-               #       raise Exception("UNRESPONSIVE")
-               try:
-                       result = map(int, line.split(" "))
-               except:
-                       raise Exception("GIBBERISH \"" + str(line) + "\"")
-               return result
 
-       def update(self, result):
-               #print "Update " + str(result) + " called for AgentPlayer"
-#              try:
-               self.p.stdin.write(result + "\n")
-#              except:
-#              raise Exception("UNRESPONSIVE")
+# The main function! It does the main stuff!
+def main(argv):
 
-       def get_move(self):
-               
-               try:
-                       self.p.stdin.write("MOVE?\n")
-                       line = self.p.stdout.readline().strip("\r\n ")
-               except:
-                       raise Exception("UNRESPONSIVE")
-               try:
-                       result = map(int, line.split(" "))
-               except:
-                       raise Exception("GIBBERISH \"" + str(line) + "\"")
-               return result
+       # Apparently python will silently treat things as local unless you do this
+       # Anyone who says "You should never use a global variable" can die in a fire
+       global game
+       global graphics
+       
+       global turn_delay
+       global agent_timeout
+       global log_file
+       global src_file
 
-       def quit(self, final_result):
-               try:
-                       self.p.stdin.write("QUIT " + final_result + "\n")
-               except:
-                       self.p.kill()
 
-# So you want to be a player here?
-class HumanPlayer(Player):
-       def __init__(self, name, colour):
-               Player.__init__(self, name, colour)
-               
-       # Select your preferred account
-       def select(self):
-               if isinstance(graphics, GraphicsThread):
-                       # Basically, we let the graphics thread do some shit and then return that information to the game thread
-                       graphics.cond.acquire()
-                       # We wait for the graphics thread to select a piece
-                       while graphics.stopped() == False and graphics.state["select"] == None:
-                               graphics.cond.wait() # The difference between humans and machines is that humans sleep
-                       select = graphics.state["select"]
-                       
-                       
-                       graphics.cond.release()
-                       if graphics.stopped():
-                               return [-1,-1]
-                       return [select.x, select.y]
-               else:
-                       # Since I don't display the board in this case, I'm not sure why I filled it in...
-                       while True:
-                               sys.stdout.write("SELECTION?\n")
-                               try:
-                                       p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
-                               except:
-                                       sys.stderr.write("ILLEGAL GIBBERISH\n")
-                                       continue
-       # It's your move captain
-       def get_move(self):
-               if isinstance(graphics, GraphicsThread):
-                       graphics.cond.acquire()
-                       while graphics.stopped() == False and graphics.state["dest"] == None:
-                               graphics.cond.wait()
-                       graphics.cond.release()
-                       
-                       return graphics.state["dest"]
-               else:
 
-                       while True:
-                               sys.stdout.write("MOVE?\n")
-                               try:
-                                       p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
-                               except:
-                                       sys.stderr.write("ILLEGAL GIBBERISH\n")
-                                       continue
+       
+       style = "quantum"
+       colour = "white"
+       graphics_enabled = True
+
+       players = []
+       i = 0
+       while i < len(argv)-1:
+               i += 1
+               arg = argv[i]
+               if arg[0] != '-':
+                       players.append(make_player(arg, colour))
+                       if colour == "white":
+                               colour = "black"
+                       elif colour == "black":
+                               pass
+                       else:
+                               sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n")
+                       continue
+
+               # Option parsing goes here
+               if arg[1] == '-' and arg[2:] == "classical":
+                       style = "classical"
+               elif arg[1] == '-' and arg[2:] == "quantum":
+                       style = "quantum"
+               elif (arg[1] == '-' and arg[2:] == "graphics"):
+                       graphics_enabled = not graphics_enabled
+               elif (arg[1] == '-' and arg[2:].split("=")[0] == "file"):
+                       # Load game from file
+                       if len(arg[2:].split("=")) == 1:
+                               src_file = sys.stdout
+                       else:
+                               src_file = arg[2:].split("=")[1]
+               elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
+                       # Log file
+                       if len(arg[2:].split("=")) == 1:
+                               log_file = sys.stdout
+                       else:
+                               log_file = arg[2:].split("=")[1]
+               elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"):
+                       # Delay
+                       if len(arg[2:].split("=")) == 1:
+                               turn_delay = 0
+                       else:
+                               turn_delay = float(arg[2:].split("=")[1])
+
+               elif (arg[1] == '-' and arg[2:].split("=")[0] == "timeout"):
+                       # Timeout
+                       if len(arg[2:].split("=")) == 1:
+                               agent_timeout = -1
+                       elif platform.system() != "Windows": # Windows breaks this option
+                               agent_timeout = float(arg[2:].split("=")[1])
+                       else:
+                               sys.stderr.write(sys.argv[0] + " : Warning - You are using Windows\n")
+                               agent_timeout = -1
+                               
+               elif (arg[1] == '-' and arg[2:] == "help"):
+                       # Help
+                       os.system("less data/help.txt") # The best help function
+                       return 0
 
-       # Are you sure you want to quit?
-       def quit(self, final_result):
-               sys.stdout.write("QUIT " + final_result + "\n")
 
-       # Completely useless function
-       def update(self, result):
-               if isinstance(graphics, GraphicsThread):
-                       pass
-               else:
-                       sys.stdout.write(result + "\n") 
+       # Create the board
+       board = Board(style)
 
 
-# Player that makes random moves
-class AgentRandom(Player):
-       def __init__(self, name, colour):
-               Player.__init__(self, name, colour)
-               self.choice = None
+       # Initialise GUI
+       if graphics_enabled == True:
+               try:
+                       graphics = GraphicsThread(board, grid_sz = [64,64]) # Construct a GraphicsThread!
+               except Exception,e:
+                       graphics = None
+                       sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
+                       graphics_enabled = False
+
+       # If there are no players listed, display a nice pretty menu
+       if len(players) != 2:
+               if graphics != None:
+                       players = graphics.SelectPlayers(players)
+               else:
+                       sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
+                       return 44
 
-               self.board = Board(style = "agent")
+       # If there are still no players, quit
+       if players == None or len(players) != 2:
+               sys.stderr.write(sys.argv[0] + " : Graphics window closed before players chosen\n")
+               return 45
 
-       def select(self):
-               while True:
-                       self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
-                       all_moves = []
-                       # Check that the piece has some possibility to move
-                       tmp = self.choice.current_type
-                       if tmp == "unknown": # For unknown pieces, try both types
-                               for t in self.choice.types:
-                                       if t == "unknown":
-                                               continue
-                                       self.choice.current_type = t
-                                       all_moves += self.board.possible_moves(self.choice)
-                       else:
-                               all_moves = self.board.possible_moves(self.choice)
-                       self.choice.current_type = tmp
-                       if len(all_moves) > 0:
-                               break
-               return [self.choice.x, self.choice.y]
 
-       def get_move(self):
-               moves = self.board.possible_moves(self.choice)
-               move = moves[random.randint(0, len(moves)-1)]
-               return move
+       # Wrap NetworkSender players around original players if necessary
+       for i in range(len(players)):
+               if isinstance(players[i], NetworkReceiver):
+                       players[i].board = board # Network players need direct access to the board
+                       for j in range(len(players)):
+                               if j == i:
+                                       continue
+                               if isinstance(players[j], NetworkSender) or isinstance(players[j], NetworkReceiver):
+                                       continue
+                               players[j] = NetworkSender(players[j], players[i].address)
+                               players[j].board = board
 
-       def update(self, result):
-               #sys.stderr.write(sys.argv[0] + " : Update board for AgentRandom\n")
-               self.board.update(result)
-               self.board.verify()
+       # Connect the networked players
+       for p in players:
+               if isinstance(p, NetworkSender) or isinstance(p, NetworkReceiver):
+                       if graphics != None:
+                               graphics.board.display_grid(graphics.window, graphics.grid_sz)
+                               graphics.message("Connecting to " + p.colour + " player...")
+                       p.connect()
 
-       def quit(self, final_result):
-               pass
-# --- player.py --- #
-# +++ thread_util.py +++ #
-import threading
 
-# A thread that can be stopped!
-# Except it can only be stopped if it checks self.stopped() periodically
-# So it can sort of be stopped
-class StoppableThread(threading.Thread):
-       def __init__(self):
-               threading.Thread.__init__(self)
-               self._stop = threading.Event()
+       # Construct a GameThread! Make it global! Damn the consequences!
+       game = GameThread(board, players) 
 
-       def stop(self):
-               self._stop.set()
 
-       def stopped(self):
-               return self._stop.isSet()
-# --- thread_util.py --- #
+       
+       if graphics != None:
+               game.start() # This runs in a new thread
+               graphics.run()
+               game.join()
+               return game.error + graphics.error
+       else:
+               game.run()
+               return game.error
+
+# This is how python does a main() function...
+if __name__ == "__main__":
+       sys.exit(main(sys.argv))
+# --- main.py --- #
+# EOF - created from make on Thu Jan 24 17:04:54 WST 2013

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