Initial Commit
[progcomp2013.git] / qchess / player.py
1 import subprocess
2
3
4
5 # A player who can't play
6 class Player():
7         def __init__(self, name, colour):
8                 self.name = name
9                 self.colour = colour
10
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)
16                 try:
17                         self.p.stdin.write(colour + "\n")
18                 except:
19                         raise Exception("UNRESPONSIVE")
20
21         def select(self):
22                 
23                 #try:
24                 self.p.stdin.write("SELECTION?\n")
25                 line = self.p.stdout.readline().strip("\r\n ")
26                 #except:
27                 #       raise Exception("UNRESPONSIVE")
28                 try:
29                         result = map(int, line.split(" "))
30                 except:
31                         raise Exception("GIBBERISH \"" + str(line) + "\"")
32                 return result
33
34         def update(self, result):
35                 #print "Update " + str(result) + " called for AgentPlayer"
36 #               try:
37                 self.p.stdin.write(result + "\n")
38 #               except:
39 #               raise Exception("UNRESPONSIVE")
40
41         def get_move(self):
42                 
43                 try:
44                         self.p.stdin.write("MOVE?\n")
45                         line = self.p.stdout.readline().strip("\r\n ")
46                 except:
47                         raise Exception("UNRESPONSIVE")
48                 try:
49                         result = map(int, line.split(" "))
50                 except:
51                         raise Exception("GIBBERISH \"" + str(line) + "\"")
52                 return result
53
54         def quit(self, final_result):
55                 try:
56                         self.p.stdin.write("QUIT " + final_result + "\n")
57                 except:
58                         self.p.kill()
59
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)
64                 
65         # Select your preferred account
66         def select(self):
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"]
74                         
75                         
76                         graphics.cond.release()
77                         if graphics.stopped():
78                                 return [-1,-1]
79                         return [select.x, select.y]
80                 else:
81                         # Since I don't display the board in this case, I'm not sure why I filled it in...
82                         while True:
83                                 sys.stdout.write("SELECTION?\n")
84                                 try:
85                                         p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
86                                 except:
87                                         sys.stderr.write("ILLEGAL GIBBERISH\n")
88                                         continue
89         # It's your move captain
90         def get_move(self):
91                 if isinstance(graphics, GraphicsThread):
92                         graphics.cond.acquire()
93                         while graphics.stopped() == False and graphics.state["dest"] == None:
94                                 graphics.cond.wait()
95                         graphics.cond.release()
96                         
97                         return graphics.state["dest"]
98                 else:
99
100                         while True:
101                                 sys.stdout.write("MOVE?\n")
102                                 try:
103                                         p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
104                                 except:
105                                         sys.stderr.write("ILLEGAL GIBBERISH\n")
106                                         continue
107
108         # Are you sure you want to quit?
109         def quit(self, final_result):
110                 sys.stdout.write("QUIT " + final_result + "\n")
111
112         # Completely useless function
113         def update(self, result):
114                 if isinstance(graphics, GraphicsThread):
115                         pass
116                 else:
117                         sys.stdout.write(result + "\n") 
118
119
120 # Player that makes random moves
121 class AgentRandom(Player):
122         def __init__(self, name, colour):
123                 Player.__init__(self, name, colour)
124                 self.choice = None
125
126                 self.board = Board(style = "agent")
127
128         def select(self):
129                 while True:
130                         self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
131                         all_moves = []
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:
136                                         if t == "unknown":
137                                                 continue
138                                         self.choice.current_type = t
139                                         all_moves += self.board.possible_moves(self.choice)
140                         else:
141                                 all_moves = self.board.possible_moves(self.choice)
142                         self.choice.current_type = tmp
143                         if len(all_moves) > 0:
144                                 break
145                 return [self.choice.x, self.choice.y]
146
147         def get_move(self):
148                 moves = self.board.possible_moves(self.choice)
149                 move = moves[random.randint(0, len(moves)-1)]
150                 return move
151
152         def update(self, result):
153                 #sys.stderr.write(sys.argv[0] + " : Update board for AgentRandom\n")
154                 self.board.update(result)
155                 self.board.verify()
156
157         def quit(self, final_result):
158                 pass

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