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)
31 if s[0] == "internal":
34 internal_agents = inspect.getmembers(sys.modules[__name__], inspect.isclass)
35 internal_agents = [x for x in internal_agents if issubclass(x[1], InternalAgent)]
36 internal_agents.remove(('InternalAgent', InternalAgent))
39 sys.stderr.write(sys.argv[0] + " : '@internal' should be followed by ':' and an agent name\n")
40 sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
43 for a in internal_agents:
45 return a[1](name, colour)
47 sys.stderr.write(sys.argv[0] + " : Can't find an internal agent matching \"" + s[1] + "\"\n")
48 sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
53 return ExternalAgent(name, colour)
57 # The main function! It does the main stuff!
60 # Apparently python will silently treat things as local unless you do this
61 # Anyone who says "You should never use a global variable" can die in a fire
69 global graphics_enabled
70 global always_reveal_states
78 # Get the important warnings out of the way
79 if platform.system() == "Windows":
80 sys.stderr.write(sys.argv[0] + " : Warning - You are using " + platform.system() + "\n")
81 if platform.release() == "Vista":
82 sys.stderr.write(sys.argv[0] + " : God help you.\n")
87 while i < len(argv)-1:
91 p = make_player(arg, colour)
92 if not isinstance(p, Player):
93 sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n")
98 elif colour == "black":
101 sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n")
104 # Option parsing goes here
105 if arg[1] == '-' and arg[2:] == "classical":
107 elif arg[1] == '-' and arg[2:] == "quantum":
109 elif arg[1] == '-' and arg[2:] == "reveal":
110 always_reveal_states = True
111 elif (arg[1] == '-' and arg[2:] == "graphics"):
112 graphics_enabled = True
113 elif (arg[1] == '-' and arg[2:] == "no-graphics"):
114 graphics_enabled = False
115 elif (arg[1] == '-' and arg[2:].split("=")[0] == "file"):
116 # Load game from file
117 if len(arg[2:].split("=")) == 1:
120 f = arg[2:].split("=")[1]
121 if f[0:7] == "http://":
122 src_file = HttpReplay(f)
124 src_file = FileReplay(f.split(":")[0])
126 if len(f.split(":")) == 2:
127 max_moves = int(f.split(":")[1])
129 elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
131 if len(arg[2:].split("=")) == 1:
132 log_files.append(LogFile(sys.stdout))
134 f = arg[2:].split("=")[1]
136 log_files.append(ShortLog(f[1:]))
138 log_files.append(LogFile(open(f, "w", 0)))
139 elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"):
141 if len(arg[2:].split("=")) == 1:
144 turn_delay = float(arg[2:].split("=")[1])
146 elif (arg[1] == '-' and arg[2:].split("=")[0] == "timeout"):
148 if len(arg[2:].split("=")) == 1:
151 agent_timeout = float(arg[2:].split("=")[1])
153 elif (arg[1] == '-' and arg[2:] == "help"):
155 os.system("less data/help.txt") # The best help function
161 # Construct a GameThread! Make it global! Damn the consequences!
164 # Hack to stop ReplayThread from exiting
165 #if len(players) == 0:
166 # players = [HumanPlayer("dummy", "white"), HumanPlayer("dummy", "black")]
168 # Normally the ReplayThread exits if there are no players
169 # TODO: Decide which behaviour to use, and fix it
170 end = (len(players) == 0)
172 players = [Player("dummy", "white"), Player("dummy", "black")]
173 elif len(players) != 2:
174 sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
176 sys.stderr.write(sys.argv[0] + " : (You won't get a GUI, because --file was used, and the author is lazy)\n")
178 game = ReplayThread(players, src_file, end=end, max_moves=max_moves)
181 board.max_moves = max_moves
182 game = GameThread(board, players)
188 if graphics_enabled == True:
190 graphics = GraphicsThread(game.board, grid_sz = [64,64]) # Construct a GraphicsThread!
194 sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
195 graphics_enabled = False
197 # If there are no players listed, display a nice pretty menu
198 if len(players) != 2:
200 players = graphics.SelectPlayers(players)
202 sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
205 # If there are still no players, quit
206 if players == None or len(players) != 2:
207 sys.stderr.write(sys.argv[0] + " : Graphics window closed before players chosen\n")
211 # Wrap NetworkSender players around original players if necessary
212 for i in range(len(players)):
213 if isinstance(players[i], NetworkReceiver):
214 players[i].board = board # Network players need direct access to the board
215 for j in range(len(players)):
218 if isinstance(players[j], NetworkSender) or isinstance(players[j], NetworkReceiver):
220 players[j] = NetworkSender(players[j], players[i].address)
221 players[j].board = board
223 # Connect the networked players
225 if isinstance(p, NetworkSender) or isinstance(p, NetworkReceiver):
227 graphics.board.display_grid(graphics.window, graphics.grid_sz)
228 graphics.message("Connecting to " + p.colour + " player...")
232 # If using windows, select won't work; use horrible TimeoutPlayer hack
233 if agent_timeout > 0:
234 if platform.system() == "Windows":
235 for i in range(len(players)):
236 if isinstance(players[i], ExternalAgent) or isinstance(players[i], InternalAgent):
237 players[i] = TimeoutPlayer(players[i], agent_timeout)
241 # InternalAgents get wrapped to an ExternalAgent when there is a timeout
242 # This is not confusing at all.
243 for i in range(len(players)):
244 if isinstance(players[i], InternalAgent):
245 players[i] = ExternalWrapper(players[i])
253 log_init(game.board, players)
257 game.start() # This runs in a new thread
263 error = game.error + graphics.error
272 if src_file != None and src_file != sys.stdin:
275 sys.stdout.write(game.final_result + "\n")
279 # This is how python does a main() function...
280 if __name__ == "__main__":
282 sys.exit(main(sys.argv))
283 except KeyboardInterrupt:
284 sys.stderr.write(sys.argv[0] + " : Got KeyboardInterrupt. Stopping everything\n")
285 if isinstance(graphics, StoppableThread):
287 graphics.run() # Will clean up graphics because it is stopped, not run it (a bit dodgy)
289 if isinstance(game, StoppableThread):