120 lines
4.1 KiB
Python
120 lines
4.1 KiB
Python
from typing import Tuple, List
|
|
|
|
import pygame
|
|
|
|
from algorithms.genetic.const import MAP_ALIASES
|
|
from common.constants import GRID_CELL_PADDING, GRID_CELL_SIZE, COLUMNS, ROWS, CLASSES, CLASS_TO_ID
|
|
import csv
|
|
import os
|
|
|
|
from common.constants import GRID_CELL_PADDING, GRID_CELL_SIZE
|
|
from common.constants import ROWS, COLUMNS, LEFT, RIGHT, UP, DOWN
|
|
|
|
directions = {
|
|
LEFT: (0, -1),
|
|
RIGHT: (0, 1),
|
|
UP: (-1, 0),
|
|
DOWN: (1, 0)
|
|
}
|
|
|
|
|
|
def draw_text(text, color, surface, x, y, text_size=30, is_bold=False):
|
|
if is_bold:
|
|
font = pygame.font.Font('resources/fonts/Poppins-SemiBold.ttf', text_size)
|
|
else:
|
|
font = pygame.font.Font('resources/fonts/Poppins-Regular.ttf', text_size)
|
|
textobj = font.render(text, 1, color)
|
|
textrect = textobj.get_rect()
|
|
textrect.topleft = (x, y)
|
|
surface.blit(textobj, textrect)
|
|
|
|
|
|
def createCSV():
|
|
train_data_path = './data/train'
|
|
test_data_path = './data/test'
|
|
|
|
if os.path.exists(train_data_path):
|
|
train_csvfile = open('./data/train_csv_file.csv', 'w', newline="")
|
|
writer = csv.writer(train_csvfile)
|
|
writer.writerow(["filepath", "type"])
|
|
|
|
for class_name in CLASSES:
|
|
class_dir = train_data_path + "/" + class_name
|
|
for filename in os.listdir(class_dir):
|
|
f = os.path.join(class_dir, filename)
|
|
if os.path.isfile(f):
|
|
writer.writerow([f, CLASS_TO_ID[class_name]])
|
|
|
|
train_csvfile.close()
|
|
|
|
else:
|
|
print("Brak plików do uczenia")
|
|
|
|
if os.path.exists(test_data_path):
|
|
test_csvfile = open('./data/test_csv_file.csv', 'w', newline="")
|
|
writer = csv.writer(test_csvfile)
|
|
writer.writerow(["filepath", "type"])
|
|
|
|
for class_name in CLASSES:
|
|
class_dir = test_data_path + "/" + class_name
|
|
for filename in os.listdir(class_dir):
|
|
f = os.path.join(class_dir, filename)
|
|
if os.path.isfile(f):
|
|
writer.writerow([f, CLASS_TO_ID[class_name]])
|
|
|
|
test_csvfile.close()
|
|
else:
|
|
print("Brak plików do testowania")
|
|
|
|
|
|
def print_numbers():
|
|
display_surface = pygame.display.get_surface()
|
|
font = pygame.font.SysFont('Arial', 16)
|
|
|
|
for row_index in range(ROWS):
|
|
for col_index in range(COLUMNS):
|
|
x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * col_index + GRID_CELL_PADDING + 7
|
|
y = (GRID_CELL_PADDING + GRID_CELL_SIZE) * row_index + GRID_CELL_PADDING + 16
|
|
display_surface.blit(font.render(f'[{col_index}, {row_index}]', True, (255, 0, 0)), (x, y))
|
|
pygame.display.update()
|
|
|
|
|
|
# parse array index to screen x or y coordinate
|
|
def parse_cord(cord):
|
|
return (GRID_CELL_PADDING + GRID_CELL_SIZE) * cord + GRID_CELL_PADDING + 7
|
|
|
|
|
|
def castle_neighbors(map, castle_bottom_right_row, castle_bottom_right_col):
|
|
neighbors = []
|
|
for row_add in range(-2, 2):
|
|
new_row = castle_bottom_right_row + row_add
|
|
if 0 <= new_row <= len(map) - 1:
|
|
for col_add in range(-2, 2):
|
|
new_col = castle_bottom_right_col + col_add
|
|
if 0 <= new_col <= len(map) - 1:
|
|
if (new_col == castle_bottom_right_col - 1 and new_row == castle_bottom_right_row - 1) \
|
|
or (new_col == castle_bottom_right_col and new_row == castle_bottom_right_row - 1) \
|
|
or (new_col == castle_bottom_right_col - 1 and new_row == castle_bottom_right_row) \
|
|
or (new_col == castle_bottom_right_col and new_row == castle_bottom_right_row):
|
|
continue
|
|
neighbors.append((new_col, new_row))
|
|
return neighbors
|
|
|
|
|
|
def find_neighbours(grid: List[List[int]], col: int, row: int) -> List[Tuple[int, int]]:
|
|
dr = [-1, 1, 0, 0]
|
|
dc = [0, 0, -1, 1]
|
|
|
|
neighbours = []
|
|
|
|
for i in range(4):
|
|
rr = row + dr[i]
|
|
cc = col + dc[i]
|
|
|
|
if rr < 0 or cc < 0: continue
|
|
if rr >= ROWS or cc >= COLUMNS: continue
|
|
if grid[rr][cc] not in [MAP_ALIASES.get("GRASS"), MAP_ALIASES.get("SAND"), '.']: continue
|
|
neighbours.append((rr, cc))
|
|
|
|
return neighbours
|