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

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