Mostly networking
[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                                 if True:
27                                         [x,y] = p.select() # Player selects a square
28                                         if self.stopped():
29                                                 break
30
31                                         
32                                                 
33
34                                         result = self.board.select(x, y, colour = p.colour)                             
35                                         for p2 in self.players:
36                                                 p2.update(result) # Inform players of what happened
37
38
39
40                                         target = self.board.grid[x][y]
41                                         if isinstance(graphics, GraphicsThread):
42                                                 with graphics.lock:
43                                                         graphics.state["moves"] = self.board.possible_moves(target)
44                                                         graphics.state["select"] = target
45
46                                         time.sleep(turn_delay)
47
48
49                                         if len(self.board.possible_moves(target)) == 0:
50                                                 #print "Piece cannot move"
51                                                 target.deselect()
52                                                 if isinstance(graphics, GraphicsThread):
53                                                         with graphics.lock:
54                                                                 graphics.state["moves"] = None
55                                                                 graphics.state["select"] = None
56                                                                 graphics.state["dest"] = None
57                                                 continue
58
59                                         try:
60                                                 [x2,y2] = p.get_move() # Player selects a destination
61                                         except:
62                                                 self.stop()
63
64                                         if self.stopped():
65                                                 break
66
67                                         result = self.board.update_move(x, y, x2, y2)
68                                         for p2 in self.players:
69                                                 p2.update(str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)) # Inform players of what happened
70
71                                         if isinstance(graphics, GraphicsThread):
72                                                 with graphics.lock:
73                                                         graphics.state["moves"] = [[x2,y2]]
74
75                                         time.sleep(turn_delay)
76
77                                         if isinstance(graphics, GraphicsThread):
78                                                 with graphics.lock:
79                                                         graphics.state["select"] = None
80                                                         graphics.state["dest"] = None
81                                                         graphics.state["moves"] = None
82
83                         # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
84                                 #except Exception,e:
85                                         #result = "ILLEGAL " + e.message
86                                         #sys.stderr.write(result + "\n")
87                                         
88                                         #self.stop()
89                                         #with self.lock:
90                                         #       self.final_result = self.state["turn"].colour + " " + "ILLEGAL"
91
92                                 if self.board.king["black"] == None:
93                                         if self.board.king["white"] == None:
94                                                 with self.lock:
95                                                         self.final_result = "DRAW"
96                                         else:
97                                                 with self.lock:
98                                                         self.final_result = "white"
99                                         self.stop()
100                                 elif self.board.king["white"] == None:
101                                         with self.lock:
102                                                 self.final_result = "black"
103                                         self.stop()
104                                                 
105
106                                 if self.stopped():
107                                         break
108
109
110                 for p2 in self.players:
111                         p2.quit(self.final_result)
112
113                 graphics.stop()
114
115         
116
117
118 def opponent(colour):
119         if colour == "white":
120                 return "black"
121         else:
122                 return "white"

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