5 agent_timeout = -1.0 # Timeout in seconds for AI players to make moves
6 # WARNING: Won't work for windows based operating systems
8 if platform.system() == "Windows":
9 agent_timeout = -1 # Hence this
11 # A player who can't play
13 def __init__(self, name, colour):
17 # Player that runs from another process
18 class AgentPlayer(Player):
21 def __init__(self, name, colour):
22 Player.__init__(self, name, colour)
23 self.p = subprocess.Popen(name, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
25 self.send_message(colour)
27 def send_message(self, s):
28 if agent_timeout > 0.0:
29 ready = select.select([], [self.p.stdin], [], agent_timeout)[1]
31 ready = [self.p.stdin]
32 if self.p.stdin in ready:
33 #print "Writing to p.stdin"
35 self.p.stdin.write(s + "\n")
37 raise Exception("UNRESPONSIVE")
39 raise Exception("UNRESPONSIVE")
41 def get_response(self):
42 if agent_timeout > 0.0:
43 ready = select.select([self.p.stdout], [], [], agent_timeout)[0]
45 ready = [self.p.stdout]
46 if self.p.stdout in ready:
47 #print "Reading from p.stdout"
49 return self.p.stdout.readline().strip("\r\n")
50 except: # Exception, e:
51 raise Exception("UNRESPONSIVE")
53 raise Exception("UNRESPONSIVE")
57 self.send_message("SELECTION?")
58 line = self.get_response()
61 result = map(int, line.split(" "))
63 raise Exception("GIBBERISH \"" + str(line) + "\"")
66 def update(self, result):
67 #print "Update " + str(result) + " called for AgentPlayer"
68 self.send_message(result)
73 self.send_message("MOVE?")
74 line = self.get_response()
77 result = map(int, line.split(" "))
79 raise Exception("GIBBERISH \"" + str(line) + "\"")
82 def quit(self, final_result):
84 self.send_message("QUIT " + final_result)
88 # So you want to be a player here?
89 class HumanPlayer(Player):
90 def __init__(self, name, colour):
91 Player.__init__(self, name, colour)
93 # Select your preferred account
95 if isinstance(graphics, GraphicsThread):
96 # Basically, we let the graphics thread do some shit and then return that information to the game thread
97 graphics.cond.acquire()
98 # We wait for the graphics thread to select a piece
99 while graphics.stopped() == False and graphics.state["select"] == None:
100 graphics.cond.wait() # The difference between humans and machines is that humans sleep
101 select = graphics.state["select"]
104 graphics.cond.release()
105 if graphics.stopped():
107 return [select.x, select.y]
109 # Since I don't display the board in this case, I'm not sure why I filled it in...
111 sys.stdout.write("SELECTION?\n")
113 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
115 sys.stderr.write("ILLEGAL GIBBERISH\n")
117 # It's your move captain
119 if isinstance(graphics, GraphicsThread):
120 graphics.cond.acquire()
121 while graphics.stopped() == False and graphics.state["dest"] == None:
123 graphics.cond.release()
125 return graphics.state["dest"]
129 sys.stdout.write("MOVE?\n")
131 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
133 sys.stderr.write("ILLEGAL GIBBERISH\n")
136 # Are you sure you want to quit?
137 def quit(self, final_result):
139 sys.stdout.write("QUIT " + final_result + "\n")
141 # Completely useless function
142 def update(self, result):
143 if isinstance(graphics, GraphicsThread):
146 sys.stdout.write(result + "\n")
149 # Player that makes random moves
150 class AgentRandom(Player):
151 def __init__(self, name, colour):
152 Player.__init__(self, name, colour)
155 self.board = Board(style = "agent")
159 self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
161 # Check that the piece has some possibility to move
162 tmp = self.choice.current_type
163 if tmp == "unknown": # For unknown pieces, try both types
164 for t in self.choice.types:
167 self.choice.current_type = t
168 all_moves += self.board.possible_moves(self.choice)
170 all_moves = self.board.possible_moves(self.choice)
171 self.choice.current_type = tmp
172 if len(all_moves) > 0:
174 return [self.choice.x, self.choice.y]
177 moves = self.board.possible_moves(self.choice)
178 move = moves[random.randint(0, len(moves)-1)]
181 def update(self, result):
182 #sys.stderr.write(sys.argv[0] + " : Update board for AgentRandom\n")
183 self.board.update(result)
186 def quit(self, final_result):