changes in tilesFactory and added KABOOM img
This commit is contained in:
parent
7a4285c053
commit
363ea4b786
BIN
images/sapper_idle/kaboom.png
Normal file
BIN
images/sapper_idle/kaboom.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 295 KiB |
28
src/const.py
28
src/const.py
@ -34,7 +34,9 @@ for name, val in {
|
|||||||
main_path + '/images/Tiles/' + name + ".png"
|
main_path + '/images/Tiles/' + name + ".png"
|
||||||
),
|
),
|
||||||
None if 'mine' not in name else (
|
None if 'mine' not in name else (
|
||||||
'AP' if 'AP' in name else 'AT'
|
'AP' if 'AP' in name else (
|
||||||
|
'AT' if 'AT' in name else 'ADM'
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -53,15 +55,15 @@ for name in [
|
|||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
DEBUG_FIELD = [
|
# DEBUG_FIELD = [
|
||||||
[Tile(1, 1), Tile(11, 20), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1)],
|
# [Tile(1, 1), Tile(11, 20), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1)],
|
||||||
[Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(4, 1, APMine()), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1)],
|
# [Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(4, 1, APMine()), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1)],
|
||||||
[Tile(11, 20), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(4, 0, APMine()), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1)],
|
# [Tile(11, 20), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(4, 0, APMine()), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1)],
|
||||||
[Tile(4, 0, APMine()), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(1, 1),Tile(1, 1), Tile(4, 0, APMine()), Tile(1, 1),Tile(1, 1)],
|
# [Tile(4, 0, APMine()), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(1, 1),Tile(1, 1), Tile(4, 0, APMine()), Tile(1, 1),Tile(1, 1)],
|
||||||
[Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(1, 1), Tile(1, 1)],
|
# [Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(1, 1), Tile(1, 1)],
|
||||||
[Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(4, 0, APMine()), Tile(1, 1)],
|
# [Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(4, 0, APMine()), Tile(1, 1)],
|
||||||
[Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(1, 1), Tile(1, 1)],
|
# [Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(1, 1), Tile(11, 20), Tile(1, 1), Tile(1, 1)],
|
||||||
[Tile(1, 1) for _ in range(10)],
|
# [Tile(1, 1) for _ in range(10)],
|
||||||
[Tile(1, 1) for _ in range(10)],
|
# [Tile(1, 1) for _ in range(10)],
|
||||||
[Tile(1, 1) for _ in range(10)],
|
# [Tile(1, 1) for _ in range(10)],
|
||||||
]
|
# ]
|
||||||
|
55
src/main.py
55
src/main.py
@ -4,7 +4,7 @@ from agent import Agent
|
|||||||
from game_ui import GameUi
|
from game_ui import GameUi
|
||||||
from environment import Environment
|
from environment import Environment
|
||||||
from tilesFactory import TilesFactory
|
from tilesFactory import TilesFactory
|
||||||
from const import ICON, IMAGES, DEBUG_FIELD
|
from const import ICON, IMAGES
|
||||||
from src.search_algoritms.a_star import a_star
|
from src.search_algoritms.a_star import a_star
|
||||||
from search_algoritms.BFS import breadth_first_search_for_a_star
|
from search_algoritms.BFS import breadth_first_search_for_a_star
|
||||||
from machine_learning.decision_tree import get_decision, tree_root
|
from machine_learning.decision_tree import get_decision, tree_root
|
||||||
@ -42,7 +42,7 @@ def main():
|
|||||||
pg.display.set_icon(pg.image.load(ICON))
|
pg.display.set_icon(pg.image.load(ICON))
|
||||||
pg.fastevent.init()
|
pg.fastevent.init()
|
||||||
|
|
||||||
env = Environment(DEBUG_FIELD)
|
env = Environment()
|
||||||
agent = Agent()
|
agent = Agent()
|
||||||
game_ui = GameUi(agent, env)
|
game_ui = GameUi(agent, env)
|
||||||
game_ui.update()
|
game_ui.update()
|
||||||
@ -63,31 +63,32 @@ def main():
|
|||||||
elif event.type == pg.KEYDOWN:
|
elif event.type == pg.KEYDOWN:
|
||||||
if event.key == pg.K_t:
|
if event.key == pg.K_t:
|
||||||
print('Starting to clear the sector')
|
print('Starting to clear the sector')
|
||||||
while env.mine_count:
|
#while env.mine_count:
|
||||||
print('-' * 20)
|
print('-' * 20)
|
||||||
# path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
|
# path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
|
||||||
goal = breadth_first_search_for_a_star(env.field, agent.x, agent.y, agent.direction)
|
goal = breadth_first_search_for_a_star(env.field, agent.x, agent.y, agent.direction)
|
||||||
path, actions = a_star(env.field, agent.x, agent.y, agent.direction, goal)
|
path, actions = a_star(env.field, agent.x, agent.y, agent.direction, goal)
|
||||||
if not path and not env.field[agent.y][agent.x].mine:
|
if not path and not env.field[agent.y][agent.x].mine:
|
||||||
print('Unable to find path, rocks are in the way')
|
print('Unable to find path, rocks are in the way')
|
||||||
break
|
break
|
||||||
print(f'Path{path}')
|
print(f'Path{path}')
|
||||||
print(f'Actions:{actions}')
|
print(f'Actions:{actions}')
|
||||||
for action in actions:
|
for action in actions:
|
||||||
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
|
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
|
||||||
goal_tile = env.field[goal[1]][goal[0]]
|
goal_tile = env.field[goal[1]][goal[0]]
|
||||||
data = {
|
data = {
|
||||||
'visibility': goal_tile.visibility,
|
'visibility': goal_tile.visibility,
|
||||||
'stability': goal_tile.stability,
|
'stability': goal_tile.stability,
|
||||||
'armed': goal_tile.mine.armed,
|
'armed': goal_tile.mine.armed,
|
||||||
'ground': goal_tile.ground,
|
'ground': goal_tile.ground,
|
||||||
'mine_type': goal_tile.mine.mine_type,
|
'mine_type': goal_tile.mine.mine_type,
|
||||||
'pressure_gt_two': True if goal_tile.mine.pressure > 2 else False
|
'pressure_gt_two': True if goal_tile.mine.pressure > 2 else False
|
||||||
}
|
}
|
||||||
decision = get_decision(data, tree_root)
|
decision = get_decision(data, tree_root)
|
||||||
print(f'Decision: {decision}')
|
print(data)
|
||||||
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
|
print(f'Decision: {decision}')
|
||||||
handle_keys(env, agent, game_ui, factory)
|
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
|
||||||
|
handle_keys(env, agent, game_ui, factory)
|
||||||
print('Sector clear')
|
print('Sector clear')
|
||||||
else:
|
else:
|
||||||
pg.fastevent.post(event)
|
pg.fastevent.post(event)
|
||||||
|
@ -3,5 +3,5 @@ from typing import Union
|
|||||||
|
|
||||||
class Mine:
|
class Mine:
|
||||||
def __init__(self, pressure: Union[float, int], armed: bool):
|
def __init__(self, pressure: Union[float, int], armed: bool):
|
||||||
self.is_armed = armed
|
self.armed = armed
|
||||||
self.pressure = pressure
|
self.pressure = pressure
|
@ -1,18 +1,23 @@
|
|||||||
|
from random import uniform,choice
|
||||||
from typing import List
|
from typing import List
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
from tile import Tile
|
from tile import Tile
|
||||||
from const import IMAGES
|
from const import IMAGES
|
||||||
|
from machine_learning.data_set import visibility,stability,ground,armed
|
||||||
|
|
||||||
class TilesFactory:
|
class TilesFactory:
|
||||||
def create_tile(self, number: int) -> Tile:
|
def create_tile(self, number: int) -> Tile:
|
||||||
img = IMAGES[number]
|
img = IMAGES[number]
|
||||||
if img.mine_type:
|
if img.mine_type:
|
||||||
module = import_module(f'{img.mine_type.lower()}_mine')
|
module = import_module(f'{img.mine_type.lower()}_mine')
|
||||||
return Tile(number, 0, getattr(module, f'{img.mine_type}Mine')())
|
return Tile(number, 0, self.get_random_value(visibility), self.get_random_value(stability), self.get_random_value(ground), getattr(module, f'{img.mine_type}Mine')(uniform(1, 5
|
||||||
|
if img.mine_type == 'AP' or img.mine_type == 'AT' else 50), self.get_random_value(armed)))
|
||||||
else:
|
else:
|
||||||
return Tile(number, 5) if 'grass' in img.name else Tile(number, 30)
|
return Tile(number, 5, self.get_random_value(visibility), self.get_random_value(stability), self.get_random_value(ground)) if 'grass' in img.name else Tile(number, 30, self.get_random_value(visibility), self.get_random_value(stability), self.get_random_value(ground))
|
||||||
|
|
||||||
def get_tiles_list(self) -> List[Tile]:
|
def get_tiles_list(self) -> List[Tile]:
|
||||||
return [self.create_tile(i) for i in range(13)]
|
return [self.create_tile(i) for i in range(13)]
|
||||||
|
|
||||||
|
def get_random_value(self, values):
|
||||||
|
return choice(values)
|
||||||
|
Loading…
Reference in New Issue
Block a user