Self inflicted wounds using cx_freeze
[progcomp2013.git] / qchess / src / main.py
1 #!/usr/bin/python -u
2
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
5
6 """
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)
11 """
12
13 # system python modules or whatever they are called
14 import sys
15 import os
16 import time
17
18 turn_delay = 0.5
19 [game, graphics] = [None, None]
20
21 def make_player(name, colour):
22         if name[0] == '@':
23                 if name[1:] == "human":
24                         return HumanPlayer(name, colour)
25                 s = name[1:].split(":")
26                 if s[0] == "network":
27                         address = None
28                         if len(s) > 1:
29                                 address = s[1]
30                         return NetworkReceiver(colour, address)
31
32         else:
33                 return AgentPlayer(name, colour)
34                         
35
36
37 # The main function! It does the main stuff!
38 def main(argv):
39
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
42         global game
43         global graphics
44         
45         global turn_delay
46         global agent_timeout
47         global log_file
48         global src_file
49
50
51
52         
53         style = "quantum"
54         colour = "white"
55         graphics_enabled = True
56
57         players = []
58         i = 0
59         while i < len(argv)-1:
60                 i += 1
61                 arg = argv[i]
62                 if arg[0] != '-':
63                         players.append(make_player(arg, colour))
64                         if colour == "white":
65                                 colour = "black"
66                         elif colour == "black":
67                                 pass
68                         else:
69                                 sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n")
70                         continue
71
72                 # Option parsing goes here
73                 if arg[1] == '-' and arg[2:] == "classical":
74                         style = "classical"
75                 elif arg[1] == '-' and arg[2:] == "quantum":
76                         style = "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"):
80                         # Load game from file
81                         if len(arg[2:].split("=")) == 1:
82                                 src_file = sys.stdout
83                         else:
84                                 src_file = arg[2:].split("=")[1]
85                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
86                         # Log file
87                         if len(arg[2:].split("=")) == 1:
88                                 log_file = sys.stdout
89                         else:
90                                 log_file = arg[2:].split("=")[1]
91                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"):
92                         # Delay
93                         if len(arg[2:].split("=")) == 1:
94                                 turn_delay = 0
95                         else:
96                                 turn_delay = float(arg[2:].split("=")[1])
97
98                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "timeout"):
99                         # Timeout
100                         if len(arg[2:].split("=")) == 1:
101                                 agent_timeout = -1
102                         else:
103                                 agent_timeout = float(arg[2:].split("=")[1])
104                                 
105                 elif (arg[1] == '-' and arg[2:] == "help"):
106                         # Help
107                         os.system("less data/help.txt") # The best help function
108                         return 0
109
110
111         # Create the board
112         board = Board(style)
113
114
115         # Initialise GUI
116         if graphics_enabled == True:
117                 try:
118                         graphics = GraphicsThread(board, grid_sz = [64,64]) # Construct a GraphicsThread!
119
120                 except Exception,e:
121                         graphics = None
122                         sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
123                         graphics_enabled = False
124
125         # If there are no players listed, display a nice pretty menu
126         if len(players) != 2:
127                 if graphics != None:
128                         players = graphics.SelectPlayers(players)
129                 else:
130                         sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
131                         return 44
132
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")
136                 return 45
137
138
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)):
144                                 if j == i:
145                                         continue
146                                 if isinstance(players[j], NetworkSender) or isinstance(players[j], NetworkReceiver):
147                                         continue
148                                 players[j] = NetworkSender(players[j], players[i].address)
149                                 players[j].board = board
150
151         # Connect the networked players
152         for p in players:
153                 if isinstance(p, NetworkSender) or isinstance(p, NetworkReceiver):
154                         if graphics != None:
155                                 graphics.board.display_grid(graphics.window, graphics.grid_sz)
156                                 graphics.message("Connecting to " + p.colour + " player...")
157                         p.connect()
158
159         
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")
164
165                 for i in range(len(players)):
166                         if isinstance(players[i], AgentPlayer):
167                                 players[i] = TimeoutPlayer(players[i], agent_timeout)
168
169         # Could potentially wrap TimeoutPlayer around internal classes...
170         # But that would suck
171
172                 
173                         
174
175
176         # Construct a GameThread! Make it global! Damn the consequences!
177         game = GameThread(board, players) 
178
179
180         
181         if graphics != None:
182                 game.start() # This runs in a new thread
183                 graphics.run()
184                 game.join()
185                 return game.error + graphics.error
186         else:
187                 game.run()
188                 return game.error
189
190 # This is how python does a main() function...
191 if __name__ == "__main__":
192         sys.exit(main(sys.argv))

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