e3f1c7cd3bdca0d421c952184836819f9f1b02ac
[progcomp2013.git] / qchess / src / game.py
1
2
3 log_file = None
4
5 def log(s):
6         if log_file != None:
7                 import datetime
8                 log_file.write(str(datetime.datetime.now()) + " : " + s + "\n")
9
10
11         
12
13 # A thread that runs the game
14 class GameThread(StoppableThread):
15         def __init__(self, board, players):
16                 StoppableThread.__init__(self)
17                 self.board = board
18                 self.players = players
19                 self.state = {"turn" : None} # The game state
20                 self.error = 0 # Whether the thread exits with an error
21                 self.lock = threading.RLock() #lock for access of self.state
22                 self.cond = threading.Condition() # conditional for some reason, I forgot
23                 self.final_result = ""
24
25         # Run the game (run in new thread with start(), run in current thread with run())
26         def run(self):
27                 result = ""
28                 while not self.stopped():
29                         
30                         for p in self.players:
31                                 with self.lock:
32                                         if isinstance(p, NetworkSender):
33                                                 self.state["turn"] = p.base_player # "turn" contains the player who's turn it is
34                                         else:
35                                                 self.state["turn"] = p
36                                 #try:
37                                 if True:
38                                         [x,y] = p.select() # Player selects a square
39                                         if self.stopped():
40                                                 break
41
42                                         
43                                                 
44
45                                         result = self.board.select(x, y, colour = p.colour)                             
46                                         for p2 in self.players:
47                                                 p2.update(result) # Inform players of what happened
48
49
50                                         log(result)
51
52                                         target = self.board.grid[x][y]
53                                         if isinstance(graphics, GraphicsThread):
54                                                 with graphics.lock:
55                                                         graphics.state["moves"] = self.board.possible_moves(target)
56                                                         graphics.state["select"] = target
57
58                                         time.sleep(turn_delay)
59
60
61                                         if len(self.board.possible_moves(target)) == 0:
62                                                 #print "Piece cannot move"
63                                                 target.deselect()
64                                                 if isinstance(graphics, GraphicsThread):
65                                                         with graphics.lock:
66                                                                 graphics.state["moves"] = None
67                                                                 graphics.state["select"] = None
68                                                                 graphics.state["dest"] = None
69                                                 continue
70
71                                         try:
72                                                 [x2,y2] = p.get_move() # Player selects a destination
73                                         except:
74                                                 self.stop()
75
76                                         if self.stopped():
77                                                 break
78
79                                         self.board.update_move(x, y, x2, y2)
80                                         result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
81                                         for p2 in self.players:
82                                                 p2.update(result) # Inform players of what happened
83
84                                         log(result)
85
86                                         if isinstance(graphics, GraphicsThread):
87                                                 with graphics.lock:
88                                                         graphics.state["moves"] = [[x2,y2]]
89
90                                         time.sleep(turn_delay)
91
92                                         if isinstance(graphics, GraphicsThread):
93                                                 with graphics.lock:
94                                                         graphics.state["select"] = None
95                                                         graphics.state["dest"] = None
96                                                         graphics.state["moves"] = None
97
98                         # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
99                         #       except Exception,e:
100                         #               result = e.message
101                         #               #sys.stderr.write(result + "\n")
102                         #               
103                         #               self.stop()
104                         #               with self.lock:
105                         #                       self.final_result = self.state["turn"].colour + " " + e.message
106
107                                 if self.board.king["black"] == None:
108                                         if self.board.king["white"] == None:
109                                                 with self.lock:
110                                                         self.final_result = self.state["turn"].colour + " DRAW"
111                                         else:
112                                                 with self.lock:
113                                                         self.final_result = "white"
114                                         self.stop()
115                                 elif self.board.king["white"] == None:
116                                         with self.lock:
117                                                 self.final_result = "black"
118                                         self.stop()
119                                                 
120
121                                 if self.stopped():
122                                         break
123
124
125                 for p2 in self.players:
126                         p2.quit(self.final_result)
127
128                 log(self.final_result)
129
130                 graphics.stop()
131
132         
133 # A thread that replays a log file
134 class ReplayThread(GameThread):
135         def __init__(self, players, src):
136                 self.board = Board(style="agent")
137                 GameThread.__init__(self, self.board, players)
138                 self.src = src
139
140                 self.ended = False
141         
142         def run(self):
143                 i = 0
144                 phase = 0
145                 for line in self.src:
146
147                         if self.stopped():
148                                 self.ended = True
149                                 break
150
151                         with self.lock:
152                                 self.state["turn"] = self.players[i]
153
154                         line = line.split(":")
155                         result = line[len(line)-1].strip(" \r\n")
156                         log(result)
157
158                         try:
159                                 self.board.update(result)
160                         except:
161                                 self.ended = True
162                                 self.final_result = result
163                                 if isinstance(graphics, GraphicsThread):
164                                         graphics.stop()
165                                 break
166
167                         [x,y] = map(int, result.split(" ")[0:2])
168                         target = self.board.grid[x][y]
169
170                         if isinstance(graphics, GraphicsThread):
171                                 if phase == 0:
172                                         with graphics.lock:
173                                                 graphics.state["moves"] = self.board.possible_moves(target)
174                                                 graphics.state["select"] = target
175
176                                         time.sleep(turn_delay)
177
178                                 elif phase == 1:
179                                         [x2,y2] = map(int, result.split(" ")[3:5])
180                                         with graphics.lock:
181                                                 graphics.state["moves"] = [[x2,y2]]
182
183                                         time.sleep(turn_delay)
184
185                                         with graphics.lock:
186                                                 graphics.state["select"] = None
187                                                 graphics.state["dest"] = None
188                                                 graphics.state["moves"] = None
189                                                 
190
191
192                         
193
194                         for p in self.players:
195                                 p.update(result)
196                         
197                         phase = (phase + 1) % 2
198                         if phase == 0:
199                                 i = (i + 1) % 2
200
201                 
202
203 def opponent(colour):
204         if colour == "white":
205                 return "black"
206         else:
207                 return "white"

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