Self inflicted wounds using cx_freeze
[progcomp2013.git] / qchess / src / player.py
1 import subprocess
2 import select
3 import platform
4
5
6 agent_timeout = -1.0 # Timeout in seconds for AI players to make moves
7                         # WARNING: Won't work for windows based operating systems
8
9 if platform.system() == "Windows":
10         agent_timeout = -1 # Hence this
11
12 # A player who can't play
13 class Player():
14         def __init__(self, name, colour):
15                 self.name = name
16                 self.colour = colour
17
18 # Player that runs from another process
19 class AgentPlayer(Player):
20
21
22         def __init__(self, name, colour):
23                 Player.__init__(self, name, colour)
24                 self.p = subprocess.Popen(name, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
25                 
26                 self.send_message(colour)
27
28         def send_message(self, s):
29                 if agent_timeout > 0.0:
30                         ready = select.select([], [self.p.stdin], [], agent_timeout)[1]
31                 else:
32                         ready = [self.p.stdin]
33                 if self.p.stdin in ready:
34                         #print "Writing to p.stdin"
35                         try:
36                                 self.p.stdin.write(s + "\n")
37                         except:
38                                 raise Exception("UNRESPONSIVE")
39                 else:
40                         raise Exception("UNRESPONSIVE")
41
42         def get_response(self):
43                 if agent_timeout > 0.0:
44                         ready = select.select([self.p.stdout], [], [], agent_timeout)[0]
45                 else:
46                         ready = [self.p.stdout]
47                 if self.p.stdout in ready:
48                         #print "Reading from p.stdout"
49                         try:
50                                 return self.p.stdout.readline().strip("\r\n")
51                         except: # Exception, e:
52                                 raise Exception("UNRESPONSIVE")
53                 else:
54                         raise Exception("UNRESPONSIVE")
55
56         def select(self):
57
58                 self.send_message("SELECTION?")
59                 line = self.get_response()
60                 
61                 try:
62                         result = map(int, line.split(" "))
63                 except:
64                         raise Exception("GIBBERISH \"" + str(line) + "\"")
65                 return result
66
67         def update(self, result):
68                 #print "Update " + str(result) + " called for AgentPlayer"
69                 self.send_message(result)
70
71
72         def get_move(self):
73                 
74                 self.send_message("MOVE?")
75                 line = self.get_response()
76                 
77                 try:
78                         result = map(int, line.split(" "))
79                 except:
80                         raise Exception("GIBBERISH \"" + str(line) + "\"")
81                 return result
82
83         def quit(self, final_result):
84                 try:
85                         self.send_message("QUIT " + final_result)
86                 except:
87                         self.p.kill()
88
89 # So you want to be a player here?
90 class HumanPlayer(Player):
91         def __init__(self, name, colour):
92                 Player.__init__(self, name, colour)
93                 
94         # Select your preferred account
95         def select(self):
96                 if isinstance(graphics, GraphicsThread):
97                         # Basically, we let the graphics thread do some shit and then return that information to the game thread
98                         graphics.cond.acquire()
99                         # We wait for the graphics thread to select a piece
100                         while graphics.stopped() == False and graphics.state["select"] == None:
101                                 graphics.cond.wait() # The difference between humans and machines is that humans sleep
102                         select = graphics.state["select"]
103                         
104                         
105                         graphics.cond.release()
106                         if graphics.stopped():
107                                 return [-1,-1]
108                         return [select.x, select.y]
109                 else:
110                         # Since I don't display the board in this case, I'm not sure why I filled it in...
111                         while True:
112                                 sys.stdout.write("SELECTION?\n")
113                                 try:
114                                         p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
115                                 except:
116                                         sys.stderr.write("ILLEGAL GIBBERISH\n")
117                                         continue
118         # It's your move captain
119         def get_move(self):
120                 if isinstance(graphics, GraphicsThread):
121                         graphics.cond.acquire()
122                         while graphics.stopped() == False and graphics.state["dest"] == None:
123                                 graphics.cond.wait()
124                         graphics.cond.release()
125                         
126                         return graphics.state["dest"]
127                 else:
128
129                         while True:
130                                 sys.stdout.write("MOVE?\n")
131                                 try:
132                                         p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
133                                 except:
134                                         sys.stderr.write("ILLEGAL GIBBERISH\n")
135                                         continue
136
137         # Are you sure you want to quit?
138         def quit(self, final_result):
139                 if graphics == None:            
140                         sys.stdout.write("QUIT " + final_result + "\n")
141
142         # Completely useless function
143         def update(self, result):
144                 if isinstance(graphics, GraphicsThread):
145                         pass
146                 else:
147                         sys.stdout.write(result + "\n") 
148
149
150 # Player that makes random moves
151 class AgentRandom(Player):
152         def __init__(self, name, colour):
153                 Player.__init__(self, name, colour)
154                 self.choice = None
155
156                 self.board = Board(style = "agent")
157
158         def select(self):
159                 while True:
160                         self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
161                         all_moves = []
162                         # Check that the piece has some possibility to move
163                         tmp = self.choice.current_type
164                         if tmp == "unknown": # For unknown pieces, try both types
165                                 for t in self.choice.types:
166                                         if t == "unknown":
167                                                 continue
168                                         self.choice.current_type = t
169                                         all_moves += self.board.possible_moves(self.choice)
170                         else:
171                                 all_moves = self.board.possible_moves(self.choice)
172                         self.choice.current_type = tmp
173                         if len(all_moves) > 0:
174                                 break
175                 return [self.choice.x, self.choice.y]
176
177         def get_move(self):
178                 moves = self.board.possible_moves(self.choice)
179                 move = moves[random.randint(0, len(moves)-1)]
180                 return move
181
182         def update(self, result):
183                 #sys.stderr.write(sys.argv[0] + " : Update board for AgentRandom\n")
184                 self.board.update(result)
185                 self.board.verify()
186
187         def quit(self, final_result):
188                 pass
189
190

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