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

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