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
9 So basically, using `./qchess.py @internal:AgentBishop` is better, unless you want to see the graphics
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])
21 square_img = pygame.Surface((self.grid_sz[0], self.grid_sz[1]),pygame.SRCALPHA) # A square image
22 while not self.stopped():
24 self.board.display_grid(window = self.window, grid_sz = self.grid_sz)
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)
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)
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)
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)
65 if self.moves != None:
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)
76 self.board.display_pieces(window = self.window, grid_sz = self.grid_sz)
80 for event in pygame.event.get():
81 if event.type == pygame.QUIT:
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]]
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)
95 self.moves = self.choice.last_moves
97 elif event.type == pygame.MOUSEBUTTONUP:
98 if self.choice == None:
100 self.choice.last_moves = self.last_moves
101 self.choice.selected_moves = self.selected_moves
105 pygame.display.quit()
107 if __name__ == "__main__":
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")