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:
102 elif platform.system() != "Windows": # Windows breaks this option
103 agent_timeout = float(arg[2:].split("=")[1])
105 sys.stderr.write(sys.argv[0] + " : Warning - You are using Windows\n")
108 elif (arg[1] == '-' and arg[2:] == "help"):
110 os.system("less data/help.txt") # The best help function
119 if graphics_enabled == True:
121 graphics = GraphicsThread(board, grid_sz = [64,64]) # Construct a GraphicsThread!
124 sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
125 graphics_enabled = False
127 # If there are no players listed, display a nice pretty menu
128 if len(players) != 2:
130 players = graphics.SelectPlayers(players)
132 sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
135 # If there are still no players, quit
136 if players == None or len(players) != 2:
137 sys.stderr.write(sys.argv[0] + " : Graphics window closed before players chosen\n")
141 # Wrap NetworkSender players around original players if necessary
142 for i in range(len(players)):
143 if isinstance(players[i], NetworkReceiver):
144 players[i].board = board # Network players need direct access to the board
145 for j in range(len(players)):
148 if isinstance(players[j], NetworkSender) or isinstance(players[j], NetworkReceiver):
150 players[j] = NetworkSender(players[j], players[i].address)
151 players[j].board = board
153 # Connect the networked players
155 if isinstance(p, NetworkSender) or isinstance(p, NetworkReceiver):
157 graphics.board.display_grid(graphics.window, graphics.grid_sz)
158 graphics.message("Connecting to " + p.colour + " player...")
162 # Construct a GameThread! Make it global! Damn the consequences!
163 game = GameThread(board, players)
168 game.start() # This runs in a new thread
171 return game.error + graphics.error
176 # This is how python does a main() function...
177 if __name__ == "__main__":
178 sys.exit(main(sys.argv))