6 agent_timeout = -1.0 # Timeout in seconds for AI players to make moves
7 # WARNING: Won't work for windows based operating systems
9 if platform.system() == "Windows":
10 agent_timeout = -1 # Hence this
12 # A player who can't play
14 def __init__(self, name, colour):
18 # Player that runs from another process
19 class AgentPlayer(Player):
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)
26 self.send_message(colour)
28 def send_message(self, s):
29 if agent_timeout > 0.0:
30 ready = select.select([], [self.p.stdin], [], agent_timeout)[1]
32 ready = [self.p.stdin]
33 if self.p.stdin in ready:
34 #print "Writing to p.stdin"
36 self.p.stdin.write(s + "\n")
38 raise Exception("UNRESPONSIVE")
40 raise Exception("UNRESPONSIVE")
42 def get_response(self):
43 if agent_timeout > 0.0:
44 ready = select.select([self.p.stdout], [], [], agent_timeout)[0]
46 ready = [self.p.stdout]
47 if self.p.stdout in ready:
48 #print "Reading from p.stdout"
50 return self.p.stdout.readline().strip("\r\n")
51 except: # Exception, e:
52 raise Exception("UNRESPONSIVE")
54 raise Exception("UNRESPONSIVE")
58 self.send_message("SELECTION?")
59 line = self.get_response()
62 result = map(int, line.split(" "))
64 raise Exception("GIBBERISH \"" + str(line) + "\"")
67 def update(self, result):
68 #print "Update " + str(result) + " called for AgentPlayer"
69 self.send_message(result)
74 self.send_message("MOVE?")
75 line = self.get_response()
78 result = map(int, line.split(" "))
80 raise Exception("GIBBERISH \"" + str(line) + "\"")
83 def quit(self, final_result):
85 self.send_message("QUIT " + final_result)
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)
94 # Select your preferred account
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"]
105 graphics.cond.release()
106 if graphics.stopped():
108 return [select.x, select.y]
110 # Since I don't display the board in this case, I'm not sure why I filled it in...
112 sys.stdout.write("SELECTION?\n")
114 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
116 sys.stderr.write("ILLEGAL GIBBERISH\n")
118 # It's your move captain
120 if isinstance(graphics, GraphicsThread):
121 graphics.cond.acquire()
122 while graphics.stopped() == False and graphics.state["dest"] == None:
124 graphics.cond.release()
126 return graphics.state["dest"]
130 sys.stdout.write("MOVE?\n")
132 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
134 sys.stderr.write("ILLEGAL GIBBERISH\n")
137 # Are you sure you want to quit?
138 def quit(self, final_result):
140 sys.stdout.write("QUIT " + final_result + "\n")
142 # Completely useless function
143 def update(self, result):
144 if isinstance(graphics, GraphicsThread):
147 sys.stdout.write(result + "\n")
150 # Player that makes random moves
151 class AgentRandom(Player):
152 def __init__(self, name, colour):
153 Player.__init__(self, name, colour)
156 self.board = Board(style = "agent")
160 self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
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:
168 self.choice.current_type = t
169 all_moves += self.board.possible_moves(self.choice)
171 all_moves = self.board.possible_moves(self.choice)
172 self.choice.current_type = tmp
173 if len(all_moves) > 0:
175 return [self.choice.x, self.choice.y]
178 moves = self.board.possible_moves(self.choice)
179 move = moves[random.randint(0, len(moves)-1)]
182 def update(self, result):
183 #sys.stderr.write(sys.argv[0] + " : Update board for AgentRandom\n")
184 self.board.update(result)
187 def quit(self, final_result):