Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- square_size = 80
- pygame.init()
- screen_width = screen_height = 8 * square_size
- black = (0, 0, 0)
- white = (255, 255, 255)
- tan = (210, 180, 140)
- pink = (255, 0, 171)
- red = (225, 0, 0)
- purple = (55, 0, 55)
- light_orange = (248, 164, 0)
- yellow = (221, 249, 0)
- green = (108, 251, 0)
- turquoise = (0, 253, 119)
- colors = [purple, pink, red, light_orange, yellow, green, turquoise]
- clock = pygame.time.Clock()
- crashed = False
- screen = pygame.display.set_mode((screen_width, screen_height)) # a surface object
- screen.fill(white)
- pygame.display.set_caption("Chess Knights!")
- knight_move_list = [
- [2, 1],
- [1, 2],
- [-2, 1],
- [-1, 2],
- [2, -1],
- [1, -2],
- [-2, -1],
- [-1, -2]
- ]
- def knight_moves_distance(input_x = 1, input_y = 1):
- steps_to_position = 0
- moves_explored = [[input_x, input_y]]
- squares_distance = [[[input_x, input_y], 0]]
- while len(moves_explored) < 64: ## Keep going until we have explored 64 moves.
- temp_moves_list = []
- temp_distance_list = []
- steps_to_position = steps_to_position + 1
- for move in moves_explored: ## go through every square already visited
- for translation in knight_move_list: ## go through every knight move possible
- inter_varX = move[0] + translation[0]
- inter_varY = move[1] + translation[1]
- if (1 <= inter_varX <= 8)\
- and (1 <= inter_varY <= 8)\
- and ([inter_varX, inter_varY] not in moves_explored):
- temp_moves_list.append([inter_varX, inter_varY])
- temp_distance_list.append([[inter_varX, inter_varY], steps_to_position])
- for element in temp_moves_list:
- if element not in moves_explored:
- moves_explored.append(element)
- for element in temp_distance_list:
- if element not in squares_distance:
- squares_distance.append(element)
- return squares_distance
- def board_draw():
- for x in range(8):
- for y in range(8):
- square_rect = (x * square_size, y * square_size, square_size, square_size)
- if (x + y) % 2 == 0:
- screen.fill(tan, square_rect)
- else:
- screen.fill(black, square_rect)
- pygame.display.update()
- def re_color_board(list):
- temp_dict = {}
- for positions in list:
- temp_dict[(positions[0][0], positions[0][1])] = positions[1]
- print(positions)
- for x in range(8):
- for y in range(8):
- square_rect = (x * square_size, y * square_size, square_size, square_size)
- screen.fill(colors[temp_dict[(x+1, y+1)]], square_rect)
- board_draw()
- while not crashed:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- crashed = True
- if event.type == pygame.MOUSEBUTTONDOWN:
- print(event.pos)
- knight_list = knight_moves_distance(math.ceil(event.pos[0]/80), math.ceil(event.pos[1]/80))
- re_color_board(knight_list)
- distance_sum = 0
- for position in knight_list:
- distance_sum += position[1]
- print("average distance per square: {:02.4}".format(distance_sum/64))
- print("mobility is: {:06f}".format(1/(distance_sum/64)))
- pygame.display.update()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement