Update website a bit
[progcomp2013.git] / agents / bishop / bishop.py
1 #!/usr/bin/python -u
2
3 from qchess import *
4
5 graphics_enabled = True
6
7 """
8         This is a wrapper to AgentBishop, which can now be found directly in qchess as one of the internal agents
9         As well as wrapping, it will also show AgentBishop's thought processes in graphics, which is kind of cool
10
11         So basically, using `./qchess.py @internal:AgentBishop` is better, unless you want to see the graphics
12 """
13
14         
15 # Horrible messy graphics class that draws what the agent is doing, kind of useful for testing
16 class AgentBishop_Graphics(GraphicsThread):
17         def __init__(self, board, title):
18                 GraphicsThread.__init__(self, board, title, grid_sz = [64,64])
19                 self.choice = None
20                 self.moves = None
21
22         def run(self):
23                 square_img = pygame.Surface((self.grid_sz[0], self.grid_sz[1]),pygame.SRCALPHA) # A square image
24                 while not self.stopped():
25                 
26                         self.board.display_grid(window = self.window, grid_sz = self.grid_sz)   
27
28                         # Draw choice of the AI
29                         if agent.choice != None:
30                                 mp = [self.grid_sz[i] * [agent.choice.x, agent.choice.y][i] for i in range(2)]
31                                 square_img.fill(pygame.Color(0,255,0,64))
32                                 self.window.blit(square_img, mp)
33
34                         # Draw calculated choices for the piece clicked on
35                         if self.choice != None:
36                                 mp = [self.grid_sz[i] * [self.choice.x, self.choice.y][i] for i in range(2)]
37                                 square_img.fill(pygame.Color(0,0,255,128))
38                                 self.window.blit(square_img, mp)
39
40                         # Draw the choices the AI calculated from the selection of the chosen piece
41                         if agent.choice != None and agent.choice.selected_moves != None:
42                                 for m in agent.choice.selected_moves:
43                                         mp = [m[0][i] * self.grid_sz[i] for i in range(2)]
44                                         square_img.fill(pygame.Color(128,128,255,128))
45                                         self.window.blit(square_img, mp)
46                                         font = pygame.font.Font(None, 14)
47                                         text = font.render("{0:.2f}".format(round(m[1],2)), 1, pygame.Color(255,0,0))
48                                         mp[0] = mp[0] + self.grid_sz[0] - text.get_width()
49                                         mp[1] = mp[1] + self.grid_sz[1] - text.get_height()
50                                         self.window.blit(text, mp)
51
52
53                         # Draw the choice the AI's chosen piece could have actually made
54                         if agent.choice != None and agent.choice.last_moves != None:
55                                 for m in agent.choice.last_moves:
56                                         mp = [m[0][i] * self.grid_sz[i] for i in range(2)]
57                                         square_img.fill(pygame.Color(255,0,0,128))
58                                         self.window.blit(square_img, mp)
59                                         font = pygame.font.Font(None, 14)
60                                         text = font.render("{0:.2f}".format(round(m[1],2)), 1, pygame.Color(0,0,255))
61                                         mp[0] = mp[0] + self.grid_sz[0] - text.get_width()
62                                         self.window.blit(text, mp)
63
64                         
65
66
67                         if self.moves != None:
68                                 for m in self.moves:
69                                         mp = [m[0][i] * self.grid_sz[i] for i in range(2)]
70                                         square_img.fill(pygame.Color(255,0,255,128))
71                                         self.window.blit(square_img, mp)
72                                         font = pygame.font.Font(None, 14)
73                                         text = font.render("{0:.2f}".format(round(m[1],2)), 1, pygame.Color(0,0,0))
74                                         self.window.blit(text, mp)
75                         
76                         
77         
78                         self.board.display_pieces(window = self.window, grid_sz = self.grid_sz)
79
80                         pygame.display.flip()
81                         
82                         for event in pygame.event.get():
83                                 if event.type == pygame.QUIT:
84                                         self.stop()
85                                         break
86                                 elif event.type == pygame.MOUSEBUTTONDOWN:
87                                         m = [event.pos[i] / self.grid_sz[i] for i in range(len(event.pos))]
88                                         p = agent.board.grid[m[0]][m[1]]
89                                         if p == None:
90                                                 continue
91                                         self.choice = p
92                                         self.last_moves = self.choice.last_moves
93                                         self.selected_moves = self.choice.selected_moves
94                                         if event.button == 3 or self.choice.last_moves == None:
95                                                 self.moves = agent.prioritise_moves(self.choice)
96                                         else:
97                                                 self.moves = self.choice.last_moves
98                                         
99                                 elif event.type == pygame.MOUSEBUTTONUP:
100                                         if self.choice == None:
101                                                 continue
102                                         self.choice.last_moves = self.last_moves
103                                         self.choice.selected_moves = self.selected_moves
104                                         self.choice = None
105                                         self.moves = None
106                                         
107                 pygame.display.quit()                           
108
109 if __name__ == "__main__":
110         
111         if sys.argv[1] == "--no-graphics":
112                 graphics_enabled = False
113
114         colour = sys.stdin.readline().strip("\r\n")
115         agent = AgentBishop(sys.argv[0], colour)
116
117         if graphics_enabled:
118                 graphics = AgentBishop_Graphics(agent.board, "Agent Bishop ("+agent.colour+") DEBUG")
119                 graphics.start()
120         run_agent(agent)
121         
122         if graphics_enabled:
123                 graphics.stop()
124                 graphics.join()

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