# ChessKnightMobility

Sep 29th, 2020
862
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import pygame
2. import math
3.
4. square_size = 80
5. pygame.init()
6.
7. screen_width = screen_height = 8 * square_size
8.
9. black = (0, 0, 0)
10. white = (255, 255, 255)
11. tan = (210, 180, 140)
12.
13. pink = (255, 0, 171)
14. red = (225, 0, 0)
15. purple = (55, 0, 55)
16. light_orange = (248, 164, 0)
17. yellow = (221, 249, 0)
18. green = (108, 251, 0)
19. turquoise = (0, 253, 119)
20.
21.
22. colors = [purple, pink, red, light_orange, yellow, green, turquoise]
23. clock = pygame.time.Clock()
24.
25. crashed = False
26.
27. screen = pygame.display.set_mode((screen_width, screen_height))  # a surface object
28. screen.fill(white)
29. pygame.display.set_caption("Chess Knights!")
30.
31. knight_move_list = [
32.                     [2, 1],
33.                     [1, 2],
34.                     [-2, 1],
35.                     [-1, 2],
36.                     [2, -1],
37.                     [1, -2],
38.                     [-2, -1],
39.                     [-1, -2]
40.                     ]
41.
42. def knight_moves_distance(input_x = 1, input_y = 1):
43.     steps_to_position = 0
44.     moves_explored = [[input_x, input_y]]
45.     squares_distance = [[[input_x, input_y], 0]]
46.     while len(moves_explored) < 64:  ## Keep going until we have explored 64 moves.
47.         temp_moves_list = []
48.         temp_distance_list = []
49.         steps_to_position = steps_to_position + 1
50.         for move in moves_explored:  ## go through every square already visited
51.             for translation in knight_move_list:  ## go through every knight move possible
52.                 inter_varX = move[0] + translation[0]
53.                 inter_varY = move[1] + translation[1]
54.                 if (1 <= inter_varX <= 8)\
55.                 and (1 <= inter_varY <= 8)\
56.                 and ([inter_varX, inter_varY] not in moves_explored):
57.                     temp_moves_list.append([inter_varX, inter_varY])
58.                     temp_distance_list.append([[inter_varX, inter_varY], steps_to_position])
59.         for element in temp_moves_list:
60.             if element not in moves_explored:
61.                 moves_explored.append(element)
62.         for element in temp_distance_list:
63.             if element not in squares_distance:
64.                 squares_distance.append(element)
65.     return squares_distance
66.
67. def board_draw():
68.     for x in range(8):
69.         for y in range(8):
70.             square_rect = (x * square_size, y * square_size, square_size, square_size)
71.             if (x + y) % 2 == 0:
72.                 screen.fill(tan, square_rect)
73.             else:
74.                 screen.fill(black, square_rect)
75.             pygame.display.update()
76.
77. def re_color_board(list):
78.     temp_dict = {}
79.     for positions in list:
80.         temp_dict[(positions[0][0], positions[0][1])] = positions[1]
81.         print(positions)
82.     for x in range(8):
83.         for y in range(8):
84.             square_rect = (x * square_size, y * square_size, square_size, square_size)
85.             screen.fill(colors[temp_dict[(x+1, y+1)]], square_rect)
86.
87. board_draw()
88.
89. while not crashed:
90.     for event in pygame.event.get():
91.         if event.type == pygame.QUIT:
92.             crashed = True
93.         if event.type == pygame.MOUSEBUTTONDOWN:
94.             print(event.pos)
95.             knight_list = knight_moves_distance(math.ceil(event.pos[0]/80), math.ceil(event.pos[1]/80))
96.             re_color_board(knight_list)
97.             distance_sum = 0
98.             for position in knight_list:
99.                 distance_sum += position[1]
100.             print("average distance per square: {:02.4}".format(distance_sum/64))
101.             print("mobility is: {:06f}".format(1/(distance_sum/64)))
102.
103.     pygame.display.update()
104.     clock.tick(60)
RAW Paste Data