forked from s464965/WMICraft
Compare commits
64 Commits
problemy_z
...
master
Author | SHA1 | Date | |
---|---|---|---|
f4aa9741e3 | |||
|
3591fedfa2 | ||
050a0359b0 | |||
c6cc763fe1 | |||
08ea0e945e | |||
cb9b8e6b2d | |||
|
dfa408d5fa | ||
|
ebaf97d82c | ||
8666a7dbf9 | |||
952e8663b4 | |||
5f7fea24fb | |||
297bd19336 | |||
bab75274dc | |||
681bf08424 | |||
c8f0dc76b6 | |||
e31364b21c | |||
|
1482730bee | ||
|
29aea5bec4 | ||
9afd1f366a | |||
|
6ddfddb2fd | ||
|
3f68f4c8a6 | ||
59a8c49b2d | |||
|
fd4b34eed7 | ||
08329f187b | |||
|
fd081d5c56 | ||
ee7a324e5d | |||
|
10e61e724c | ||
|
f255178162 | ||
d80e83cecf | |||
|
ea105ad140 | ||
32847593e6 | |||
|
f1f2302acd | ||
|
e89f564dd6 | ||
162e2df890 | |||
|
e9ef300dde | ||
|
c7c1feb82c | ||
|
7a9d685701 | ||
|
c06624d79c | ||
8d73a85707 | |||
b6ba817d55 | |||
5c1a1605b8 | |||
97628965f1 | |||
e8c32ade2a | |||
c6697bdc79 | |||
088e90ec5b | |||
4d88b300ae | |||
94b4cbef58 | |||
4e3e68d4c3 | |||
|
431113a04c | ||
|
736dbc9616 | ||
7ce2f0cc38 | |||
dc411fae42 | |||
|
3fce0a5b57 | ||
|
89c307535b | ||
30e2d39d28 | |||
504cfd1af0 | |||
|
33577c1824 | ||
|
1a4f245087 | ||
|
8b76d3635b | ||
|
9aa9552499 | ||
4ab8065879 | |||
9f3c5fb2d5 | |||
7030488a8a | |||
f9c5f331f2 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -149,4 +149,5 @@ cython_debug/
|
|||||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
.idea/
|
.idea/
|
||||||
|
/algorithms/neural_network/data/
|
||||||
|
@ -4,25 +4,16 @@ import heapq
|
|||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Tuple, Optional, List
|
from typing import Tuple, Optional, List
|
||||||
|
|
||||||
from common.constants import ROWS, COLUMNS
|
from algorithms.genetic.const import MAP_ALIASES
|
||||||
|
from common.constants import ROWS, COLUMNS, LEFT, RIGHT, UP, DOWN
|
||||||
|
from common.helpers import directions
|
||||||
|
|
||||||
EMPTY_FIELDS = ['s', 'g', ' ']
|
EMPTY_FIELDS = [MAP_ALIASES.get("SAND"), MAP_ALIASES.get("GRASS"), ' ']
|
||||||
LEFT = 'LEFT'
|
|
||||||
RIGHT = 'RIGHT'
|
|
||||||
UP = 'UP'
|
|
||||||
DOWN = 'DOWN'
|
|
||||||
|
|
||||||
TURN_LEFT = 'TURN_LEFT'
|
TURN_LEFT = 'TURN_LEFT'
|
||||||
TURN_RIGHT = 'TURN_RIGHT'
|
TURN_RIGHT = 'TURN_RIGHT'
|
||||||
FORWARD = 'FORWARD'
|
FORWARD = 'FORWARD'
|
||||||
|
|
||||||
directions = {
|
|
||||||
LEFT: (0, -1),
|
|
||||||
RIGHT: (0, 1),
|
|
||||||
UP: (-1, 0),
|
|
||||||
DOWN: (1, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class State:
|
class State:
|
||||||
|
@ -51,7 +51,7 @@ def graphsearch(initial_state: State, map, goal_list, fringe: List[Node] = None,
|
|||||||
explored_states = set()
|
explored_states = set()
|
||||||
fringe_states = set()
|
fringe_states = set()
|
||||||
|
|
||||||
# root Node
|
# train Node
|
||||||
fringe.append(Node(initial_state))
|
fringe.append(Node(initial_state))
|
||||||
fringe_states.add((initial_state.row, initial_state.column, initial_state.direction))
|
fringe_states.add((initial_state.row, initial_state.column, initial_state.direction))
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ def graphsearch(initial_state: State, map, goal_list, fringe: List[Node] = None,
|
|||||||
parent = element.parent
|
parent = element.parent
|
||||||
|
|
||||||
while parent is not None:
|
while parent is not None:
|
||||||
# root's action will be None, don't add it
|
# train's action will be None, don't add it
|
||||||
if parent.action is not None:
|
if parent.action is not None:
|
||||||
actions_sequence.append(parent.action)
|
actions_sequence.append(parent.action)
|
||||||
parent = parent.parent
|
parent = parent.parent
|
||||||
|
142
algorithms/genetic/common.py
Normal file
142
algorithms/genetic/common.py
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from const import *
|
||||||
|
from typing import List, Dict, Tuple
|
||||||
|
|
||||||
|
import numpy.typing as npt
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Position:
|
||||||
|
row: int
|
||||||
|
col: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Area:
|
||||||
|
position: Position
|
||||||
|
width: int
|
||||||
|
height: int
|
||||||
|
|
||||||
|
|
||||||
|
AREAS_TO_CROSS = [
|
||||||
|
# up above left knights spawn
|
||||||
|
Area(position=Position(row=0, col=0),
|
||||||
|
width=KNIGHTS_SPAWN_WIDTH,
|
||||||
|
height=LEFT_KNIGHTS_SPAWN_FIRST_ROW),
|
||||||
|
|
||||||
|
# down below left knights spawn
|
||||||
|
Area(position=Position(row=LEFT_KNIGHTS_SPAWN_FIRST_ROW + KNIGHTS_SPAWN_HEIGHT, col=0),
|
||||||
|
width=KNIGHTS_SPAWN_WIDTH,
|
||||||
|
height=ROWS - LEFT_KNIGHTS_SPAWN_FIRST_ROW - KNIGHTS_SPAWN_HEIGHT),
|
||||||
|
|
||||||
|
# between left knights spawn and castle
|
||||||
|
Area(position=Position(row=0, col=KNIGHTS_SPAWN_WIDTH),
|
||||||
|
width=CASTLE_SPAWN_FIRST_COL - KNIGHTS_SPAWN_WIDTH,
|
||||||
|
height=ROWS),
|
||||||
|
|
||||||
|
# up above castle
|
||||||
|
Area(position=Position(row=0, col=CASTLE_SPAWN_FIRST_COL),
|
||||||
|
width=2,
|
||||||
|
height=CASTLE_SPAWN_FIRST_ROW),
|
||||||
|
|
||||||
|
# down below castle
|
||||||
|
Area(position=Position(row=CASTLE_SPAWN_FIRST_ROW + 2, col=CASTLE_SPAWN_FIRST_COL),
|
||||||
|
width=2,
|
||||||
|
height=ROWS - CASTLE_SPAWN_FIRST_ROW - 2),
|
||||||
|
|
||||||
|
# between castle and right knights spawn
|
||||||
|
Area(position=Position(row=0, col=CASTLE_SPAWN_FIRST_COL + 2),
|
||||||
|
width=RIGHT_KNIGHTS_SPAWN_FIRST_COL - CASTLE_SPAWN_FIRST_COL - 2,
|
||||||
|
height=ROWS),
|
||||||
|
|
||||||
|
# up above right knights spawn
|
||||||
|
Area(position=Position(row=0, col=RIGHT_KNIGHTS_SPAWN_FIRST_COL),
|
||||||
|
width=KNIGHTS_SPAWN_WIDTH,
|
||||||
|
height=RIGHT_KNIGHTS_SPAWN_FIRST_ROW),
|
||||||
|
|
||||||
|
# down below right knights spawn
|
||||||
|
Area(position=Position(row=RIGHT_KNIGHTS_SPAWN_FIRST_ROW + KNIGHTS_SPAWN_HEIGHT, col=RIGHT_KNIGHTS_SPAWN_FIRST_COL),
|
||||||
|
width=KNIGHTS_SPAWN_WIDTH,
|
||||||
|
height=ROWS - RIGHT_KNIGHTS_SPAWN_FIRST_ROW - KNIGHTS_SPAWN_HEIGHT),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def dfs(grid: npt.NDArray, visited: Dict[Tuple[int, int], bool], position: Position, rows: int, cols: int) -> None:
|
||||||
|
visited[(position.row, position.col)] = True
|
||||||
|
|
||||||
|
row_vector = [0, 0, 1, -1]
|
||||||
|
col_vector = [-1, 1, 0, 0]
|
||||||
|
|
||||||
|
neighbours = []
|
||||||
|
for i in range(4):
|
||||||
|
rr = position.row + row_vector[i]
|
||||||
|
cc = position.col + col_vector[i]
|
||||||
|
if rr < 0 or rr >= ROWS:
|
||||||
|
continue
|
||||||
|
elif cc < 0 or cc >= COLUMNS:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
p = Position(rr, cc)
|
||||||
|
if (p.row, p.col) in visited:
|
||||||
|
neighbours.append(p)
|
||||||
|
|
||||||
|
for neighbour in neighbours:
|
||||||
|
if not visited[(neighbour.row, neighbour.col)]:
|
||||||
|
dfs(grid, visited, neighbour, rows, cols)
|
||||||
|
|
||||||
|
|
||||||
|
def get_islands(grid: npt.NDArray, positions: List[Position], rows: int = ROWS, cols: int = COLUMNS) -> List[Position]:
|
||||||
|
"""it returns list of all islands roots"""
|
||||||
|
visited = {}
|
||||||
|
|
||||||
|
for position in positions:
|
||||||
|
visited[(position.row, position.col)] = False
|
||||||
|
|
||||||
|
islands = 0
|
||||||
|
roots = []
|
||||||
|
for position in positions:
|
||||||
|
if not visited[(position.row, position.col)]:
|
||||||
|
dfs(grid, visited, position, rows, cols)
|
||||||
|
roots.append(position)
|
||||||
|
islands += 1
|
||||||
|
|
||||||
|
return roots
|
||||||
|
|
||||||
|
|
||||||
|
def find_neighbours(grid: npt.NDArray, col: int, row: int) -> List[Position]:
|
||||||
|
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 0 <= rr < ROWS and 0 <= cc < COLUMNS and grid[rr][cc] == MAP_ALIASES.get('GRASS'):
|
||||||
|
neighbours.append(Position(row=rr, col=cc))
|
||||||
|
|
||||||
|
return neighbours
|
||||||
|
|
||||||
|
|
||||||
|
def get_tiles_positions(grid: npt.NDArray):
|
||||||
|
sands = []
|
||||||
|
trees = []
|
||||||
|
waters = []
|
||||||
|
monsters = []
|
||||||
|
|
||||||
|
for row_num in range(len(grid)):
|
||||||
|
for col_num in range(len(grid[row_num])):
|
||||||
|
if grid[row_num][col_num] == MAP_ALIASES.get('WATER'):
|
||||||
|
waters.append(Position(row=row_num, col=col_num))
|
||||||
|
elif grid[row_num][col_num] == MAP_ALIASES.get('TREE'):
|
||||||
|
trees.append(Position(row=row_num, col=col_num))
|
||||||
|
elif grid[row_num][col_num] == MAP_ALIASES.get('SAND'):
|
||||||
|
sands.append(Position(row=row_num, col=col_num))
|
||||||
|
elif grid[row_num][col_num] == MAP_ALIASES.get('MONSTER'):
|
||||||
|
monsters.append(Position(row=row_num, col=col_num))
|
||||||
|
|
||||||
|
return sands, trees, waters, monsters
|
29
algorithms/genetic/const.py
Normal file
29
algorithms/genetic/const.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# map config
|
||||||
|
KNIGHTS_PER_TEAM_COUNT = 4
|
||||||
|
SAND_COUNT = 21
|
||||||
|
WATER_COUNT = 21
|
||||||
|
TREE_COUNT = 37
|
||||||
|
MONSTERS_COUNT = 2
|
||||||
|
CASTLES_COUNT = 1
|
||||||
|
ROWS = 19
|
||||||
|
COLUMNS = 24
|
||||||
|
KNIGHTS_SPAWN_WIDTH = 4
|
||||||
|
KNIGHTS_SPAWN_HEIGHT = 7
|
||||||
|
LEFT_KNIGHTS_SPAWN_FIRST_ROW = 6
|
||||||
|
LEFT_KNIGHTS_SPAWN_FIRST_COL = 0
|
||||||
|
RIGHT_KNIGHTS_SPAWN_FIRST_ROW = 6
|
||||||
|
RIGHT_KNIGHTS_SPAWN_FIRST_COL = 20
|
||||||
|
CASTLE_SPAWN_FIRST_ROW = 7
|
||||||
|
CASTLE_SPAWN_FIRST_COL = 11
|
||||||
|
|
||||||
|
# map aliases
|
||||||
|
MAP_ALIASES = {
|
||||||
|
"GRASS": 0,
|
||||||
|
"SAND": 1,
|
||||||
|
"WATER": 2,
|
||||||
|
"TREE": 3,
|
||||||
|
"MONSTER": 4,
|
||||||
|
"CASTLE": 5,
|
||||||
|
"KNIGHT_RED": 6,
|
||||||
|
"KNIGHT_BLUE": 7,
|
||||||
|
}
|
166
algorithms/genetic/genome.py
Normal file
166
algorithms/genetic/genome.py
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
import math
|
||||||
|
import random
|
||||||
|
from copy import deepcopy
|
||||||
|
from random import randrange
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import numpy.typing as npt
|
||||||
|
|
||||||
|
from common import Position, get_islands, AREAS_TO_CROSS, find_neighbours, get_tiles_positions
|
||||||
|
from const import *
|
||||||
|
|
||||||
|
|
||||||
|
class Genome:
|
||||||
|
grid: npt.NDArray
|
||||||
|
knights_red: List[Position]
|
||||||
|
knights_blue: List[Position]
|
||||||
|
waters: List[Position]
|
||||||
|
trees: List[Position]
|
||||||
|
sands: List[Position]
|
||||||
|
monsters: List[Position]
|
||||||
|
fitness: int
|
||||||
|
sand_islands: List[Position]
|
||||||
|
tree_islands: List[Position]
|
||||||
|
water_islands: List[Position]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.grid = np.zeros((ROWS, COLUMNS), dtype=int)
|
||||||
|
self.fitness = 0
|
||||||
|
|
||||||
|
self.knights_red = spawn_objects_in_given_area(
|
||||||
|
grid=self.grid,
|
||||||
|
object_alias=MAP_ALIASES.get("KNIGHT_RED"),
|
||||||
|
objects_count=KNIGHTS_PER_TEAM_COUNT,
|
||||||
|
spawn_position_start=Position(row=LEFT_KNIGHTS_SPAWN_FIRST_ROW, col=LEFT_KNIGHTS_SPAWN_FIRST_COL),
|
||||||
|
width=KNIGHTS_SPAWN_WIDTH,
|
||||||
|
height=KNIGHTS_SPAWN_HEIGHT
|
||||||
|
)
|
||||||
|
|
||||||
|
self.knights_blue = spawn_objects_in_given_area(
|
||||||
|
grid=self.grid,
|
||||||
|
object_alias=MAP_ALIASES.get("KNIGHT_BLUE"),
|
||||||
|
objects_count=KNIGHTS_PER_TEAM_COUNT,
|
||||||
|
spawn_position_start=Position(row=RIGHT_KNIGHTS_SPAWN_FIRST_ROW, col=RIGHT_KNIGHTS_SPAWN_FIRST_COL),
|
||||||
|
width=KNIGHTS_SPAWN_WIDTH,
|
||||||
|
height=KNIGHTS_SPAWN_HEIGHT
|
||||||
|
)
|
||||||
|
|
||||||
|
spawn_objects_in_given_area(
|
||||||
|
grid=self.grid,
|
||||||
|
object_alias=MAP_ALIASES.get("CASTLE"),
|
||||||
|
objects_count=4,
|
||||||
|
spawn_position_start=Position(row=CASTLE_SPAWN_FIRST_ROW, col=CASTLE_SPAWN_FIRST_COL),
|
||||||
|
width=2,
|
||||||
|
height=2
|
||||||
|
)
|
||||||
|
|
||||||
|
self.waters = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("WATER"),
|
||||||
|
objects_count=WATER_COUNT)
|
||||||
|
self.trees = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("TREE"),
|
||||||
|
objects_count=TREE_COUNT)
|
||||||
|
self.sands = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("SAND"),
|
||||||
|
objects_count=SAND_COUNT)
|
||||||
|
self.monsters = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("MONSTER"),
|
||||||
|
objects_count=MONSTERS_COUNT)
|
||||||
|
|
||||||
|
self.sand_islands = get_islands(self.grid, self.sands)
|
||||||
|
self.tree_islands = get_islands(self.grid, self.trees)
|
||||||
|
self.water_islands = get_islands(self.grid, self.waters)
|
||||||
|
|
||||||
|
def update_map(self):
|
||||||
|
self.sands, self.trees, self.waters, self.monsters = get_tiles_positions(self.grid)
|
||||||
|
|
||||||
|
self.sand_islands = get_islands(self.grid, self.sands)
|
||||||
|
self.tree_islands = get_islands(self.grid, self.trees)
|
||||||
|
self.water_islands = get_islands(self.grid, self.waters)
|
||||||
|
|
||||||
|
def calc_fitness(self):
|
||||||
|
score = SAND_COUNT + TREE_COUNT + WATER_COUNT
|
||||||
|
score = score - len(self.sand_islands) - len(self.tree_islands) - len(self.water_islands)
|
||||||
|
|
||||||
|
sands, trees, waters, monsters = get_tiles_positions(self.grid)
|
||||||
|
|
||||||
|
if len(monsters) != MONSTERS_COUNT:
|
||||||
|
self.fitness = 0
|
||||||
|
return
|
||||||
|
|
||||||
|
if len(sands) < SAND_COUNT or len(trees) < TREE_COUNT or len(waters) < WATER_COUNT:
|
||||||
|
self.fitness = 5
|
||||||
|
return
|
||||||
|
|
||||||
|
self.fitness = score
|
||||||
|
|
||||||
|
def crossover(self, partner):
|
||||||
|
# replace a randomly selected part of the grid with partner's part
|
||||||
|
child = Genome()
|
||||||
|
child.grid = deepcopy(self.grid)
|
||||||
|
area_to_cross = random.choice(AREAS_TO_CROSS)
|
||||||
|
|
||||||
|
for row in range(area_to_cross.position.row, area_to_cross.position.row + area_to_cross.height):
|
||||||
|
for col in range(area_to_cross.position.col, area_to_cross.position.col + area_to_cross.width):
|
||||||
|
child.grid[row][col] = partner.grid[row][col]
|
||||||
|
|
||||||
|
child.update_map()
|
||||||
|
|
||||||
|
return child
|
||||||
|
|
||||||
|
def mutate(self, mutation_rate: float):
|
||||||
|
# remove 1 item from a random island and add a neighbor to another island
|
||||||
|
if random.random() < mutation_rate:
|
||||||
|
|
||||||
|
# select islands of the same, random type
|
||||||
|
islands_of_same_type = random.choice([self.sand_islands, self.tree_islands, self.water_islands])
|
||||||
|
random_index = random.randint(0, len(islands_of_same_type) - 1)
|
||||||
|
island = islands_of_same_type[random_index]
|
||||||
|
next_island = islands_of_same_type[(random_index + 1) % len(islands_of_same_type)]
|
||||||
|
|
||||||
|
free_tiles_nearby = find_neighbours(self.grid, next_island.col, next_island.row)
|
||||||
|
|
||||||
|
tile_type = self.grid[island.row][island.col]
|
||||||
|
self.grid[island.row][island.col] = MAP_ALIASES.get('GRASS')
|
||||||
|
|
||||||
|
# todo: if there are no free tiles around then randomize another next_island
|
||||||
|
if len(free_tiles_nearby) > 0:
|
||||||
|
random_free_tile = random.choice(free_tiles_nearby)
|
||||||
|
island.row = random_free_tile.row
|
||||||
|
island.col = random_free_tile.col
|
||||||
|
self.grid[island.row][island.col] = tile_type
|
||||||
|
|
||||||
|
self.update_map()
|
||||||
|
|
||||||
|
|
||||||
|
def is_empty(grid: npt.NDArray, position: Position) -> bool:
|
||||||
|
return grid[position.row, position.col] in [MAP_ALIASES.get("GRASS"), MAP_ALIASES.get("SAND")]
|
||||||
|
|
||||||
|
|
||||||
|
def is_invalid_area(spawn_position_start, height, width) -> bool:
|
||||||
|
return spawn_position_start.row + height - 1 < 0 or \
|
||||||
|
spawn_position_start.row + height - 1 >= ROWS or \
|
||||||
|
spawn_position_start.col + width - 1 < 0 or \
|
||||||
|
spawn_position_start.col + width - 1 >= COLUMNS
|
||||||
|
|
||||||
|
|
||||||
|
def spawn_objects_in_given_area(grid: npt.NDArray,
|
||||||
|
object_alias: str,
|
||||||
|
objects_count: int = 1,
|
||||||
|
spawn_position_start: Position = Position(row=0, col=0),
|
||||||
|
width: int = COLUMNS,
|
||||||
|
height: int = ROWS) -> List[Position]:
|
||||||
|
if is_invalid_area(spawn_position_start, height, width):
|
||||||
|
raise ValueError("Invalid spawn area")
|
||||||
|
|
||||||
|
objects_remaining = int(objects_count)
|
||||||
|
positions = []
|
||||||
|
|
||||||
|
while objects_remaining > 0:
|
||||||
|
row = randrange(spawn_position_start.row, spawn_position_start.row + height)
|
||||||
|
col = randrange(spawn_position_start.col, spawn_position_start.col + width)
|
||||||
|
position = Position(row=row, col=col)
|
||||||
|
|
||||||
|
if is_empty(grid=grid, position=position):
|
||||||
|
grid[position.row, position.col] = object_alias
|
||||||
|
positions.append(position)
|
||||||
|
objects_remaining -= 1
|
||||||
|
|
||||||
|
return positions
|
26
algorithms/genetic/map_generator.py
Normal file
26
algorithms/genetic/map_generator.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
from algorithms.genetic.genome import Genome
|
||||||
|
from algorithms.genetic.map_importer_exporter import export_map
|
||||||
|
from population import Population
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
population_size = 500
|
||||||
|
mutation_rate = 0.3
|
||||||
|
|
||||||
|
population = Population(mutation_rate, population_size, 55)
|
||||||
|
|
||||||
|
while not population.evaluate():
|
||||||
|
# create next generation
|
||||||
|
population.generate()
|
||||||
|
|
||||||
|
# calc fitness
|
||||||
|
population.calc_fitness()
|
||||||
|
|
||||||
|
print(population.best_genome.grid)
|
||||||
|
print("Fitness of the best: ", population.best_genome.fitness)
|
||||||
|
|
||||||
|
export_map(population.best_genome.grid)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
42
algorithms/genetic/map_importer_exporter.py
Normal file
42
algorithms/genetic/map_importer_exporter.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import json
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
import numpy
|
||||||
|
import numpy.typing as npt
|
||||||
|
from os import listdir
|
||||||
|
from os.path import isfile, join
|
||||||
|
|
||||||
|
|
||||||
|
# Save map to file
|
||||||
|
def export_map(grid: npt.NDArray):
|
||||||
|
json_data = {"map": grid.tolist()}
|
||||||
|
|
||||||
|
now = datetime.now()
|
||||||
|
file_name = "map_" + now.strftime("%Y_%m_%d_%H_%M_%S") + ".json"
|
||||||
|
path = Path("../../resources/maps/")
|
||||||
|
file_to_open = path / file_name
|
||||||
|
|
||||||
|
with open(file_to_open, "w+") as write_file:
|
||||||
|
json.dump(json_data, write_file)
|
||||||
|
print("Saved map to file " + file_name)
|
||||||
|
|
||||||
|
|
||||||
|
def import_random_map() -> object:
|
||||||
|
path = "resources/maps"
|
||||||
|
files = [f for f in listdir(path) if isfile(join(path, f))]
|
||||||
|
random_map_name = random.choice(files)
|
||||||
|
return import_map(random_map_name)
|
||||||
|
|
||||||
|
|
||||||
|
# Read map from file
|
||||||
|
def import_map(file_name: string) -> object:
|
||||||
|
file_to_open = "resources/maps/" + file_name
|
||||||
|
with open(file_to_open, "r") as read_file:
|
||||||
|
print("Reading map from file " + file_name)
|
||||||
|
decoded_json = json.load(read_file)
|
||||||
|
|
||||||
|
decoded_grid = numpy.asarray(decoded_json["map"])
|
||||||
|
print(decoded_grid)
|
||||||
|
return decoded_grid.tolist()
|
81
algorithms/genetic/population.py
Normal file
81
algorithms/genetic/population.py
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import random
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import numpy.typing as npt
|
||||||
|
|
||||||
|
from genome import Genome
|
||||||
|
|
||||||
|
|
||||||
|
class Population:
|
||||||
|
population: List[Genome] = [] # array to hold the current population
|
||||||
|
mating_pool: List[Genome] = [] # array which we will use for our "mating pool"
|
||||||
|
generations: int = 0 # number of generations
|
||||||
|
finished: bool = False # are we finished evolving?
|
||||||
|
mutation_rate: float
|
||||||
|
perfect_score: int
|
||||||
|
best_genome: Genome
|
||||||
|
|
||||||
|
def __init__(self, mutation_rate, population_size, perfect_score=20):
|
||||||
|
self.mutation_rate = mutation_rate
|
||||||
|
self.perfect_score = perfect_score
|
||||||
|
|
||||||
|
for i in range(0, population_size):
|
||||||
|
new_genome = Genome()
|
||||||
|
new_genome.calc_fitness()
|
||||||
|
self.population.append(new_genome)
|
||||||
|
|
||||||
|
# create a new generation
|
||||||
|
def generate(self):
|
||||||
|
max_fitness = 0
|
||||||
|
for genome in self.population:
|
||||||
|
if genome.fitness > max_fitness:
|
||||||
|
max_fitness = genome.fitness
|
||||||
|
|
||||||
|
print("Max fitness of generation " + str(self.generations) + " = " + str(max_fitness))
|
||||||
|
|
||||||
|
# refill the population with children from the mating pool
|
||||||
|
new_population = []
|
||||||
|
for genome in self.population:
|
||||||
|
partner_a = self.accept_reject(max_fitness)
|
||||||
|
partner_b = self.accept_reject(max_fitness)
|
||||||
|
child = partner_a.crossover(partner_b)
|
||||||
|
child.mutate(self.mutation_rate)
|
||||||
|
new_population.append(child)
|
||||||
|
|
||||||
|
self.population = new_population
|
||||||
|
self.generations += 1
|
||||||
|
|
||||||
|
# select random with correct probability from population
|
||||||
|
def accept_reject(self, max_fitness: int):
|
||||||
|
safe_flag = 0
|
||||||
|
|
||||||
|
while safe_flag < 10000:
|
||||||
|
partner = random.choice(self.population)
|
||||||
|
r = random.randint(0, max_fitness)
|
||||||
|
|
||||||
|
if r < partner.fitness:
|
||||||
|
return partner
|
||||||
|
|
||||||
|
safe_flag += 1
|
||||||
|
|
||||||
|
# compute the current "most fit" member of the population
|
||||||
|
def evaluate(self):
|
||||||
|
record = 0
|
||||||
|
best_index = 0
|
||||||
|
|
||||||
|
for index in range(len(self.population)):
|
||||||
|
genome = self.population[index]
|
||||||
|
if genome.fitness > record:
|
||||||
|
record = genome.fitness
|
||||||
|
best_index = index
|
||||||
|
|
||||||
|
self.best_genome = self.population[best_index]
|
||||||
|
if record >= self.perfect_score:
|
||||||
|
self.finished = True
|
||||||
|
|
||||||
|
return self.finished
|
||||||
|
|
||||||
|
def calc_fitness(self):
|
||||||
|
for genome in self.population:
|
||||||
|
genome.calc_fitness()
|
0
algorithms/neural_network/__init__.py
Normal file
0
algorithms/neural_network/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
@ -0,0 +1 @@
|
|||||||
|
{}
|
64
algorithms/neural_network/neural_network.py
Normal file
64
algorithms/neural_network/neural_network.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import torch
|
||||||
|
import pytorch_lightning as pl
|
||||||
|
import torch.nn as nn
|
||||||
|
from torch.optim import SGD, Adam, lr_scheduler
|
||||||
|
import torch.nn.functional as F
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
from watersandtreegrass import WaterSandTreeGrass
|
||||||
|
from common.constants import DEVICE, BATCH_SIZE, NUM_EPOCHS, LEARNING_RATE, SETUP_PHOTOS, ID_TO_CLASS
|
||||||
|
|
||||||
|
|
||||||
|
class NeuralNetwork(pl.LightningModule):
|
||||||
|
def __init__(self, numChannels=3, batch_size=BATCH_SIZE, learning_rate=LEARNING_RATE, num_classes=4):
|
||||||
|
super(NeuralNetwork, self).__init__()
|
||||||
|
self.conv1 = nn.Conv2d(numChannels, 24, (3, 3), padding=1)
|
||||||
|
self.relu1 = nn.ReLU()
|
||||||
|
self.maxpool1 = nn.MaxPool2d((2, 2), stride=2)
|
||||||
|
self.conv2 = nn.Conv2d(24, 48, (3, 3), padding=1)
|
||||||
|
self.relu2 = nn.ReLU()
|
||||||
|
self.fc1 = nn.Linear(48*18*18, 800)
|
||||||
|
self.relu3 = nn.ReLU()
|
||||||
|
self.fc2 = nn.Linear(800, 400)
|
||||||
|
self.relu4 = nn.ReLU()
|
||||||
|
self.fc3 = nn.Linear(400, 4)
|
||||||
|
self.logSoftmax = nn.LogSoftmax(dim=1)
|
||||||
|
|
||||||
|
self.batch_size = batch_size
|
||||||
|
self.learning_rate = learning_rate
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.conv1(x)
|
||||||
|
x = self.relu1(x)
|
||||||
|
x = self.maxpool1(x)
|
||||||
|
x = self.conv2(x)
|
||||||
|
x = self.relu2(x)
|
||||||
|
x = x.reshape(x.shape[0], -1)
|
||||||
|
x = self.fc1(x)
|
||||||
|
x = self.relu3(x)
|
||||||
|
x = self.fc2(x)
|
||||||
|
x = self.relu4(x)
|
||||||
|
x = self.fc3(x)
|
||||||
|
x = self.logSoftmax(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
def configure_optimizers(self):
|
||||||
|
optimizer = Adam(self.parameters(), lr=self.learning_rate)
|
||||||
|
return optimizer
|
||||||
|
|
||||||
|
def training_step(self, batch, batch_idx):
|
||||||
|
x, y = batch
|
||||||
|
scores = self(x)
|
||||||
|
loss = F.nll_loss(scores, y)
|
||||||
|
return loss
|
||||||
|
|
||||||
|
def validation_step(self, batch, batch_idx):
|
||||||
|
x, y = batch
|
||||||
|
scores = self(x)
|
||||||
|
val_loss = F.nll_loss(scores, y)
|
||||||
|
self.log("val_loss", val_loss, on_step=True, on_epoch=True, sync_dist=True)
|
||||||
|
|
||||||
|
def test_step(self, batch, batch_idx):
|
||||||
|
x, y = batch
|
||||||
|
scores = self(x)
|
||||||
|
test_loss = F.nll_loss(scores, y)
|
||||||
|
self.log("test_loss", test_loss, on_step=True, on_epoch=True, sync_dist=True)
|
125
algorithms/neural_network/neural_network_interface.py
Normal file
125
algorithms/neural_network/neural_network_interface.py
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
import torch
|
||||||
|
import common.helpers
|
||||||
|
from common.constants import DEVICE, BATCH_SIZE, NUM_EPOCHS, LEARNING_RATE, SETUP_PHOTOS, ID_TO_CLASS
|
||||||
|
from watersandtreegrass import WaterSandTreeGrass
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
from neural_network import NeuralNetwork
|
||||||
|
from torchvision.io import read_image, ImageReadMode
|
||||||
|
import torch.nn as nn
|
||||||
|
from torch.optim import Adam
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import pytorch_lightning as pl
|
||||||
|
from pytorch_lightning.callbacks import EarlyStopping
|
||||||
|
import torchvision.transforms.functional as F
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
def check_accuracy_tiles():
|
||||||
|
answer = 0
|
||||||
|
for i in range(100):
|
||||||
|
if what_is_it('../../resources/textures/grass_with_tree.jpg') == 'tree':
|
||||||
|
answer = answer + 1
|
||||||
|
print("Accuracy(%) grass_with_tree.jpg", answer)
|
||||||
|
|
||||||
|
answer = 0
|
||||||
|
for i in range(100):
|
||||||
|
if what_is_it('../../resources/textures/grass2.png') == 'grass':
|
||||||
|
answer = answer + 1
|
||||||
|
print("Accuracy(%) grass2.png", answer)
|
||||||
|
|
||||||
|
answer = 0
|
||||||
|
for i in range(100):
|
||||||
|
if what_is_it('../../resources/textures/grass3.png') == 'grass':
|
||||||
|
answer = answer + 1
|
||||||
|
print("Accuracy(%) grass3.png", answer)
|
||||||
|
|
||||||
|
answer = 0
|
||||||
|
for i in range(100):
|
||||||
|
if what_is_it('../../resources/textures/grass4.png') == 'grass':
|
||||||
|
answer = answer + 1
|
||||||
|
print("Accuracy(%) grass4.png", answer)
|
||||||
|
|
||||||
|
answer = 0
|
||||||
|
for i in range(100):
|
||||||
|
if what_is_it('../../resources/textures/grass1.png') == 'grass':
|
||||||
|
answer = answer + 1
|
||||||
|
print("Accuracy(%) grass1.png", answer)
|
||||||
|
|
||||||
|
answer = 0
|
||||||
|
for i in range(100):
|
||||||
|
if what_is_it('../../resources/textures/water.png') == 'water':
|
||||||
|
answer = answer + 1
|
||||||
|
print("Accuracy(%) water.png", answer)
|
||||||
|
|
||||||
|
answer = 0
|
||||||
|
for i in range(100):
|
||||||
|
if what_is_it('../../resources/textures/sand.png') == 'sand':
|
||||||
|
answer = answer + 1
|
||||||
|
print("Accuracy(%) sand.png", answer)
|
||||||
|
|
||||||
|
|
||||||
|
def what_is_it(img_path, show_img=False):
|
||||||
|
image = Image.open(img_path).convert('RGB')
|
||||||
|
if show_img:
|
||||||
|
plt.imshow(image)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
image = SETUP_PHOTOS(image).unsqueeze(0)
|
||||||
|
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_20/checkpoints/epoch=3-step=324.ckpt')
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
model.eval()
|
||||||
|
idx = int(model(image).argmax(dim=1))
|
||||||
|
return ID_TO_CLASS[idx]
|
||||||
|
|
||||||
|
|
||||||
|
def check_accuracy(tset):
|
||||||
|
model = NeuralNetwork.load_from_checkpoint('./lightning_logs/version_23/checkpoints/epoch=3-step=324.ckpt')
|
||||||
|
num_correct = 0
|
||||||
|
num_samples = 0
|
||||||
|
model = model.to(DEVICE)
|
||||||
|
model.eval()
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
for photo, label in tset:
|
||||||
|
photo = photo.to(DEVICE)
|
||||||
|
label = label.to(DEVICE)
|
||||||
|
|
||||||
|
scores = model(photo)
|
||||||
|
predictions = scores.argmax(dim=1)
|
||||||
|
num_correct += (predictions == label).sum()
|
||||||
|
num_samples += predictions.size(0)
|
||||||
|
|
||||||
|
print(f'Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}%')
|
||||||
|
|
||||||
|
|
||||||
|
def check_accuracy_data():
|
||||||
|
trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
|
testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
|
train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
|
||||||
|
test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
|
||||||
|
|
||||||
|
print("Accuracy of train_set:")
|
||||||
|
check_accuracy(train_loader)
|
||||||
|
print("Accuracy of test_set:")
|
||||||
|
check_accuracy(test_loader)
|
||||||
|
|
||||||
|
#CNN = NeuralNetwork()
|
||||||
|
#common.helpers.createCSV()
|
||||||
|
|
||||||
|
#trainer = pl.Trainer(accelerator='gpu', callbacks=EarlyStopping('val_loss'), devices=1, max_epochs=NUM_EPOCHS)
|
||||||
|
#trainer = pl.Trainer(accelerator='gpu', devices=1, auto_lr_find=True, max_epochs=NUM_EPOCHS)
|
||||||
|
|
||||||
|
#trainset = WaterSandTreeGrass('./data/train_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
|
#testset = WaterSandTreeGrass('./data/test_csv_file.csv', transform=SETUP_PHOTOS)
|
||||||
|
#train_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True)
|
||||||
|
#test_loader = DataLoader(testset, batch_size=BATCH_SIZE)
|
||||||
|
#trainer.fit(CNN, train_loader, test_loader)
|
||||||
|
#trainer.tune(CNN, train_loader, test_loader)
|
||||||
|
|
||||||
|
|
||||||
|
#print(what_is_it('../../resources/textures/grass2.png', True))
|
||||||
|
|
||||||
|
#check_accuracy_data()
|
||||||
|
|
||||||
|
#check_accuracy_tiles()
|
27
algorithms/neural_network/watersandtreegrass.py
Normal file
27
algorithms/neural_network/watersandtreegrass.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import torch
|
||||||
|
from torch.utils.data import Dataset
|
||||||
|
import pandas as pd
|
||||||
|
from torchvision.io import read_image, ImageReadMode
|
||||||
|
from common.helpers import createCSV
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
class WaterSandTreeGrass(Dataset):
|
||||||
|
def __init__(self, annotations_file, transform=None):
|
||||||
|
createCSV()
|
||||||
|
self.img_labels = pd.read_csv(annotations_file)
|
||||||
|
self.transform = transform
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.img_labels)
|
||||||
|
|
||||||
|
def __getitem__(self, idx):
|
||||||
|
image = Image.open(self.img_labels.iloc[idx, 0]).convert('RGB')
|
||||||
|
|
||||||
|
label = torch.tensor(int(self.img_labels.iloc[idx, 1]))
|
||||||
|
|
||||||
|
if self.transform:
|
||||||
|
image = self.transform(image)
|
||||||
|
|
||||||
|
return image, label
|
||||||
|
|
@ -1,10 +1,12 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
import torch
|
||||||
|
|
||||||
GAME_TITLE = 'WMICraft'
|
GAME_TITLE = 'WMICraft'
|
||||||
WINDOW_HEIGHT = 800
|
WINDOW_HEIGHT = 800
|
||||||
WINDOW_WIDTH = 1360
|
WINDOW_WIDTH = 1360
|
||||||
FPS_COUNT = 60
|
FPS_COUNT = 60
|
||||||
TURN_INTERVAL = 300
|
TURN_INTERVAL = 200
|
||||||
|
|
||||||
GRID_CELL_PADDING = 5
|
GRID_CELL_PADDING = 5
|
||||||
GRID_CELL_SIZE = 36
|
GRID_CELL_SIZE = 36
|
||||||
@ -63,7 +65,32 @@ ACTION = {
|
|||||||
"go": 0,
|
"go": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LEFT = 'LEFT'
|
||||||
|
RIGHT = 'RIGHT'
|
||||||
|
UP = 'UP'
|
||||||
|
DOWN = 'DOWN'
|
||||||
|
|
||||||
# HEALTH_BAR
|
# HEALTH_BAR
|
||||||
BAR_ANIMATION_SPEED = 1
|
BAR_ANIMATION_SPEED = 1
|
||||||
BAR_WIDTH_MULTIPLIER = 0.9 # (0;1>
|
BAR_WIDTH_MULTIPLIER = 0.9 # (0;1>
|
||||||
BAR_HEIGHT_MULTIPLIER = 0.1
|
BAR_HEIGHT_MULTIPLIER = 0.1
|
||||||
|
|
||||||
|
|
||||||
|
#NEURAL_NETWORK
|
||||||
|
LEARNING_RATE = 0.000630957344480193
|
||||||
|
BATCH_SIZE = 64
|
||||||
|
NUM_EPOCHS = 9
|
||||||
|
|
||||||
|
DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
||||||
|
print("Using ", DEVICE)
|
||||||
|
CLASSES = ['grass', 'sand', 'tree', 'water']
|
||||||
|
|
||||||
|
SETUP_PHOTOS = transforms.Compose([
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Resize((36, 36)),
|
||||||
|
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
|
||||||
|
])
|
||||||
|
|
||||||
|
ID_TO_CLASS = {i: j for i, j in enumerate(CLASSES)}
|
||||||
|
CLASS_TO_ID = {value: key for key, value in ID_TO_CLASS.items()}
|
||||||
|
|
||||||
|
@ -1,5 +1,21 @@
|
|||||||
|
from typing import Tuple, List
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
from common.constants import GRID_CELL_PADDING, GRID_CELL_SIZE, COLUMNS, ROWS
|
|
||||||
|
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):
|
def draw_text(text, color, surface, x, y, text_size=30, is_bold=False):
|
||||||
@ -13,6 +29,44 @@ def draw_text(text, color, surface, x, y, text_size=30, is_bold=False):
|
|||||||
surface.blit(textobj, textrect)
|
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():
|
def print_numbers():
|
||||||
display_surface = pygame.display.get_surface()
|
display_surface = pygame.display.get_surface()
|
||||||
font = pygame.font.SysFont('Arial', 16)
|
font = pygame.font.SysFont('Arial', 16)
|
||||||
@ -45,3 +99,21 @@ def castle_neighbors(map, castle_bottom_right_row, castle_bottom_right_col):
|
|||||||
continue
|
continue
|
||||||
neighbors.append((new_col, new_row))
|
neighbors.append((new_col, new_row))
|
||||||
return neighbors
|
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
|
||||||
|
0
learning/__init__.py
Normal file
0
learning/__init__.py
Normal file
1001
learning/dataset_tree_1000.csv
Normal file
1001
learning/dataset_tree_1000.csv
Normal file
File diff suppressed because it is too large
Load Diff
302
learning/dataset_tree_300.csv
Normal file
302
learning/dataset_tree_300.csv
Normal file
@ -0,0 +1,302 @@
|
|||||||
|
tower_dist;mob1_dist;mob2_dist;opp1_dist;opp2_dist;opp3_dist;opp4_dist;agent_hp;tower_hp;mob1_hp;mob2_hp;opp1_hp;opp2_hp;opp3_hp;opp4_hp;goal
|
||||||
|
24;37;41;19;37;29;20;3;38;13;3;12;9;0;10;tower
|
||||||
|
1;35;11;38;37;10;34;4;11;15;7;5;0;6;4;tower
|
||||||
|
7;10;5;41;17;27;19;8;31;14;2;2;7;4;0;mob2
|
||||||
|
15;3;12;18;9;25;40;8;44;7;5;10;0;5;9;mob1
|
||||||
|
18;19;21;12;8;13;10;2;44;5;2;9;7;1;10;opp3
|
||||||
|
19;30;11;34;17;26;13;3;35;4;2;0;12;2;12;mob2
|
||||||
|
13;17;10;7;9;30;9;2;40;1;5;10;9;12;3;opp4
|
||||||
|
14;30;21;11;18;21;7;12;28;5;3;4;0;11;0;tower
|
||||||
|
7;38;7;21;5;25;15;6;14;1;4;12;10;12;8;tower
|
||||||
|
40;24;41;39;1;9;38;2;12;6;1;1;1;9;11;opp2
|
||||||
|
41;2;24;15;17;19;3;10;54;6;6;11;1;2;0;mob1
|
||||||
|
25;42;32;25;6;19;11;12;25;10;3;8;11;4;6;tower
|
||||||
|
6;19;38;10;32;17;24;5;67;12;3;8;4;10;2;tower
|
||||||
|
24;35;32;37;10;7;9;4;75;10;7;11;3;12;10;opp2
|
||||||
|
16;25;11;23;6;35;30;11;71;9;4;8;9;12;0;mob2
|
||||||
|
37;38;5;22;23;13;25;7;6;3;0;8;12;10;1;mob2
|
||||||
|
40;42;34;28;34;8;15;1;54;4;5;10;6;9;1;opp4
|
||||||
|
6;16;2;4;18;27;5;12;73;8;7;10;12;5;11;mob2
|
||||||
|
9;1;1;18;40;34;20;6;69;12;5;10;6;1;9;mob1
|
||||||
|
13;22;4;20;25;29;7;7;27;2;1;9;7;1;3;mob2
|
||||||
|
34;15;39;5;37;23;1;3;15;2;1;1;9;10;5;opp1
|
||||||
|
38;20;4;22;43;23;22;6;60;6;3;4;4;8;0;mob1
|
||||||
|
12;31;5;14;23;15;27;1;50;7;2;6;8;6;3;mob2
|
||||||
|
14;37;2;25;23;26;8;4;53;10;0;12;9;9;11;mob2
|
||||||
|
33;8;22;35;42;21;21;8;68;6;6;6;6;7;6;mob1
|
||||||
|
17;11;7;25;6;16;8;5;52;7;7;0;11;2;2;mob2
|
||||||
|
28;12;20;3;7;32;28;12;74;0;6;0;5;12;9;opp2
|
||||||
|
21;9;10;38;1;40;41;1;61;3;5;9;7;9;2;opp2
|
||||||
|
16;1;39;36;7;36;17;1;37;2;6;10;2;7;4;opp2
|
||||||
|
22;15;31;27;3;40;26;9;71;1;0;10;9;5;9;mob1
|
||||||
|
27;26;25;9;13;7;27;2;2;9;4;1;1;3;10;opp1
|
||||||
|
28;43;18;15;21;16;18;3;51;3;4;10;0;5;0;mob2
|
||||||
|
41;8;4;4;6;11;23;12;54;14;5;6;9;8;0;mob2
|
||||||
|
15;34;12;17;18;15;19;1;75;5;4;5;1;1;7;opp3
|
||||||
|
42;36;1;3;28;24;28;2;54;13;3;6;7;11;7;mob2
|
||||||
|
25;22;23;34;8;42;14;9;40;14;5;2;1;7;12;opp2
|
||||||
|
41;14;7;43;15;26;19;4;16;3;0;9;12;1;7;mob1
|
||||||
|
18;40;1;40;38;10;5;2;54;14;4;11;2;11;9;mob2
|
||||||
|
14;35;5;3;5;43;37;3;23;2;1;10;9;11;8;mob2
|
||||||
|
40;10;23;25;33;37;26;1;42;14;6;2;11;5;6;mob1
|
||||||
|
32;9;26;2;39;27;17;1;36;14;0;6;5;9;1;opp4
|
||||||
|
17;12;15;16;29;18;5;9;75;15;0;12;10;1;11;opp3
|
||||||
|
23;19;24;31;37;16;23;1;5;8;1;3;7;1;5;tower
|
||||||
|
16;23;41;24;20;40;29;2;44;11;0;12;7;0;4;tower
|
||||||
|
21;39;26;16;33;17;29;7;53;12;6;11;2;8;9;tower
|
||||||
|
9;28;22;33;10;23;8;7;65;15;0;3;9;11;11;mob1
|
||||||
|
29;9;34;7;14;7;20;5;69;2;5;4;1;4;4;mob1
|
||||||
|
40;39;19;5;14;3;5;11;49;8;1;9;5;1;11;opp3
|
||||||
|
19;22;33;29;24;5;39;2;60;15;4;9;5;2;10;opp3
|
||||||
|
5;28;7;34;12;6;7;6;42;5;0;8;1;11;2;opp4
|
||||||
|
31;36;11;5;34;7;2;7;57;8;7;3;11;2;4;opp3
|
||||||
|
20;37;9;38;29;33;14;9;69;0;1;7;0;10;10;mob2
|
||||||
|
27;5;9;13;26;8;42;7;0;15;5;3;9;10;4;opp1
|
||||||
|
17;38;5;23;34;42;21;2;7;2;6;5;11;12;7;mob2
|
||||||
|
37;14;33;32;7;31;41;8;72;1;0;3;6;1;1;opp2
|
||||||
|
13;35;21;35;33;7;42;9;57;7;7;4;4;12;9;mob2
|
||||||
|
16;40;35;15;19;35;21;8;53;10;3;10;4;0;10;mob1
|
||||||
|
25;11;39;2;9;7;18;6;13;0;6;9;4;1;2;mob1
|
||||||
|
16;2;41;6;20;21;30;7;45;1;4;9;8;0;2;mob1
|
||||||
|
29;33;23;36;38;27;34;6;76;10;4;6;3;8;8;mob2
|
||||||
|
15;40;7;41;5;11;14;6;69;6;0;7;11;4;7;mob2
|
||||||
|
3;33;35;3;29;20;25;12;22;12;7;4;2;1;7;tower
|
||||||
|
12;14;27;30;18;6;35;10;21;1;1;9;4;3;10;tower
|
||||||
|
27;21;21;7;10;17;34;5;77;4;5;3;4;0;12;opp1
|
||||||
|
29;9;9;18;22;1;13;3;78;12;2;5;1;2;7;opp3
|
||||||
|
4;8;6;1;20;38;39;7;50;14;2;10;11;2;2;mob2
|
||||||
|
29;10;20;25;24;9;13;11;16;11;6;10;5;5;1;opp4
|
||||||
|
35;28;30;42;32;28;29;1;32;11;0;10;2;9;3;mob1
|
||||||
|
22;25;1;4;40;18;26;9;80;4;5;8;4;10;2;mob1
|
||||||
|
19;29;21;17;35;17;10;9;49;3;0;4;2;9;11;tower
|
||||||
|
19;40;5;2;10;5;15;2;14;13;0;6;1;2;0;opp3
|
||||||
|
21;33;13;32;15;15;40;6;66;3;0;4;12;8;2;mob1
|
||||||
|
14;5;32;32;25;27;1;7;24;8;1;5;5;1;3;opp4
|
||||||
|
19;15;24;31;31;40;20;3;54;8;2;8;6;2;0;mob2
|
||||||
|
38;15;1;35;14;15;27;4;33;11;2;1;6;7;11;mob2
|
||||||
|
1;24;25;23;37;25;19;6;2;12;6;10;0;10;2;tower
|
||||||
|
37;19;29;19;2;33;20;12;29;3;6;5;7;8;0;opp2
|
||||||
|
5;16;28;42;16;15;30;8;11;0;6;6;5;12;11;tower
|
||||||
|
23;2;37;42;40;3;15;7;28;11;2;3;7;0;8;mob1
|
||||||
|
11;10;31;11;20;18;9;10;76;5;7;7;5;3;8;mob1
|
||||||
|
11;4;16;7;1;39;25;4;66;12;1;2;3;3;7;opp2
|
||||||
|
28;2;31;26;43;29;30;9;23;6;7;7;11;9;1;mob1
|
||||||
|
9;40;12;13;10;41;29;8;27;2;0;5;3;3;6;tower
|
||||||
|
26;37;23;14;23;17;6;4;56;0;7;9;6;5;2;opp4
|
||||||
|
26;14;35;13;11;4;38;8;49;3;6;1;11;0;9;mob1
|
||||||
|
5;42;19;22;36;16;37;9;46;9;7;2;11;4;10;tower
|
||||||
|
20;24;34;29;24;16;2;3;23;0;1;2;10;2;12;tower
|
||||||
|
32;40;7;41;38;43;25;8;5;0;0;4;6;10;12;mob2
|
||||||
|
28;15;25;14;13;7;30;7;37;11;0;6;10;11;1;mob1
|
||||||
|
40;24;28;10;35;38;23;1;40;11;7;0;11;3;12;mob1
|
||||||
|
10;6;5;6;20;20;37;12;13;9;0;6;1;10;7;mob1
|
||||||
|
36;15;39;28;28;17;14;7;37;6;5;12;12;8;11;mob1
|
||||||
|
12;12;30;43;36;42;35;5;41;12;6;0;3;0;2;tower
|
||||||
|
37;21;17;8;18;13;33;11;59;4;2;0;0;0;9;mob2
|
||||||
|
43;6;20;21;17;29;30;1;55;2;7;5;10;6;4;mob1
|
||||||
|
28;27;28;40;12;39;20;9;71;13;5;12;6;7;10;opp2
|
||||||
|
3;17;24;31;12;34;43;6;16;11;5;10;1;6;12;tower
|
||||||
|
17;35;37;15;16;15;29;5;69;3;2;3;4;7;10;opp1
|
||||||
|
30;19;24;1;12;21;32;6;57;12;2;5;7;0;10;mob1
|
||||||
|
35;38;20;7;21;38;41;12;66;1;3;4;2;7;8;opp1
|
||||||
|
25;13;7;39;18;17;7;12;46;0;1;9;2;10;11;mob2
|
||||||
|
35;19;33;11;13;32;15;1;48;3;3;4;4;5;10;opp1
|
||||||
|
24;37;36;7;30;32;10;4;5;8;0;7;0;12;7;tower
|
||||||
|
18;10;11;35;34;9;39;12;44;6;5;1;0;11;6;mob1
|
||||||
|
1;12;33;5;27;3;18;5;75;8;5;10;11;1;10;opp3
|
||||||
|
17;34;39;25;35;31;9;5;75;12;5;11;6;6;3;opp4
|
||||||
|
35;22;3;24;12;18;32;11;76;5;7;5;10;10;8;mob2
|
||||||
|
27;14;42;4;22;29;20;12;50;1;1;9;8;3;8;mob1
|
||||||
|
31;30;34;19;35;36;13;6;26;7;5;1;9;11;12;opp1
|
||||||
|
29;9;3;27;12;11;25;7;68;13;1;10;11;1;2;opp3
|
||||||
|
40;14;35;23;2;26;29;7;69;8;6;0;3;1;10;opp2
|
||||||
|
3;33;40;24;34;39;41;3;11;5;1;4;5;0;7;tower
|
||||||
|
4;14;35;5;5;1;17;3;37;8;0;5;2;8;0;opp2
|
||||||
|
28;35;25;18;21;12;3;4;20;4;4;6;12;7;1;opp4
|
||||||
|
42;23;4;26;37;34;35;3;2;15;2;8;12;5;12;mob2
|
||||||
|
28;43;9;27;23;5;30;2;48;7;5;5;7;12;6;mob2
|
||||||
|
1;42;1;39;12;8;12;3;6;11;4;4;0;11;0;tower
|
||||||
|
17;20;15;2;26;11;25;4;62;1;2;1;6;7;5;opp1
|
||||||
|
23;5;23;22;11;31;23;11;64;4;7;10;1;11;10;opp2
|
||||||
|
18;41;29;22;12;14;29;1;17;3;0;12;9;8;9;tower
|
||||||
|
32;11;22;27;27;22;21;6;27;10;4;10;11;11;9;tower
|
||||||
|
37;10;39;34;7;4;1;4;72;8;7;7;10;5;5;opp4
|
||||||
|
29;6;26;21;37;34;18;3;65;8;4;0;9;12;9;mob1
|
||||||
|
9;25;36;39;37;9;20;3;80;11;5;3;11;8;11;tower
|
||||||
|
34;23;40;25;23;22;40;6;13;14;0;4;9;12;0;tower
|
||||||
|
5;1;40;21;11;42;30;4;56;0;2;6;10;2;0;mob1
|
||||||
|
13;20;17;5;35;17;11;5;20;8;0;0;11;2;6;mob1
|
||||||
|
35;41;7;14;37;33;25;4;15;1;3;6;1;12;12;mob2
|
||||||
|
19;35;37;29;11;20;26;12;38;3;2;11;8;10;9;tower
|
||||||
|
32;8;39;14;5;3;9;5;23;12;4;5;1;12;7;opp2
|
||||||
|
7;20;1;31;35;15;5;3;72;4;4;12;1;7;1;opp4
|
||||||
|
7;41;39;30;1;32;22;4;36;13;6;3;0;9;9;tower
|
||||||
|
17;3;40;22;38;40;23;8;43;13;2;5;3;2;4;mob1
|
||||||
|
35;34;34;42;35;34;33;8;12;11;7;12;10;11;2;tower
|
||||||
|
42;29;10;22;30;36;27;8;25;3;7;0;2;1;9;mob2
|
||||||
|
12;2;9;38;13;15;1;7;63;9;4;0;10;1;0;mob1
|
||||||
|
23;23;43;24;15;20;36;3;19;4;0;8;8;9;11;tower
|
||||||
|
30;18;16;32;20;41;8;10;58;0;0;6;7;3;1;opp4
|
||||||
|
7;25;2;31;26;34;15;4;6;11;0;2;5;11;2;tower
|
||||||
|
38;20;32;30;37;15;8;7;5;9;6;12;1;4;2;opp4
|
||||||
|
34;37;9;11;2;7;32;7;79;13;4;9;9;5;6;opp3
|
||||||
|
33;5;14;34;40;21;26;5;31;5;0;4;11;12;1;mob2
|
||||||
|
30;36;41;19;9;10;9;7;41;1;6;2;4;4;6;opp2
|
||||||
|
6;35;28;8;25;3;21;2;79;11;0;6;10;2;4;opp3
|
||||||
|
42;11;27;28;34;14;37;1;10;6;0;5;11;6;4;mob1
|
||||||
|
36;38;23;21;15;32;25;2;50;10;0;10;5;2;2;mob1
|
||||||
|
4;26;31;19;18;32;40;5;25;12;7;7;2;8;9;tower
|
||||||
|
18;29;38;42;4;13;36;9;61;12;5;3;4;7;7;opp2
|
||||||
|
5;37;22;24;27;26;32;5;65;4;2;1;6;8;7;tower
|
||||||
|
7;32;10;37;23;43;18;12;54;15;1;6;5;4;5;mob2
|
||||||
|
21;36;30;41;29;31;2;3;58;9;0;9;6;8;12;mob1
|
||||||
|
14;17;5;19;16;39;20;5;43;13;1;2;4;3;4;mob2
|
||||||
|
16;28;7;12;28;40;9;8;69;13;1;11;0;1;1;opp4
|
||||||
|
33;25;5;18;12;24;24;12;29;11;1;12;7;3;10;mob2
|
||||||
|
16;2;31;43;29;16;14;9;25;5;5;6;10;5;1;opp4
|
||||||
|
32;18;36;13;29;40;20;12;13;14;2;10;10;11;11;tower
|
||||||
|
23;22;23;3;27;24;2;8;62;1;1;3;5;5;8;opp1
|
||||||
|
15;2;20;16;10;41;18;2;29;4;3;2;11;7;6;mob1
|
||||||
|
19;13;20;8;4;29;15;12;32;10;1;9;11;9;9;mob1
|
||||||
|
13;32;41;1;33;33;11;1;28;5;1;10;5;9;6;tower
|
||||||
|
27;18;5;2;34;27;17;8;66;9;5;8;2;2;12;mob2
|
||||||
|
24;21;17;3;24;4;17;12;52;5;5;6;3;9;0;opp1
|
||||||
|
3;5;29;23;27;24;38;1;62;4;4;1;3;0;11;tower
|
||||||
|
35;3;28;33;31;6;36;11;69;1;7;4;5;5;0;mob1
|
||||||
|
2;15;17;39;6;29;39;3;43;4;2;11;6;1;3;tower
|
||||||
|
17;35;10;36;18;4;27;11;5;0;3;5;1;4;3;mob2
|
||||||
|
18;15;11;40;24;31;10;7;58;7;0;3;8;4;1;opp4
|
||||||
|
11;23;8;20;7;38;6;3;51;12;0;11;10;10;2;opp4
|
||||||
|
7;16;13;27;41;1;13;10;25;9;4;11;10;11;5;mob1
|
||||||
|
1;37;22;9;20;24;36;10;53;12;0;3;9;10;2;tower
|
||||||
|
10;27;42;42;19;26;39;7;35;0;5;9;2;2;10;tower
|
||||||
|
10;1;28;12;9;10;7;9;5;2;4;6;0;9;7;mob1
|
||||||
|
28;19;27;8;3;37;34;11;25;7;1;11;0;1;3;mob1
|
||||||
|
39;15;23;9;7;32;1;3;52;8;1;6;7;0;2;opp4
|
||||||
|
11;9;5;16;17;8;29;4;45;3;6;2;12;6;1;mob2
|
||||||
|
42;40;37;31;37;37;30;4;6;11;3;9;6;2;4;mob1
|
||||||
|
39;6;12;16;32;13;20;7;52;4;5;0;4;9;3;mob1
|
||||||
|
18;8;42;26;27;15;13;6;41;11;1;2;4;7;12;mob1
|
||||||
|
25;32;15;24;31;18;7;12;24;0;4;12;9;3;2;opp4
|
||||||
|
38;34;32;6;18;27;30;6;8;12;7;12;11;10;9;tower
|
||||||
|
39;29;10;29;12;42;10;7;15;6;2;3;8;10;5;mob2
|
||||||
|
26;9;18;24;1;23;27;2;78;8;2;3;6;0;2;opp2
|
||||||
|
16;30;13;4;10;29;8;4;78;4;1;7;3;5;10;opp2
|
||||||
|
2;36;22;20;42;1;15;10;30;11;6;2;4;12;11;tower
|
||||||
|
21;24;25;17;32;4;10;9;31;14;3;11;7;0;5;mob1
|
||||||
|
37;10;14;10;2;38;23;1;39;10;5;2;10;12;1;opp1
|
||||||
|
11;34;26;20;26;30;6;4;50;3;4;4;3;1;12;tower
|
||||||
|
14;8;30;29;17;41;3;4;77;8;6;8;4;3;9;mob1
|
||||||
|
16;25;10;14;23;15;41;11;24;1;7;8;9;11;8;mob2
|
||||||
|
36;16;16;24;25;34;17;1;51;13;5;9;11;0;5;mob2
|
||||||
|
35;17;2;18;29;38;39;12;31;5;5;3;0;2;11;mob2
|
||||||
|
11;39;41;5;5;27;17;11;30;6;0;0;8;11;11;tower
|
||||||
|
14;21;13;17;7;21;16;5;48;8;6;10;11;10;0;mob2
|
||||||
|
9;23;19;33;12;15;34;11;36;10;1;12;11;7;0;tower
|
||||||
|
32;2;43;38;28;3;27;9;67;9;3;7;8;8;7;opp3
|
||||||
|
32;2;32;21;13;6;16;10;37;3;7;9;5;12;2;opp4
|
||||||
|
5;6;43;9;31;15;15;2;75;11;7;4;12;12;11;mob1
|
||||||
|
26;20;27;28;40;32;17;2;56;6;2;8;7;3;10;mob1
|
||||||
|
24;10;25;25;25;38;35;5;47;14;0;6;0;2;11;mob1
|
||||||
|
26;39;26;33;14;39;14;2;24;12;7;3;2;7;12;opp2
|
||||||
|
3;32;20;38;40;39;25;2;66;8;3;11;10;3;2;tower
|
||||||
|
40;6;41;21;1;4;25;3;79;10;2;12;8;5;9;mob1
|
||||||
|
8;39;19;4;14;15;5;10;52;9;0;3;7;12;4;opp1
|
||||||
|
18;12;29;42;33;43;23;9;69;12;1;5;1;11;2;mob1
|
||||||
|
4;26;28;23;20;34;14;8;7;0;0;10;1;12;12;tower
|
||||||
|
25;24;29;40;25;37;33;1;64;8;0;3;0;6;5;mob1
|
||||||
|
29;12;41;37;3;42;16;11;43;7;3;10;1;0;0;opp2
|
||||||
|
19;4;8;34;34;1;7;10;62;5;4;10;1;3;1;opp4
|
||||||
|
11;24;27;43;10;9;32;12;43;10;2;1;0;11;2;tower
|
||||||
|
30;42;34;12;41;6;6;8;57;6;5;6;8;11;9;opp1
|
||||||
|
21;25;26;10;18;19;15;8;13;14;4;8;11;0;8;tower
|
||||||
|
36;24;25;6;10;30;13;1;64;9;3;5;9;4;6;opp1
|
||||||
|
18;29;20;19;30;21;3;11;36;1;7;4;12;8;0;tower
|
||||||
|
32;23;3;40;14;8;19;8;77;13;5;10;5;11;5;mob1
|
||||||
|
30;31;27;13;8;35;35;8;75;0;7;4;1;1;4;opp2
|
||||||
|
30;43;1;5;3;2;20;2;33;1;2;6;0;10;1;mob2
|
||||||
|
33;40;5;36;7;25;40;9;72;5;0;6;6;5;6;mob1
|
||||||
|
42;32;16;30;7;6;14;2;64;1;5;8;0;6;8;mob2
|
||||||
|
13;25;6;38;26;23;30;2;5;2;7;9;0;10;8;tower
|
||||||
|
5;24;10;7;15;1;37;6;69;10;6;0;7;2;11;opp3
|
||||||
|
13;38;12;18;42;23;16;8;21;12;0;6;12;1;10;tower
|
||||||
|
15;6;32;40;12;26;11;1;1;3;7;7;4;0;8;mob1
|
||||||
|
9;38;29;26;19;22;28;6;52;8;5;11;2;5;3;tower
|
||||||
|
18;19;23;43;21;28;19;8;42;14;4;11;0;5;6;tower
|
||||||
|
6;37;4;35;10;4;26;11;44;6;1;2;5;4;12;mob2
|
||||||
|
4;26;36;9;34;11;38;10;18;4;7;6;9;12;5;tower
|
||||||
|
10;36;8;16;8;42;9;11;67;6;4;1;10;9;3;opp1
|
||||||
|
15;7;22;13;19;16;28;2;20;7;5;0;3;7;8;mob1
|
||||||
|
16;22;8;35;10;12;32;5;33;0;3;6;10;4;5;mob2
|
||||||
|
27;4;3;9;29;26;22;1;1;9;1;3;0;8;6;mob2
|
||||||
|
31;5;29;5;41;17;5;4;12;12;7;8;0;12;4;opp4
|
||||||
|
29;42;10;39;5;40;43;12;3;15;4;11;2;12;9;opp2
|
||||||
|
19;22;17;14;36;11;2;9;69;8;0;12;8;8;12;mob1
|
||||||
|
11;5;3;34;37;37;20;7;37;15;7;4;4;9;12;mob1
|
||||||
|
5;12;10;4;34;26;30;3;5;3;5;0;8;11;9;tower
|
||||||
|
31;9;42;22;10;8;32;9;16;6;7;10;5;1;0;opp3
|
||||||
|
22;27;31;10;21;18;41;3;39;4;6;5;1;12;11;opp2
|
||||||
|
5;19;26;28;37;26;22;1;31;4;6;10;7;5;11;tower
|
||||||
|
3;7;2;8;3;26;24;9;12;10;4;7;6;4;7;tower
|
||||||
|
41;30;13;25;36;41;7;12;11;2;3;7;12;8;3;opp4
|
||||||
|
23;26;24;13;17;21;24;9;29;15;5;8;0;4;11;tower
|
||||||
|
36;6;7;18;6;1;15;1;12;14;6;4;1;11;9;opp2
|
||||||
|
5;23;43;2;5;6;11;10;75;15;2;0;12;11;4;opp4
|
||||||
|
17;39;8;7;41;14;16;7;45;8;1;2;2;2;8;opp1
|
||||||
|
37;26;34;5;9;20;18;12;41;13;6;0;0;6;6;tower
|
||||||
|
40;35;12;6;10;10;18;10;29;14;1;2;11;0;11;opp1
|
||||||
|
2;35;18;2;9;34;10;4;42;0;0;2;3;10;6;opp1
|
||||||
|
1;14;1;3;17;8;39;8;56;3;4;2;1;5;4;mob2
|
||||||
|
40;28;8;20;37;2;42;10;19;8;1;3;7;8;0;mob2
|
||||||
|
31;28;14;3;6;17;1;7;45;2;3;2;9;3;0;opp1
|
||||||
|
24;13;15;11;38;28;13;12;51;3;4;6;4;2;12;mob2
|
||||||
|
30;38;6;26;11;11;30;4;40;15;2;6;4;1;8;opp3
|
||||||
|
42;37;32;2;5;19;35;2;64;9;5;12;3;8;3;opp2
|
||||||
|
23;24;32;40;4;24;1;3;78;14;4;5;9;10;2;opp4
|
||||||
|
5;22;19;22;41;3;34;11;50;5;2;6;10;1;3;opp3
|
||||||
|
3;38;20;2;25;6;25;6;59;15;3;3;0;10;8;opp1
|
||||||
|
33;41;39;42;38;29;27;5;33;14;5;5;2;1;12;opp3
|
||||||
|
20;10;27;16;14;7;35;1;24;13;6;7;11;4;4;opp3
|
||||||
|
6;8;4;35;12;40;15;10;38;12;7;0;10;6;5;mob2
|
||||||
|
18;41;35;8;13;14;4;1;70;6;1;0;7;11;6;opp4
|
||||||
|
39;42;12;28;11;40;7;3;71;3;7;5;2;1;10;opp2
|
||||||
|
32;32;24;19;13;14;40;12;18;6;1;1;8;11;10;opp1
|
||||||
|
31;14;16;12;33;25;5;5;8;8;6;8;6;5;2;opp4
|
||||||
|
38;31;34;27;5;26;27;8;75;3;6;6;10;1;5;mob1
|
||||||
|
2;7;23;8;24;28;20;2;31;5;7;9;0;11;11;tower
|
||||||
|
39;43;17;21;31;13;41;8;43;10;5;4;10;6;8;mob1
|
||||||
|
3;6;17;7;22;23;22;6;40;8;6;7;1;7;11;tower
|
||||||
|
3;42;13;24;32;1;33;5;68;7;0;4;6;1;10;opp3
|
||||||
|
12;26;4;18;2;42;29;10;41;11;3;10;10;10;9;mob2
|
||||||
|
21;26;7;24;31;10;33;4;51;1;2;8;2;8;3;mob2
|
||||||
|
39;22;7;20;5;29;38;10;8;4;1;9;12;11;0;mob2
|
||||||
|
20;19;1;22;36;13;5;7;4;3;7;3;9;1;2;mob2
|
||||||
|
4;7;42;17;6;14;26;11;45;9;4;10;6;11;2;tower
|
||||||
|
23;27;11;25;38;26;34;10;77;13;7;4;3;5;9;mob1
|
||||||
|
40;10;29;8;7;32;32;12;32;9;2;10;11;2;4;mob2
|
||||||
|
37;22;34;17;17;25;40;5;74;9;3;3;8;2;9;opp1
|
||||||
|
23;10;36;43;31;26;32;5;59;1;1;2;3;8;3;mob1
|
||||||
|
14;32;8;4;17;33;8;9;74;5;1;7;12;7;3;mob2
|
||||||
|
8;14;21;26;16;43;10;5;26;1;1;5;0;1;5;tower
|
||||||
|
7;3;19;7;39;28;12;11;32;9;0;7;12;11;10;tower
|
||||||
|
11;21;13;31;30;15;43;10;73;6;1;3;11;9;12;mob2
|
||||||
|
28;10;32;28;26;36;38;4;15;11;0;12;11;12;7;mob1
|
||||||
|
39;2;25;11;11;25;35;8;36;14;0;9;10;1;7;mob1
|
||||||
|
43;41;39;38;16;5;35;12;48;14;4;7;3;11;6;opp2
|
||||||
|
36;31;28;41;40;23;23;11;27;7;2;11;0;6;11;mob1
|
||||||
|
28;8;38;39;31;3;7;7;47;0;6;8;0;2;6;opp3
|
||||||
|
5;8;19;26;26;9;1;8;66;15;3;8;11;7;7;opp4
|
||||||
|
36;28;27;7;25;1;21;10;63;0;6;4;2;10;4;opp1
|
||||||
|
19;3;6;35;5;29;3;9;63;2;7;3;12;1;3;opp4
|
||||||
|
27;5;36;31;6;10;27;12;9;8;6;10;9;6;11;mob1
|
||||||
|
21;21;27;18;26;9;39;1;43;6;3;5;10;0;7;mob1
|
||||||
|
12;39;15;31;32;9;39;7;26;4;0;12;5;0;12;tower
|
||||||
|
41;18;22;33;25;6;37;1;77;14;7;12;7;4;8;opp3
|
||||||
|
8;33;19;22;5;36;28;3;69;15;5;5;0;7;3;mob1
|
||||||
|
24;8;15;16;21;18;15;9;78;4;0;4;9;5;4;mob2
|
||||||
|
38;24;26;28;41;21;43;2;65;15;3;1;10;5;4;mob1
|
||||||
|
33;17;6;4;34;36;25;6;35;4;4;10;9;0;3;mob2
|
||||||
|
29;25;30;19;35;38;33;6;68;5;1;0;5;11;6;mob1
|
||||||
|
23;43;41;25;27;26;19;7;12;8;3;4;10;11;9;tower
|
||||||
|
7;9;18;31;36;21;16;4;23;8;4;9;8;11;5;tower
|
||||||
|
35;21;39;36;36;37;33;10;41;9;4;1;0;7;0;mob1
|
|
78
learning/decision_tree.py
Normal file
78
learning/decision_tree.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
from sklearn.preprocessing import LabelEncoder
|
||||||
|
from sklearn.tree import DecisionTreeClassifier
|
||||||
|
|
||||||
|
from common.helpers import castle_neighbors, find_neighbours
|
||||||
|
from models.castle import Castle
|
||||||
|
from models.knight import Knight
|
||||||
|
from models.monster import Monster
|
||||||
|
|
||||||
|
|
||||||
|
def manhattan_distance(p1: Tuple[int, int], p2: Tuple[int, int]) -> int:
|
||||||
|
x1, y1 = p1
|
||||||
|
x2, y2 = p2
|
||||||
|
return abs(x1 - x2) + abs(y1 - y2)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_hp(hp: int) -> int:
|
||||||
|
return max(0, hp)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_idx_of_opp_or_monster(s: str) -> int:
|
||||||
|
return int(s[-1]) - 1
|
||||||
|
|
||||||
|
|
||||||
|
class DecisionTree:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
data_frame = pd.read_csv('learning/dataset_tree_1000.csv', delimiter=';')
|
||||||
|
unlabeled_goals = data_frame['goal']
|
||||||
|
self.goals_label_encoder = LabelEncoder()
|
||||||
|
self.goals = self.goals_label_encoder.fit_transform(unlabeled_goals)
|
||||||
|
self.train_set = data_frame.drop('goal', axis='columns')
|
||||||
|
self.model = DecisionTreeClassifier(criterion='entropy')
|
||||||
|
self.model.fit(self.train_set.values, self.goals)
|
||||||
|
|
||||||
|
def predict_move(self, grid: List[List[int]], current_knight: Knight, castle: Castle, monsters: List[Monster],
|
||||||
|
opponents: List[Knight]) -> \
|
||||||
|
List[Tuple[int, int]]:
|
||||||
|
distance_to_castle = manhattan_distance(current_knight.position, castle.position)
|
||||||
|
|
||||||
|
monsters_parsed = []
|
||||||
|
for monster in monsters:
|
||||||
|
monsters_parsed.append((manhattan_distance(current_knight.position, monster.position), parse_hp(
|
||||||
|
monster.health_bar.current_hp)))
|
||||||
|
|
||||||
|
opponents_parsed = []
|
||||||
|
for opponent in opponents:
|
||||||
|
opponents_parsed.append(
|
||||||
|
(manhattan_distance(current_knight.position, opponent.position),
|
||||||
|
parse_hp(opponent.health_bar.current_hp)))
|
||||||
|
|
||||||
|
prediction = self.get_prediction(tower_dist=distance_to_castle, tower_hp=castle.health_bar.current_hp,
|
||||||
|
mob1_dist=monsters_parsed[0][0], mob1_hp=monsters_parsed[0][1],
|
||||||
|
mob2_dist=monsters_parsed[1][0], mob2_hp=monsters_parsed[1][1],
|
||||||
|
opp1_dist=opponents_parsed[0][0], opp1_hp=opponents_parsed[0][1],
|
||||||
|
opp2_dist=opponents_parsed[1][0], opp2_hp=opponents_parsed[1][1],
|
||||||
|
opp3_dist=opponents_parsed[2][0], opp3_hp=opponents_parsed[2][1],
|
||||||
|
opp4_dist=opponents_parsed[3][0], opp4_hp=opponents_parsed[3][1],
|
||||||
|
agent_hp=current_knight.health_bar.current_hp)
|
||||||
|
print(f'Prediction = {prediction}')
|
||||||
|
if prediction == 'tower': # castle...
|
||||||
|
return castle_neighbors(grid, castle_bottom_right_row=castle.position[0],
|
||||||
|
castle_bottom_right_col=castle.position[1])
|
||||||
|
elif prediction.startswith('opp'):
|
||||||
|
idx = parse_idx_of_opp_or_monster(prediction)
|
||||||
|
return find_neighbours(grid, opponents[idx].position[1], opponents[idx].position[0])
|
||||||
|
else:
|
||||||
|
idx = parse_idx_of_opp_or_monster(prediction)
|
||||||
|
return find_neighbours(grid, monsters[idx].position[1], monsters[idx].position[0])
|
||||||
|
|
||||||
|
def get_prediction(self, tower_dist: int, mob1_dist: int, mob2_dist: int, opp1_dist: int, opp2_dist: int,
|
||||||
|
opp3_dist: int, opp4_dist: int, agent_hp: int, tower_hp: int, mob1_hp: int, mob2_hp: int,
|
||||||
|
opp1_hp: int, opp2_hp: int, opp3_hp: int, opp4_hp) -> str:
|
||||||
|
prediction = self.model.predict(
|
||||||
|
[[tower_dist, mob1_dist, mob2_dist, opp1_dist, opp2_dist, opp3_dist, opp4_dist, agent_hp,
|
||||||
|
tower_hp, mob1_hp, mob2_hp, opp1_hp, opp2_hp, opp3_hp, opp4_hp]])
|
||||||
|
return self.goals_label_encoder.inverse_transform(prediction)[0]
|
@ -27,7 +27,6 @@ class Game:
|
|||||||
self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)}
|
self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main_menu(self):
|
def main_menu(self):
|
||||||
menu = MainMenu(self.screen, self.clock, self.bg,
|
menu = MainMenu(self.screen, self.clock, self.bg,
|
||||||
self.game,
|
self.game,
|
||||||
@ -38,7 +37,6 @@ class Game:
|
|||||||
def game(self):
|
def game(self):
|
||||||
logs = Logs(self.screen)
|
logs = Logs(self.screen)
|
||||||
level = Level(self.screen, logs)
|
level = Level(self.screen, logs)
|
||||||
stats = Stats(self.screen)
|
|
||||||
|
|
||||||
# setup clock for rounds
|
# setup clock for rounds
|
||||||
NEXT_TURN = pygame.USEREVENT + 1
|
NEXT_TURN = pygame.USEREVENT + 1
|
||||||
@ -46,6 +44,8 @@ class Game:
|
|||||||
|
|
||||||
# create level
|
# create level
|
||||||
level.create_map()
|
level.create_map()
|
||||||
|
stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red)
|
||||||
|
level.setup_stats(stats)
|
||||||
|
|
||||||
print_numbers_flag = False
|
print_numbers_flag = False
|
||||||
running = True
|
running = True
|
||||||
@ -61,8 +61,7 @@ class Game:
|
|||||||
running = False
|
running = False
|
||||||
if event.key == pygame.K_n:
|
if event.key == pygame.K_n:
|
||||||
print_numbers_flag = not print_numbers_flag
|
print_numbers_flag = not print_numbers_flag
|
||||||
if event.key == pygame.K_r:
|
|
||||||
stats.red_team_hp_bar.take_dmg(5)
|
|
||||||
if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds
|
if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds
|
||||||
level.handle_turn()
|
level.handle_turn()
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ class HealthBar:
|
|||||||
self.rect = rect
|
self.rect = rect
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.current_hp = current_hp
|
self.current_hp = current_hp
|
||||||
self.target_hp = current_hp
|
|
||||||
self.max_hp = max_hp
|
self.max_hp = max_hp
|
||||||
self.x = self.rect.x
|
self.x = self.rect.x
|
||||||
self.y = self.rect.y
|
self.y = self.rect.y
|
||||||
@ -38,22 +37,22 @@ class HealthBar:
|
|||||||
|
|
||||||
self.health_ratio = self.max_hp / self.width
|
self.health_ratio = self.max_hp / self.width
|
||||||
|
|
||||||
def take_dmg(self, dmg_taken):
|
def take_dmg(self, amount):
|
||||||
if self.target_hp > 0:
|
if self.current_hp - amount > 0:
|
||||||
self.target_hp -= dmg_taken
|
self.current_hp -= amount
|
||||||
elif self.target_hp < 0:
|
elif self.current_hp - amount <= 0:
|
||||||
self.target_hp = 0
|
self.current_hp = 0
|
||||||
|
|
||||||
def heal(self, amount):
|
def heal(self, amount):
|
||||||
if self.target_hp < self.max_hp:
|
if self.current_hp + amount < self.max_hp:
|
||||||
self.target_hp += amount
|
self.current_hp += amount
|
||||||
elif self.target_hp > self.max_hp:
|
elif self.current_hp + amount >= self.max_hp:
|
||||||
self.target_hp = self.max_hp
|
self.current_hp = self.max_hp
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
pygame.Surface.fill(self.screen, BLACK, (self.x-1, self.y-1, self.width+2, self.height+2))
|
pygame.Surface.fill(self.screen, BLACK, (self.x-1, self.y-1, self.width+2, self.height+2))
|
||||||
pygame.Surface.fill(self.screen, RED, (self.x, self.y, self.width, self.height))
|
pygame.Surface.fill(self.screen, RED, (self.x, self.y, self.width, self.height))
|
||||||
pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.target_hp / self.health_ratio), self.height))
|
pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.current_hp / self.health_ratio), self.height))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ class KnightsQueue:
|
|||||||
def dequeue_knight(self):
|
def dequeue_knight(self):
|
||||||
if self.both_teams_alive():
|
if self.both_teams_alive():
|
||||||
knight = self.queues[self.team_idx_turn].popleft()
|
knight = self.queues[self.team_idx_turn].popleft()
|
||||||
if knight.max_hp <= 0:
|
if knight.health_bar.current_hp <= 0:
|
||||||
return self.dequeue_knight()
|
return self.dequeue_knight()
|
||||||
else:
|
else:
|
||||||
self.queues[self.team_idx_turn].append(knight)
|
self.queues[self.team_idx_turn].append(knight)
|
||||||
|
210
logic/level.py
210
logic/level.py
@ -2,11 +2,12 @@ import random
|
|||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD, UP, DOWN, LEFT, RIGHT
|
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD
|
||||||
|
from algorithms.genetic.const import MAP_ALIASES
|
||||||
|
from algorithms.genetic.map_importer_exporter import import_random_map
|
||||||
from common.constants import *
|
from common.constants import *
|
||||||
from common.helpers import castle_neighbors
|
from learning.decision_tree import DecisionTree
|
||||||
from logic.knights_queue import KnightsQueue
|
from logic.knights_queue import KnightsQueue
|
||||||
from logic.spawner import Spawner
|
|
||||||
from models.castle import Castle
|
from models.castle import Castle
|
||||||
from models.knight import Knight
|
from models.knight import Knight
|
||||||
from models.monster import Monster
|
from models.monster import Monster
|
||||||
@ -17,10 +18,11 @@ class Level:
|
|||||||
def __init__(self, screen, logs):
|
def __init__(self, screen, logs):
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.logs = logs
|
self.logs = logs
|
||||||
|
self.decision_tree = DecisionTree()
|
||||||
# sprite group setup
|
# sprite group setup
|
||||||
self.sprites = pygame.sprite.LayeredUpdates()
|
self.sprites = pygame.sprite.LayeredUpdates()
|
||||||
|
|
||||||
self.map = [['g' for _ in range(COLUMNS)] for y in range(ROWS)]
|
self.map = []
|
||||||
|
|
||||||
self.list_knights_blue = []
|
self.list_knights_blue = []
|
||||||
self.list_knights_red = []
|
self.list_knights_red = []
|
||||||
@ -29,28 +31,21 @@ class Level:
|
|||||||
|
|
||||||
self.knights_queue = None
|
self.knights_queue = None
|
||||||
|
|
||||||
|
self.stats = None
|
||||||
|
|
||||||
|
def setup_stats(self, stats):
|
||||||
|
self.stats = stats
|
||||||
|
|
||||||
|
def add_points(self, team, points_to_add):
|
||||||
|
if self.stats is not None:
|
||||||
|
self.stats.add_points(team, points_to_add)
|
||||||
|
|
||||||
def create_map(self):
|
def create_map(self):
|
||||||
self.generate_map()
|
self.map = import_random_map()
|
||||||
self.setup_base_tiles()
|
self.setup_base_tiles()
|
||||||
self.setup_objects()
|
self.setup_objects()
|
||||||
self.knights_queue = KnightsQueue(self.list_knights_blue, self.list_knights_red)
|
self.knights_queue = KnightsQueue(self.list_knights_blue, self.list_knights_red)
|
||||||
|
|
||||||
def generate_map(self):
|
|
||||||
spawner = Spawner(self.map)
|
|
||||||
spawner.spawn_where_possible(['w' for _ in range(NBR_OF_WATER)])
|
|
||||||
spawner.spawn_where_possible(['t' for _ in range(NBR_OF_TREES)])
|
|
||||||
spawner.spawn_where_possible(['s' for _ in range(NBR_OF_SANDS)])
|
|
||||||
|
|
||||||
spawner.spawn_in_area(['k_b' for _ in range(4)], LEFT_KNIGHTS_SPAWN_FIRST_ROW, LEFT_KNIGHTS_SPAWN_FIRST_COL,
|
|
||||||
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
|
||||||
spawner.spawn_in_area(['k_r' for _ in range(4)], RIGHT_KNIGHTS_SPAWN_FIRST_ROW, RIGHT_KNIGHTS_SPAWN_FIRST_COL,
|
|
||||||
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
|
||||||
|
|
||||||
spawner.spawn_in_area(['c'], CASTLE_SPAWN_FIRST_ROW, CASTLE_SPAWN_FIRST_COL, CASTLE_SPAWN_WIDTH,
|
|
||||||
CASTLE_SPAWN_HEIGHT, 2)
|
|
||||||
|
|
||||||
spawner.spawn_where_possible(['m' for _ in range(NBR_OF_MONSTERS)])
|
|
||||||
|
|
||||||
def setup_base_tiles(self):
|
def setup_base_tiles(self):
|
||||||
textures = []
|
textures = []
|
||||||
for texture_path in TILES:
|
for texture_path in TILES:
|
||||||
@ -62,15 +57,15 @@ class Level:
|
|||||||
for col_index, col in enumerate(row):
|
for col_index, col in enumerate(row):
|
||||||
|
|
||||||
# add base tiles, e.g. water, tree, grass
|
# add base tiles, e.g. water, tree, grass
|
||||||
if col == "w":
|
if col == MAP_ALIASES.get('WATER'):
|
||||||
texture_index = 5
|
texture_index = 5
|
||||||
texture_surface = textures[texture_index][1]
|
texture_surface = textures[texture_index][1]
|
||||||
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
|
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
|
||||||
elif col == "t":
|
elif col == MAP_ALIASES.get('TREE'):
|
||||||
texture_index = 6
|
texture_index = 6
|
||||||
texture_surface = textures[texture_index][1]
|
texture_surface = textures[texture_index][1]
|
||||||
Tile((col_index, row_index), texture_surface, self.sprites, 't')
|
Tile((col_index, row_index), texture_surface, self.sprites, 't')
|
||||||
elif col == "s":
|
elif col == MAP_ALIASES.get('SAND'):
|
||||||
texture_index = 4
|
texture_index = 4
|
||||||
texture_surface = textures[texture_index][1]
|
texture_surface = textures[texture_index][1]
|
||||||
Tile((col_index, row_index), texture_surface, self.sprites)
|
Tile((col_index, row_index), texture_surface, self.sprites)
|
||||||
@ -87,42 +82,173 @@ class Level:
|
|||||||
for col_index, col in enumerate(row):
|
for col_index, col in enumerate(row):
|
||||||
|
|
||||||
# add objects, e.g. knights, monsters, castle
|
# add objects, e.g. knights, monsters, castle
|
||||||
if col == "k_b":
|
if col == MAP_ALIASES.get('KNIGHT_BLUE'):
|
||||||
knight = Knight(self.screen, (col_index, row_index), self.sprites, "blue")
|
knight = Knight(self.screen, (col_index, row_index), self.sprites, "blue")
|
||||||
self.map[row_index][col_index] = knight
|
self.map[row_index][col_index] = knight
|
||||||
self.list_knights_blue.append(knight)
|
self.list_knights_blue.append(knight)
|
||||||
elif col == "k_r":
|
elif col == MAP_ALIASES.get('KNIGHT_RED'):
|
||||||
knight = Knight(self.screen, (col_index, row_index), self.sprites, "red")
|
knight = Knight(self.screen, (col_index, row_index), self.sprites, "red")
|
||||||
self.map[row_index][col_index] = knight
|
self.map[row_index][col_index] = knight
|
||||||
self.list_knights_red.append(knight)
|
self.list_knights_red.append(knight)
|
||||||
elif col == "m":
|
elif col == MAP_ALIASES.get('MONSTER'):
|
||||||
monster = Monster(self.screen, (col_index, row_index), self.sprites)
|
monster = Monster(self.screen, (col_index, row_index), self.sprites)
|
||||||
self.map[row_index][col_index] = monster
|
self.map[row_index][col_index] = monster
|
||||||
self.list_monsters.append(monster)
|
self.list_monsters.append(monster)
|
||||||
elif col == "c":
|
elif col == MAP_ALIASES.get('CASTLE'):
|
||||||
castle_count += 1
|
castle_count += 1
|
||||||
if castle_count == 4:
|
if castle_count == 4:
|
||||||
castle = Castle(self.screen, (col_index, row_index), self.sprites)
|
castle = Castle(self.screen, (col_index, row_index), self.sprites)
|
||||||
self.map[row_index][col_index] = castle
|
self.map[row_index][col_index] = castle
|
||||||
self.list_castles.append(castle)
|
self.list_castles.append(castle)
|
||||||
|
|
||||||
|
#def attack_knight(self, knights_list, positions, current_knight):
|
||||||
|
# op_pos_1 = current_knight.position[0] - 1, current_knight.position[1]
|
||||||
|
# positions.append(op_pos_1)
|
||||||
|
# op_pos_2 = current_knight.position[0], current_knight.position[1] - 1
|
||||||
|
# positions.append(op_pos_2)
|
||||||
|
# op_pos_3 = current_knight.position[0] + 1, current_knight.position[1]
|
||||||
|
# positions.append(op_pos_3)
|
||||||
|
# op_pos_4 = current_knight.position[0], current_knight.position[1] + 1
|
||||||
|
# positions.append(op_pos_4)
|
||||||
|
# for some_knight in knights_list:
|
||||||
|
# for some_position in positions:
|
||||||
|
# if (some_knight.position == some_position and some_knight.team != current_knight.team):
|
||||||
|
# some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
# if some_knight.health_bar.current_hp == 0:
|
||||||
|
# some_knight.kill()
|
||||||
|
# positions.clear()
|
||||||
|
|
||||||
|
def attack_knight_left(self, knights_list, current_knight):
|
||||||
|
position_left = current_knight.position[0] - 1, current_knight.position[1]
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_left and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp <= 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_left:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_left:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
|
|
||||||
|
def attack_knight_right(self, knights_list, current_knight):
|
||||||
|
position_right = current_knight.position[0] + 1, current_knight.position[1]
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_right and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp == 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_right:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_right:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
|
def attack_knight_up(self, knights_list, current_knight):
|
||||||
|
position_up = current_knight.position[0], current_knight.position[1] - 1
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_up and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp == 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_up:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_up:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
|
def attack_knight_down(self, knights_list, current_knight):
|
||||||
|
position_down = current_knight.position[0], current_knight.position[1] + 1
|
||||||
|
for some_knight in knights_list:
|
||||||
|
if (some_knight.position == position_down and some_knight.team != current_knight.team):
|
||||||
|
some_knight.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if some_knight.health_bar.current_hp == 0:
|
||||||
|
some_knight.kill()
|
||||||
|
self.add_points(current_knight.team, 5)
|
||||||
|
for monster in self.list_monsters:
|
||||||
|
if monster.position == position_down:
|
||||||
|
monster.health_bar.take_dmg(current_knight.attack)
|
||||||
|
if monster.health_bar.current_hp <= 0:
|
||||||
|
monster.kill()
|
||||||
|
self.add_points(current_knight.team, monster.points)
|
||||||
|
else:
|
||||||
|
current_knight.health_bar.take_dmg(monster.attack)
|
||||||
|
if current_knight.health_bar.current_hp <= 0:
|
||||||
|
current_knight.kill()
|
||||||
|
for castle in self.list_castles:
|
||||||
|
if castle.position == position_down:
|
||||||
|
castle.health_bar.take_dmg(current_knight.attack)
|
||||||
|
|
||||||
def handle_turn(self):
|
def handle_turn(self):
|
||||||
print("next turn")
|
|
||||||
current_knight = self.knights_queue.dequeue_knight()
|
current_knight = self.knights_queue.dequeue_knight()
|
||||||
|
knights_list = self.list_knights_red + self.list_knights_blue
|
||||||
|
print("next turn " + current_knight.team)
|
||||||
knight_pos_x = current_knight.position[0]
|
knight_pos_x = current_knight.position[0]
|
||||||
knight_pos_y = current_knight.position[1]
|
knight_pos_y = current_knight.position[1]
|
||||||
|
positions = []
|
||||||
|
|
||||||
castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1])
|
goal_list = self.decision_tree.predict_move(grid=self.map, current_knight=current_knight,
|
||||||
goal_list = castle_neighbors(self.map, castle_cords[0], castle_cords[1]) # list of castle neighbors
|
monsters=self.list_monsters,
|
||||||
|
opponents=self.list_knights_blue
|
||||||
|
if current_knight.team_alias() == 'k_r' else self.list_knights_red,
|
||||||
|
castle=self.list_castles[0])
|
||||||
|
|
||||||
|
if (len(self.list_knights_blue) == 0 or len(self.list_knights_red) == 0):
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
if len(goal_list) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
|
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
|
||||||
action_list = a_star(state, self.map, goal_list)
|
action_list = a_star(state, self.map, goal_list)
|
||||||
print(action_list)
|
print(action_list)
|
||||||
|
print(goal_list)
|
||||||
|
|
||||||
if len(action_list) == 0:
|
if len(action_list) == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
next_action = action_list.pop(0)
|
next_action = action_list.pop(0)
|
||||||
|
|
||||||
|
#if current_knight.health_bar.current_hp != 0:
|
||||||
|
#self.attack_knight(knights_list, positions, current_knight)
|
||||||
|
|
||||||
|
if current_knight.direction.name == UP:
|
||||||
|
self.attack_knight_up(knights_list, current_knight)
|
||||||
|
elif current_knight.direction.name == DOWN:
|
||||||
|
self.attack_knight_down(knights_list, current_knight)
|
||||||
|
elif current_knight.direction.name == RIGHT:
|
||||||
|
self.attack_knight_right(knights_list, current_knight)
|
||||||
|
elif current_knight.direction.name == LEFT:
|
||||||
|
self.attack_knight_left(knights_list, current_knight)
|
||||||
|
|
||||||
if next_action == TURN_LEFT:
|
if next_action == TURN_LEFT:
|
||||||
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
|
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
|
||||||
current_knight.rotate_left()
|
current_knight.rotate_left()
|
||||||
@ -131,9 +257,9 @@ class Level:
|
|||||||
current_knight.rotate_right()
|
current_knight.rotate_right()
|
||||||
elif next_action == FORWARD:
|
elif next_action == FORWARD:
|
||||||
current_knight.step_forward()
|
current_knight.step_forward()
|
||||||
self.map[knight_pos_y][knight_pos_x] = 'g'
|
self.map[knight_pos_y][knight_pos_x] = MAP_ALIASES.get("GRASS")
|
||||||
|
|
||||||
# update knight on map
|
# update knight on map
|
||||||
if current_knight.direction.name == UP:
|
if current_knight.direction.name == UP:
|
||||||
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch do góry.')
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch do góry.')
|
||||||
self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias()
|
self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias()
|
||||||
@ -147,19 +273,6 @@ class Level:
|
|||||||
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.')
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.')
|
||||||
self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias()
|
self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias()
|
||||||
|
|
||||||
def update_health_bars(self):
|
|
||||||
for knight in self.list_knights_blue:
|
|
||||||
knight.health_bar.update()
|
|
||||||
|
|
||||||
for knight in self.list_knights_red:
|
|
||||||
knight.health_bar.update()
|
|
||||||
|
|
||||||
for monster in self.list_monsters:
|
|
||||||
monster.health_bar.update()
|
|
||||||
|
|
||||||
for castle in self.list_castles:
|
|
||||||
castle.health_bar.update()
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH
|
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH
|
||||||
bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH
|
bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH
|
||||||
@ -167,4 +280,7 @@ class Level:
|
|||||||
|
|
||||||
# update and draw the game
|
# update and draw the game
|
||||||
self.sprites.draw(self.screen)
|
self.sprites.draw(self.screen)
|
||||||
self.update_health_bars() # has to be called last
|
self.sprites.update()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,5 +16,7 @@ class Castle(pygame.sprite.Sprite):
|
|||||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||||
self.rect = self.image.get_rect(center=position_in_px)
|
self.rect = self.image.get_rect(center=position_in_px)
|
||||||
self.max_hp = 80
|
self.max_hp = 80
|
||||||
self.current_hp = random.randint(1, self.max_hp)
|
self.health_bar = HealthBar(screen, self.rect, current_hp=self.max_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
||||||
self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
|
||||||
|
def update(self):
|
||||||
|
self.health_bar.update()
|
||||||
|
@ -7,8 +7,11 @@ from common.helpers import parse_cord
|
|||||||
from logic.health_bar import HealthBar
|
from logic.health_bar import HealthBar
|
||||||
|
|
||||||
|
|
||||||
def load_knight_textures():
|
def load_knight_textures(team):
|
||||||
random_index = random.randint(1, 4)
|
if team == "blue":
|
||||||
|
random_index = 3
|
||||||
|
else:
|
||||||
|
random_index = 4
|
||||||
states = [
|
states = [
|
||||||
pygame.image.load(f'resources/textures/knight_{random_index}_up.png').convert_alpha(), # up = 0
|
pygame.image.load(f'resources/textures/knight_{random_index}_up.png').convert_alpha(), # up = 0
|
||||||
pygame.image.load(f'resources/textures/knight_{random_index}_right.png').convert_alpha(), # right = 1
|
pygame.image.load(f'resources/textures/knight_{random_index}_right.png').convert_alpha(), # right = 1
|
||||||
@ -24,7 +27,7 @@ class Knight(pygame.sprite.Sprite):
|
|||||||
super().__init__(group)
|
super().__init__(group)
|
||||||
|
|
||||||
self.direction = Direction.DOWN
|
self.direction = Direction.DOWN
|
||||||
self.states = load_knight_textures()
|
self.states = load_knight_textures(team)
|
||||||
|
|
||||||
self.image = self.states[self.direction.value]
|
self.image = self.states[self.direction.value]
|
||||||
self.position = position
|
self.position = position
|
||||||
@ -33,21 +36,35 @@ class Knight(pygame.sprite.Sprite):
|
|||||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||||
|
|
||||||
self.team = team
|
self.team = team
|
||||||
self.max_hp = random.randint(7, 12)
|
self.max_hp = random.randint(9, 13)
|
||||||
self.current_hp = random.randint(1, self.max_hp)
|
self.attack = random.randint(2, 4)
|
||||||
self.attack = random.randint(4, 7)
|
|
||||||
self.defense = random.randint(1, 4)
|
self.defense = random.randint(1, 4)
|
||||||
self.points = 1
|
self.points = 1
|
||||||
self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
self.health_bar = HealthBar(screen, self.rect, current_hp=self.max_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
||||||
|
|
||||||
def rotate_left(self):
|
def rotate_left(self):
|
||||||
self.direction = self.direction.left()
|
self.direction = self.direction.left()
|
||||||
self.image = self.states[self.direction.value]
|
self.image = self.states[self.direction.value]
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.health_bar.update()
|
||||||
|
|
||||||
def rotate_right(self):
|
def rotate_right(self):
|
||||||
self.direction = self.direction.right()
|
self.direction = self.direction.right()
|
||||||
self.image = self.states[self.direction.value]
|
self.image = self.states[self.direction.value]
|
||||||
|
|
||||||
|
def take_dmg(self, amount):
|
||||||
|
self.health_bar.take_dmg(amount)
|
||||||
|
|
||||||
|
def heal(self, amount):
|
||||||
|
self.health_bar.heal(amount)
|
||||||
|
|
||||||
|
def get_current_hp(self):
|
||||||
|
return self.health_bar.current_hp
|
||||||
|
|
||||||
|
def get_max_hp(self):
|
||||||
|
return self.health_bar.max_hp
|
||||||
|
|
||||||
def step_forward(self):
|
def step_forward(self):
|
||||||
if self.direction.name == 'UP':
|
if self.direction.name == 'UP':
|
||||||
self.position = (self.position[0], self.position[1] - 1)
|
self.position = (self.position[0], self.position[1] - 1)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import pygame.image
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
import pygame.image
|
||||||
|
|
||||||
from common.helpers import parse_cord
|
from common.helpers import parse_cord
|
||||||
from logic.health_bar import HealthBar
|
from logic.health_bar import HealthBar
|
||||||
|
|
||||||
@ -20,14 +21,14 @@ class Monster(pygame.sprite.Sprite):
|
|||||||
self.image = pygame.transform.scale(self.image, (40, 40))
|
self.image = pygame.transform.scale(self.image, (40, 40))
|
||||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||||
|
self.position = position
|
||||||
self.max_hp = random.randrange(15, 25)
|
self.max_hp = random.randrange(15, 20)
|
||||||
self.current_hp = random.randint(1, self.max_hp)
|
self.health_bar = HealthBar(screen, self.rect, current_hp=self.max_hp, max_hp=self.max_hp,
|
||||||
self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
|
calculate_xy=True, calculate_size=True)
|
||||||
self.attack = random.randrange(2, 10)
|
self.attack = random.randrange(4, 6)
|
||||||
if self.image == monster_images[0]:
|
if self.image == monster_images[0]:
|
||||||
self.max_hp = 20
|
self.max_hp = 20
|
||||||
self.attack = 9
|
self.attack = 6
|
||||||
self.points = 10
|
self.points = 10
|
||||||
elif self.image == monster_images[1]:
|
elif self.image == monster_images[1]:
|
||||||
self.max_hp = 15
|
self.max_hp = 15
|
||||||
@ -41,3 +42,6 @@ class Monster(pygame.sprite.Sprite):
|
|||||||
self.max_hp = 7
|
self.max_hp = 7
|
||||||
self.attack = 2
|
self.attack = 2
|
||||||
self.points = 2
|
self.points = 2
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.health_bar.update()
|
||||||
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
1
resources/maps/map_2022_06_02_13_27_18.json
Normal file
1
resources/maps/map_2022_06_02_13_27_18.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"map": [[0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 3, 3, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 3], [0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3], [0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 3], [0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2], [0, 0, 3, 3, 3, 0, 0, 0, 3, 3, 3, 3, 0, 2, 2, 2, 0, 0, 0, 0, 0, 7, 2, 0], [0, 0, 0, 6, 0, 0, 0, 2, 2, 2, 0, 5, 5, 0, 2, 0, 0, 2, 2, 2, 2, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 5, 5, 0, 2, 0, 0, 0, 0, 2, 2, 1, 1, 7], [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 3, 1, 7], [6, 0, 0, 6, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3, 1, 1, 0], [6, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 3, 3, 0, 0, 0, 0, 3, 3, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 3, 0, 1, 1, 7, 0], [0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, 1, 0], [0, 3, 3, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 1, 0, 0], [0, 3, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}
|
1
resources/maps/map_2022_06_06_14_55_49.json
Normal file
1
resources/maps/map_2022_06_06_14_55_49.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"map": [[0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0], [0, 3, 3, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 3, 3, 3, 0, 0, 0, 2, 0, 0, 0, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0], [0, 0, 3, 2, 2, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 2, 3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 3, 0, 0, 0, 0, 0, 3, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 6, 3, 3, 0, 0, 0, 0, 0, 3, 0, 4, 0, 3, 0, 0, 0, 0, 0, 0, 7, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 7, 7, 0, 0], [6, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 3, 7, 0, 0, 0], [0, 0, 6, 6, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0], [0, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0], [2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 0, 0, 0, 0, 4, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}
|
1
resources/maps/map_2022_06_06_14_58_11.json
Normal file
1
resources/maps/map_2022_06_06_14_58_11.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"map": [[0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 3], [0, 0, 0, 0, 2, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 0], [0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 2, 0], [0, 0, 3, 3, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0], [0, 0, 3, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 0], [0, 0, 3, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0], [0, 0, 0, 6, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0], [0, 6, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5, 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 5, 5, 1, 2, 0, 0, 0, 0, 0, 0, 7, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0, 4, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0], [0, 0, 3, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 4], [6, 0, 0, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 0, 7, 0, 7, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 3, 2, 0, 0, 0, 0, 0, 0], [0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0], [0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}
|
@ -1,6 +1,31 @@
|
|||||||
from ui.screens.screen import Screen
|
import pygame
|
||||||
|
|
||||||
|
from ui.screens.screen import Screen
|
||||||
|
from common.colors import BLACK
|
||||||
|
from common.helpers import draw_text
|
||||||
|
|
||||||
class Credits(Screen):
|
class Credits(Screen):
|
||||||
|
|
||||||
def __init__(self, screen, clock):
|
def __init__(self, screen, clock):
|
||||||
super().__init__('credits', screen, clock)
|
super().__init__('credits', screen, clock)
|
||||||
|
|
||||||
|
def display_screen(self):
|
||||||
|
running = True
|
||||||
|
while running:
|
||||||
|
self.screen.fill((252, 164, 12))
|
||||||
|
draw_text('Twórcy :', BLACK, self.screen, 520, 150)
|
||||||
|
draw_text('Angelika Iskra', BLACK, self.screen, 520, 250)
|
||||||
|
draw_text('Dawid Korzępa', BLACK, self.screen, 520, 300)
|
||||||
|
draw_text('Juliusz Sadowski', BLACK, self.screen, 520, 350)
|
||||||
|
draw_text('Aleksandra Muczyńska', BLACK, self.screen, 520, 400)
|
||||||
|
draw_text('Jerzy Tomaszewski', BLACK, self.screen, 520, 450)
|
||||||
|
draw_text('Mateusz Konofał', BLACK, self.screen, 520, 500)
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
running = False
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_ESCAPE:
|
||||||
|
running = False
|
||||||
|
pygame.display.update()
|
||||||
|
self.clock.tick(60)
|
||||||
|
|
||||||
|
40
ui/stats.py
40
ui/stats.py
@ -8,13 +8,23 @@ from common.helpers import draw_text
|
|||||||
|
|
||||||
|
|
||||||
class Stats:
|
class Stats:
|
||||||
def __init__(self, screen):
|
def __init__(self, screen, list_knights_blue, list_knights_red):
|
||||||
self.grid = []
|
self.grid = []
|
||||||
|
self.list_knights_blue = list_knights_blue
|
||||||
|
self.list_knights_red = list_knights_red
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15
|
self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15
|
||||||
self.y = 5
|
self.y = 5
|
||||||
self.blue_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 30, self.y + 210, 100, 15), current_hp=50, max_hp=100)
|
self.blue_team_hp_bar = HealthBar(self.screen,
|
||||||
self.red_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 210, self.y + 210, 100, 15), 100, 100)
|
pygame.Rect(self.x + 30, self.y + 210, 100, 15),
|
||||||
|
current_hp=sum([knight.get_current_hp() for knight in self.list_knights_blue]),
|
||||||
|
max_hp=sum([knight.get_max_hp() for knight in self.list_knights_blue]))
|
||||||
|
self.red_team_hp_bar = HealthBar(self.screen,
|
||||||
|
pygame.Rect(self.x + 210, self.y + 210, 100, 15),
|
||||||
|
current_hp=sum([knight.get_current_hp() for knight in self.list_knights_red]),
|
||||||
|
max_hp=sum([knight.get_max_hp() for knight in self.list_knights_red]))
|
||||||
|
self.blue_team_points = 0
|
||||||
|
self.red_team_points = 0
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
|
||||||
@ -33,21 +43,25 @@ class Stats:
|
|||||||
draw_text('VS', FONT_DARK, self.screen, self.x + 150, self.y + 120, 36)
|
draw_text('VS', FONT_DARK, self.screen, self.x + 150, self.y + 120, 36)
|
||||||
|
|
||||||
# HP bars
|
# HP bars
|
||||||
#pygame.draw.rect(screen, RED, pygame.Rect(x + 30, y + 210, 100, 15), 0, 4)
|
self.red_team_hp_bar.take_dmg(self.red_team_hp_bar.current_hp -
|
||||||
#pygame.draw.rect(screen, RED, pygame.Rect(x + 210, y + 210, 100, 15), 0, 4)
|
sum([knight.get_current_hp() for knight in self.list_knights_red]))
|
||||||
|
self.blue_team_hp_bar.take_dmg(self.blue_team_hp_bar.current_hp -
|
||||||
|
sum([knight.get_current_hp() for knight in self.list_knights_blue]))
|
||||||
self.red_team_hp_bar.update()
|
self.red_team_hp_bar.update()
|
||||||
self.blue_team_hp_bar.update()
|
self.blue_team_hp_bar.update()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# texts
|
# texts
|
||||||
draw_text('Rycerze: 2', FONT_DARK, self.screen, self.x + 35, self.y + 240, 18)
|
draw_text('Rycerze: ' + str(len(self.list_knights_blue)), FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) # blue
|
||||||
draw_text('Fortece: 1', FONT_DARK, self.screen, self.x + 35, self.y + 270, 18)
|
|
||||||
|
|
||||||
draw_text('Rycerze: 4', FONT_DARK, self.screen, self.x + 215, self.y + 240, 18)
|
draw_text('Rycerze: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 215, self.y + 240, 18)
|
||||||
draw_text('Fortece: 0', FONT_DARK, self.screen, self.x + 215, self.y + 270, 18)
|
|
||||||
|
|
||||||
# points
|
# points
|
||||||
pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3))
|
pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3))
|
||||||
draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 35, self.y + 408, 18, True)
|
draw_text('PUNKTY: ' + str(self.blue_team_points), FONT_DARK, self.screen, self.x + 35, self.y + 408, 18, True)
|
||||||
draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 215, self.y + 408, 18, True)
|
draw_text('PUNKTY: ' + str(self.red_team_points), FONT_DARK, self.screen, self.x + 215, self.y + 408, 18, True)
|
||||||
|
|
||||||
|
def add_points(self, team, points):
|
||||||
|
if team == "blue":
|
||||||
|
self.blue_team_points += points
|
||||||
|
else:
|
||||||
|
self.red_team_points += points
|
||||||
|
Loading…
Reference in New Issue
Block a user