Advertisement
_Jacques

newChessMobility

Sep 29th, 2020
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | None | 0 0
  1. import pygame
  2. import math
  3. from color_gradients import RGB_to_hex, polylinear_gradient
  4.  
  5. square_size = 8
  6. board_dimension = 80
  7. pygame.init()
  8.  
  9. screen_width = screen_height = board_dimension * square_size
  10.  
  11.  
  12.  
  13. red = (225, 0, 0)
  14. purple = (55, 0, 55)
  15. yellow = (255, 255, 0)
  16. green = (0, 255, 0)
  17. blue = (0, 0, 255)
  18. magenta = (255, 0, 255)
  19. tan = (210, 180, 140)
  20. black = (0, 0, 0)
  21. white = (255, 255, 255)
  22.  
  23. colorPoints = [black, magenta, blue, green, yellow, red, white]
  24. colorPoints.reverse()
  25.  
  26. color_discrete_RGB_list = [RGB_to_hex(RGB) for RGB in colorPoints]
  27.  
  28. intermediate_list = (polylinear_gradient(color_discrete_RGB_list, 36))
  29.  
  30.  
  31. colors_long_list = []
  32.  
  33. for color_number in range(len(intermediate_list["r"])):
  34.     RGB = (intermediate_list["r"][color_number], intermediate_list["g"][color_number], intermediate_list["b"][color_number])
  35.     colors_long_list.append(RGB)
  36.  
  37. print(colors_long_list)
  38. depth = len(colors_long_list)
  39. clock = pygame.time.Clock()
  40.  
  41. crashed = False
  42.  
  43. screen = pygame.display.set_mode((screen_width, screen_height))  # a surface object
  44. screen.fill(white)
  45. pygame.display.set_caption("Chess Knights!")
  46.  
  47. knight_move_list = [
  48.                     [2, 1],
  49.                     [1, 2],
  50.                     [-2, 1],
  51.                     [-1, 2],
  52.                     [2, -1],
  53.                     [1, -2],
  54.                     [-2, -1],
  55.                     [-1, -2]
  56.                     ]
  57.  
  58. def knight_moves_distance(input_x = 1, input_y = 1):
  59.     steps_to_position = 0
  60.     moves_explored = [[input_x, input_y]]
  61.     squares_distance = [[[input_x, input_y], 0]]
  62.     while steps_to_position < depth:  ## Keep going until we have reach every square x moves awway
  63.         temp_moves_list = []
  64.         temp_distance_list = []
  65.         steps_to_position = steps_to_position + 1
  66.         for move in moves_explored:  ## go through every square already visited
  67.             for translation in knight_move_list:  ## go through every knight move possible
  68.                 inter_varX = move[0] + translation[0] ##perform a knight move to a position, and store this new position
  69.                 inter_varY = move[1] + translation[1]
  70.                 if ([inter_varX, inter_varY] not in moves_explored):## if this position hasn't been explored, add to temp list
  71.                     temp_moves_list.append([inter_varX, inter_varY])
  72.                     temp_distance_list.append([[inter_varX, inter_varY], steps_to_position])
  73.         for element in temp_moves_list: ## go through the temp list, and add each position if it isn't already on the list, to avoid redundancy
  74.             if element not in moves_explored:
  75.                 moves_explored.append(element)
  76.         for element in temp_distance_list:
  77.             if element not in squares_distance:
  78.                 squares_distance.append(element)
  79.     return squares_distance
  80.  
  81. def board_draw():
  82.     for x in range(board_dimension):
  83.         for y in range(board_dimension):
  84.             square_rect = (x * square_size, y * square_size, square_size, square_size)
  85.             if (x + y) % 2 == 0:
  86.                 screen.fill(tan, square_rect)
  87.             else:
  88.                 screen.fill(black, square_rect)
  89.             pygame.display.update()
  90.  
  91. def re_color_board(list):
  92.     temp_dict = {}
  93.     for positions in list:
  94.         temp_dict[(positions[0][0], positions[0][1])] = positions[1] #create a dict where each key is a position and each value is the number of moves it takes to get to that position.
  95.         print(positions)
  96.     for x in range(board_dimension):
  97.         for y in range(board_dimension):
  98.             square_rect = (x * square_size, y * square_size, square_size, square_size)
  99.             screen.fill(colors_long_list[temp_dict[(x+1, y+1)]], square_rect)
  100.  
  101. board_draw()
  102.  
  103. while not crashed:
  104.     for event in pygame.event.get():
  105.         if event.type == pygame.QUIT:
  106.             crashed = True
  107.         if event.type == pygame.MOUSEBUTTONDOWN:
  108.             print(event.pos)
  109.             knight_list = knight_moves_distance(math.ceil(event.pos[0]/square_size), math.ceil(event.pos[1]/square_size))
  110.             re_color_board(knight_list)
  111.             distance_sum = 0
  112.             for position in knight_list:
  113.                 distance_sum += position[1]
  114.             print("average distance per square: {:02.4}".format(distance_sum/board_dimension**2))
  115.             print("mobility is: {:06f}".format(1/(distance_sum/board_dimension**2)))
  116.  
  117.     pygame.display.update()
  118.     clock.tick(60)
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement