5 graphics_enabled = True
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
11 So basically, using `./qchess.py @internal:AgentBishop` is better, unless you want to see the graphics
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])
23 square_img = pygame.Surface((self.grid_sz[0], self.grid_sz[1]),pygame.SRCALPHA) # A square image
24 while not self.stopped():
26 self.board.display_grid(window = self.window, grid_sz = self.grid_sz)
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)
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)
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)
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)
67 if self.moves != None:
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)
78 self.board.display_pieces(window = self.window, grid_sz = self.grid_sz)
82 for event in pygame.event.get():
83 if event.type == pygame.QUIT:
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]]
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)
97 self.moves = self.choice.last_moves
99 elif event.type == pygame.MOUSEBUTTONUP:
100 if self.choice == None:
102 self.choice.last_moves = self.last_moves
103 self.choice.selected_moves = self.selected_moves
107 pygame.display.quit()
109 if __name__ == "__main__":
111 if sys.argv[1] == "--no-graphics":
112 graphics_enabled = False
114 colour = sys.stdin.readline().strip("\r\n")
115 agent = AgentBishop(sys.argv[0], colour)
118 graphics = AgentBishop_Graphics(agent.board, "Agent Bishop ("+agent.colour+") DEBUG")