Lots of stuff happened
[progcomp2013.git] / qchess / game.py
1
2 # A thread that runs the game
3 class GameThread(StoppableThread):
4         def __init__(self, board, players):
5                 StoppableThread.__init__(self)
6                 self.board = board
7                 self.players = players
8                 self.state = {"turn" : None} # The game state
9                 self.error = 0 # Whether the thread exits with an error
10                 self.lock = threading.RLock() #lock for access of self.state
11                 self.cond = threading.Condition() # conditional for some reason, I forgot
12                 self.final_result = ""
13
14         # Run the game (run in new thread with start(), run in current thread with run())
15         def run(self):
16                 result = ""
17                 while not self.stopped():
18                         
19                         for p in self.players:
20                                 with self.lock:
21                                         if isinstance(p, NetworkSender):
22                                                 self.state["turn"] = p.base_player # "turn" contains the player who's turn it is
23                                         else:
24                                                 self.state["turn"] = p
25                                 try:
26                                         [x,y] = p.select() # Player selects a square
27                                         if self.stopped():
28                                                 break
29
30                                         
31                                                 
32
33                                         result = self.board.select(x, y, colour = p.colour)                             
34                                         for p2 in self.players:
35                                                 p2.update(result) # Inform players of what happened
36
37
38
39                                         target = self.board.grid[x][y]
40                                         if isinstance(graphics, GraphicsThread):
41                                                 with graphics.lock:
42                                                         graphics.state["moves"] = self.board.possible_moves(target)
43                                                         graphics.state["select"] = target
44
45                                         time.sleep(turn_delay)
46
47
48                                         if len(self.board.possible_moves(target)) == 0:
49                                                 #print "Piece cannot move"
50                                                 target.deselect()
51                                                 if isinstance(graphics, GraphicsThread):
52                                                         with graphics.lock:
53                                                                 graphics.state["moves"] = None
54                                                                 graphics.state["select"] = None
55                                                                 graphics.state["dest"] = None
56                                                 continue
57
58                                         try:
59                                                 [x2,y2] = p.get_move() # Player selects a destination
60                                         except:
61                                                 self.stop()
62
63                                         if self.stopped():
64                                                 break
65
66                                         result = self.board.update_move(x, y, x2, y2)
67                                         for p2 in self.players:
68                                                 p2.update(str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)) # Inform players of what happened
69
70                                         if isinstance(graphics, GraphicsThread):
71                                                 with graphics.lock:
72                                                         graphics.state["moves"] = [[x2,y2]]
73
74                                         time.sleep(turn_delay)
75
76                                         if isinstance(graphics, GraphicsThread):
77                                                 with graphics.lock:
78                                                         graphics.state["select"] = None
79                                                         graphics.state["dest"] = None
80                                                         graphics.state["moves"] = None
81
82                         # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
83                                 except Exception,e:
84                                         result = e.message
85                                         #sys.stderr.write(result + "\n")
86                                         
87                                         self.stop()
88                                         with self.lock:
89                                                 self.final_result = self.state["turn"].colour + " " + e.message
90
91                                 if self.board.king["black"] == None:
92                                         if self.board.king["white"] == None:
93                                                 with self.lock:
94                                                         self.final_result = self.state["turn"].colour + " DRAW"
95                                         else:
96                                                 with self.lock:
97                                                         self.final_result = "white"
98                                         self.stop()
99                                 elif self.board.king["white"] == None:
100                                         with self.lock:
101                                                 self.final_result = "black"
102                                         self.stop()
103                                                 
104
105                                 if self.stopped():
106                                         break
107
108
109                 for p2 in self.players:
110                         p2.quit(self.final_result)
111
112                 graphics.stop()
113
114         
115
116
117 def opponent(colour):
118         if colour == "white":
119                 return "black"
120         else:
121                 return "white"

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