Lots of stuff happened
[progcomp2013.git] / qchess / 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                         elif platform.system() != "Windows": # Windows breaks this option
103                                 agent_timeout = float(arg[2:].split("=")[1])
104                         else:
105                                 sys.stderr.write(sys.argv[0] + " : Warning - You are using Windows\n")
106                                 agent_timeout = -1
107                                 
108                 elif (arg[1] == '-' and arg[2:] == "help"):
109                         # Help
110                         os.system("less data/help.txt") # The best help function
111                         return 0
112
113
114         # Create the board
115         board = Board(style)
116
117
118         # Initialise GUI
119         if graphics_enabled == True:
120                 try:
121                         graphics = GraphicsThread(board, grid_sz = [64,64]) # Construct a GraphicsThread!
122                 except Exception,e:
123                         graphics = None
124                         sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
125                         graphics_enabled = False
126
127         # If there are no players listed, display a nice pretty menu
128         if len(players) != 2:
129                 if graphics != None:
130                         players = graphics.SelectPlayers(players)
131                 else:
132                         sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
133                         return 44
134
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")
138                 return 45
139
140
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)):
146                                 if j == i:
147                                         continue
148                                 if isinstance(players[j], NetworkSender) or isinstance(players[j], NetworkReceiver):
149                                         continue
150                                 players[j] = NetworkSender(players[j], players[i].address)
151                                 players[j].board = board
152
153         # Connect the networked players
154         for p in players:
155                 if isinstance(p, NetworkSender) or isinstance(p, NetworkReceiver):
156                         if graphics != None:
157                                 graphics.board.display_grid(graphics.window, graphics.grid_sz)
158                                 graphics.message("Connecting to " + p.colour + " player...")
159                         p.connect()
160
161
162         # Construct a GameThread! Make it global! Damn the consequences!
163         game = GameThread(board, players) 
164
165
166         
167         if graphics != None:
168                 game.start() # This runs in a new thread
169                 graphics.run()
170                 game.join()
171                 return game.error + graphics.error
172         else:
173                 game.run()
174                 return game.error
175
176 # This is how python does a main() function...
177 if __name__ == "__main__":
178         sys.exit(main(sys.argv))

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