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
20 [game, graphics] = [None, None]
22 def make_player(name, colour):
24 if name[1:] == "human":
25 return HumanPlayer(name, colour)
26 s = name[1:].split(":")
31 return NetworkReceiver(colour, address)
32 if s[0] == "internal":
35 internal_agents = inspect.getmembers(sys.modules[__name__], inspect.isclass)
36 internal_agents = [x for x in internal_agents if issubclass(x[1], InternalAgent)]
37 internal_agents.remove(('InternalAgent', InternalAgent))
40 sys.stderr.write(sys.argv[0] + " : '@internal' should be followed by ':' and an agent name\n")
41 sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
44 for a in internal_agents:
46 return a[1](name, colour)
48 sys.stderr.write(sys.argv[0] + " : Can't find an internal agent matching \"" + s[1] + "\"\n")
49 sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
54 return ExternalAgent(name, colour)
58 # The main function! It does the main stuff!
61 # Apparently python will silently treat things as local unless you do this
62 # Anyone who says "You should never use a global variable" can die in a fire
70 global graphics_enabled
71 global always_reveal_states
80 # Get the important warnings out of the way
81 if platform.system() == "Windows":
82 sys.stderr.write(sys.argv[0] + " : Warning - You are using " + platform.system() + "\n")
83 if platform.release() == "Vista":
84 sys.stderr.write(sys.argv[0] + " : God help you.\n")
89 while i < len(argv)-1:
93 p = make_player(arg, colour)
94 if not isinstance(p, Player):
95 sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n")
100 elif colour == "black":
103 sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n")
106 # Option parsing goes here
107 if arg[1] == '-' and arg[2:] == "classical":
109 elif arg[1] == '-' and arg[2:] == "quantum":
111 elif arg[1] == '-' and arg[2:] == "reveal":
112 always_reveal_states = True
113 elif (arg[1] == '-' and arg[2:] == "graphics"):
114 graphics_enabled = True
115 elif (arg[1] == '-' and arg[2:] == "no-graphics"):
116 graphics_enabled = False
117 elif (arg[1] == '-' and arg[2:].split("=")[0] == "file"):
118 # Load game from file
119 if len(arg[2:].split("=")) == 1:
122 f = arg[2:].split("=")[1]
123 if f[0:7] == "http://":
124 src_file = HttpReplay(f)
126 src_file = FileReplay(f.split(":")[0])
128 if len(f.split(":")) == 2:
129 max_moves = int(f.split(":")[1])
131 elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
133 if len(arg[2:].split("=")) == 1:
134 log_files.append(LogFile(sys.stdout))
136 f = arg[2:].split("=")[1]
138 log_files.append(ShortLog(f[1:]))
140 log_files.append(LogFile(open(f, "w", 0)))
141 elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"):
143 if len(arg[2:].split("=")) == 1:
146 turn_delay = float(arg[2:].split("=")[1])
148 elif (arg[1] == '-' and arg[2:].split("=")[0] == "timeout"):
150 if len(arg[2:].split("=")) == 1:
153 agent_timeout = float(arg[2:].split("=")[1])
154 elif (arg[1] == '-' and arg[2:].split("=")[0] == "blackout"):
156 if len(arg[2:].split("=")) == 1:
159 sleep_timeout = float(arg[2:].split("=")[1])
161 elif (arg[1] == '-' and arg[2:] == "help"):
163 os.system("less data/help.txt") # The best help function
169 # Construct a GameThread! Make it global! Damn the consequences!
172 # Hack to stop ReplayThread from exiting
173 #if len(players) == 0:
174 # players = [HumanPlayer("dummy", "white"), HumanPlayer("dummy", "black")]
176 # Normally the ReplayThread exits if there are no players
177 # TODO: Decide which behaviour to use, and fix it
178 end = (len(players) == 0)
180 players = [Player("dummy", "white"), Player("dummy", "black")]
181 elif len(players) != 2:
182 sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
184 sys.stderr.write(sys.argv[0] + " : (You won't get a GUI, because --file was used, and the author is lazy)\n")
186 game = ReplayThread(players, src_file, end=end, max_moves=max_moves)
189 board.max_moves = max_moves
190 game = GameThread(board, players)
196 if graphics_enabled == True:
198 graphics = GraphicsThread(game.board, grid_sz = [64,64]) # Construct a GraphicsThread!
200 graphics.sleep_timeout = sleep_timeout
204 sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
205 graphics_enabled = False
207 # If there are no players listed, display a nice pretty menu
208 if len(players) != 2:
210 players = graphics.SelectPlayers(players)
212 sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
215 # If there are still no players, quit
216 if players == None or len(players) != 2:
217 sys.stderr.write(sys.argv[0] + " : Graphics window closed before players chosen\n")
221 # Wrap NetworkSender players around original players if necessary
222 for i in range(len(players)):
223 if isinstance(players[i], NetworkReceiver):
224 players[i].board = board # Network players need direct access to the board
225 for j in range(len(players)):
228 if isinstance(players[j], NetworkSender) or isinstance(players[j], NetworkReceiver):
230 players[j] = NetworkSender(players[j], players[i].address)
231 players[j].board = board
233 # Connect the networked players
235 if isinstance(p, NetworkSender) or isinstance(p, NetworkReceiver):
237 graphics.board.display_grid(graphics.window, graphics.grid_sz)
238 graphics.message("Connecting to " + p.colour + " player...")
240 # Handle race condition by having clients wait longer than servers to connect
241 if p.address != None:
246 # If using windows, select won't work; use horrible TimeoutPlayer hack
247 if agent_timeout > 0:
248 if platform.system() == "Windows":
249 for i in range(len(players)):
250 if isinstance(players[i], ExternalAgent) or isinstance(players[i], InternalAgent):
251 players[i] = TimeoutPlayer(players[i], agent_timeout)
255 # InternalAgents get wrapped to an ExternalAgent when there is a timeout
256 # This is not confusing at all.
257 for i in range(len(players)):
258 if isinstance(players[i], InternalAgent):
259 players[i] = ExternalWrapper(players[i])
267 log_init(game.board, players)
271 game.start() # This runs in a new thread
277 error = game.error + graphics.error
286 if src_file != None and src_file != sys.stdin:
289 sys.stdout.write(game.final_result + "\n")
293 # This is how python does a main() function...
294 if __name__ == "__main__":
296 sys.exit(main(sys.argv))
297 except KeyboardInterrupt:
298 sys.stderr.write(sys.argv[0] + " : Got KeyboardInterrupt. Stopping everything\n")
299 if isinstance(graphics, StoppableThread):
301 graphics.run() # Will clean up graphics because it is stopped, not run it (a bit dodgy)
303 if isinstance(game, StoppableThread):