3 # Do you know what the -u does? It unbuffers stdin and stdout
4 # I can't remember why, but last year things broke without that
7 UCC::Progcomp 2013 Quantum Chess game
8 @author Sam Moore [SZM] "matches"
9 @copyright The University Computer Club, Incorporated
10 (ie: You can copy it for not for profit purposes)
13 # system python modules or whatever they are called
19 [game, graphics] = [None, None]
21 def make_player(name, colour):
23 if name[1:] == "human":
24 return HumanPlayer(name, colour)
25 s = name[1:].split(":")
30 return NetworkReceiver(colour, address)
33 return AgentPlayer(name, colour)
37 # The main function! It does the main stuff!
40 # Apparently python will silently treat things as local unless you do this
41 # Anyone who says "You should never use a global variable" can die in a fire
55 graphics_enabled = True
59 while i < len(argv)-1:
63 players.append(make_player(arg, colour))
66 elif colour == "black":
69 sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n")
72 # Option parsing goes here
73 if arg[1] == '-' and arg[2:] == "classical":
75 elif arg[1] == '-' and arg[2:] == "quantum":
77 elif (arg[1] == '-' and arg[2:] == "graphics"):
78 graphics_enabled = not graphics_enabled
79 elif (arg[1] == '-' and arg[2:].split("=")[0] == "file"):
81 if len(arg[2:].split("=")) == 1:
84 src_file = arg[2:].split("=")[1]
85 elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
87 if len(arg[2:].split("=")) == 1:
90 log_file = arg[2:].split("=")[1]
91 elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"):
93 if len(arg[2:].split("=")) == 1:
96 turn_delay = float(arg[2:].split("=")[1])
98 elif (arg[1] == '-' and arg[2:].split("=")[0] == "timeout"):
100 if len(arg[2:].split("=")) == 1:
103 agent_timeout = float(arg[2:].split("=")[1])
105 elif (arg[1] == '-' and arg[2:] == "help"):
107 os.system("less data/help.txt") # The best help function
116 if graphics_enabled == True:
118 graphics = GraphicsThread(board, grid_sz = [64,64]) # Construct a GraphicsThread!
122 sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
123 graphics_enabled = False
125 # If there are no players listed, display a nice pretty menu
126 if len(players) != 2:
128 players = graphics.SelectPlayers(players)
130 sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
133 # If there are still no players, quit
134 if players == None or len(players) != 2:
135 sys.stderr.write(sys.argv[0] + " : Graphics window closed before players chosen\n")
139 # Wrap NetworkSender players around original players if necessary
140 for i in range(len(players)):
141 if isinstance(players[i], NetworkReceiver):
142 players[i].board = board # Network players need direct access to the board
143 for j in range(len(players)):
146 if isinstance(players[j], NetworkSender) or isinstance(players[j], NetworkReceiver):
148 players[j] = NetworkSender(players[j], players[i].address)
149 players[j].board = board
151 # Connect the networked players
153 if isinstance(p, NetworkSender) or isinstance(p, NetworkReceiver):
155 graphics.board.display_grid(graphics.window, graphics.grid_sz)
156 graphics.message("Connecting to " + p.colour + " player...")
160 # If using windows, select won't work; use horrible TimeoutPlayer hack
161 if agent_timeout > 0 and platform.system() == "Windows":
162 sys.stderr.write(sys.argv[0] + " : Warning - You are using Windows\n")
163 sys.stderr.write(sys.argv[0] + " : - Timeouts will be implemented with a terrible hack.\n")
165 for i in range(len(players)):
166 if isinstance(players[i], AgentPlayer):
167 players[i] = TimeoutPlayer(players[i], agent_timeout)
169 # Could potentially wrap TimeoutPlayer around internal classes...
170 # But that would suck
176 # Construct a GameThread! Make it global! Damn the consequences!
177 game = GameThread(board, players)
182 game.start() # This runs in a new thread
185 return game.error + graphics.error
190 # This is how python does a main() function...
191 if __name__ == "__main__":
192 sys.exit(main(sys.argv))