5 # A player who can't play
7 def __init__(self, name, colour):
11 # Player that runs from another process
12 class AgentPlayer(Player):
13 def __init__(self, name, colour):
14 Player.__init__(self, name, colour)
15 self.p = subprocess.Popen(name, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=sys.stderr)
17 self.p.stdin.write(colour + "\n")
19 raise Exception("UNRESPONSIVE")
24 self.p.stdin.write("SELECTION?\n")
25 line = self.p.stdout.readline().strip("\r\n ")
27 # raise Exception("UNRESPONSIVE")
29 result = map(int, line.split(" "))
31 raise Exception("GIBBERISH \"" + str(line) + "\"")
34 def update(self, result):
35 #print "Update " + str(result) + " called for AgentPlayer"
37 self.p.stdin.write(result + "\n")
39 # raise Exception("UNRESPONSIVE")
44 self.p.stdin.write("MOVE?\n")
45 line = self.p.stdout.readline().strip("\r\n ")
47 raise Exception("UNRESPONSIVE")
49 result = map(int, line.split(" "))
51 raise Exception("GIBBERISH \"" + str(line) + "\"")
54 def quit(self, final_result):
56 self.p.stdin.write("QUIT " + final_result + "\n")
60 # So you want to be a player here?
61 class HumanPlayer(Player):
62 def __init__(self, name, colour):
63 Player.__init__(self, name, colour)
65 # Select your preferred account
67 if isinstance(graphics, GraphicsThread):
68 # Basically, we let the graphics thread do some shit and then return that information to the game thread
69 graphics.cond.acquire()
70 # We wait for the graphics thread to select a piece
71 while graphics.stopped() == False and graphics.state["select"] == None:
72 graphics.cond.wait() # The difference between humans and machines is that humans sleep
73 select = graphics.state["select"]
76 graphics.cond.release()
77 if graphics.stopped():
79 return [select.x, select.y]
81 # Since I don't display the board in this case, I'm not sure why I filled it in...
83 sys.stdout.write("SELECTION?\n")
85 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
87 sys.stderr.write("ILLEGAL GIBBERISH\n")
89 # It's your move captain
91 if isinstance(graphics, GraphicsThread):
92 graphics.cond.acquire()
93 while graphics.stopped() == False and graphics.state["dest"] == None:
95 graphics.cond.release()
97 return graphics.state["dest"]
101 sys.stdout.write("MOVE?\n")
103 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
105 sys.stderr.write("ILLEGAL GIBBERISH\n")
108 # Are you sure you want to quit?
109 def quit(self, final_result):
110 sys.stdout.write("QUIT " + final_result + "\n")
112 # Completely useless function
113 def update(self, result):
114 if isinstance(graphics, GraphicsThread):
117 sys.stdout.write(result + "\n")
120 # Player that makes random moves
121 class AgentRandom(Player):
122 def __init__(self, name, colour):
123 Player.__init__(self, name, colour)
126 self.board = Board(style = "agent")
130 self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
132 # Check that the piece has some possibility to move
133 tmp = self.choice.current_type
134 if tmp == "unknown": # For unknown pieces, try both types
135 for t in self.choice.types:
138 self.choice.current_type = t
139 all_moves += self.board.possible_moves(self.choice)
141 all_moves = self.board.possible_moves(self.choice)
142 self.choice.current_type = tmp
143 if len(all_moves) > 0:
145 return [self.choice.x, self.choice.y]
148 moves = self.board.possible_moves(self.choice)
149 move = moves[random.randint(0, len(moves)-1)]
152 def update(self, result):
153 #sys.stderr.write(sys.argv[0] + " : Update board for AgentRandom\n")
154 self.board.update(result)
157 def quit(self, final_result):