From 3f4b73373a8fb28ca6ef3a22323e3af079a53150 Mon Sep 17 00:00:00 2001 From: eugenep Date: Wed, 23 Jun 2021 12:42:00 +0200 Subject: [PATCH] reset mode, minefield mode Co-authored-by: Marcin Matoga --- Looper_maps/sprite_map | 7 ++++ astar2.py | 55 +++++++++++++++++++++++-------- main.py | 73 ++++++++++++++++++++++++++++++++++++++++-- sprites.py | 12 +++++++ 4 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 Looper_maps/sprite_map diff --git a/Looper_maps/sprite_map b/Looper_maps/sprite_map new file mode 100644 index 0000000..7c44afb --- /dev/null +++ b/Looper_maps/sprite_map @@ -0,0 +1,7 @@ +>.....2 +ppp.... +p2pp... +..#2... +...#... +....ppp +2p.p... \ No newline at end of file diff --git a/astar2.py b/astar2.py index 2bab99d..dea28e4 100644 --- a/astar2.py +++ b/astar2.py @@ -224,9 +224,11 @@ class SweeperAgent: self.allowed_points = set() self.puddle_points = set() + - def where_am_i(self): - temp_map = [list(item) for item in SweeperAgent.loadMap("map.txt")] + + def where_am_i(self, map_name = "map.txt"): + temp_map = [list(item) for item in SweeperAgent.loadMap(map_name)] for row in range(MAP_SIZE): for column, pos in enumerate(temp_map[row]): @@ -238,8 +240,8 @@ class SweeperAgent: # add orientation - def where_to_go(self): - temp_map = [list(item) for item in SweeperAgent.loadMap("goal_map.txt")] + def where_to_go(self, map_name = "goal_map.txt"): + temp_map = [list(item) for item in SweeperAgent.loadMap(map_name)] for row in range(MAP_SIZE): for column, pos in enumerate(temp_map[row]): @@ -250,8 +252,8 @@ class SweeperAgent: return row, column @staticmethod - def set_allowed(allowed_points): - temp_map = [list(item) for item in SweeperAgent.loadMap('map.txt')] + def set_allowed(allowed_points, map_name = 'map.txt'): + temp_map = [list(item) for item in SweeperAgent.loadMap(map_name)] a_row = 0 a_column = 0 @@ -281,8 +283,8 @@ class SweeperAgent: @staticmethod - def set_puddles(puddle_points): - temp_map = [list(item) for item in SweeperAgent.loadMap('map.txt')] + def set_puddles(puddle_points, map_name = 'map.txt'): + temp_map = [list(item) for item in SweeperAgent.loadMap(map_name)] a_row = 0 a_column = 0 @@ -297,8 +299,8 @@ class SweeperAgent: @staticmethod - def get_goal(): - temp_map = [list(item) for item in SweeperAgent.loadMap('goal_map.txt')] + def get_goal(map_name = 'goal_map.txt'): + temp_map = [list(item) for item in SweeperAgent.loadMap(map_name)] a_row = 0 a_column = 0 @@ -310,6 +312,10 @@ class SweeperAgent: a_column = column return a_row, a_column + + + + @staticmethod def set_initial(initial): @@ -330,8 +336,8 @@ class SweeperAgent: @staticmethod - def set_orientation(): - temp_map = [list(item) for item in SweeperAgent.loadMap('map.txt')] + def set_orientation(map_name = 'map.txt'): + temp_map = [list(item) for item in SweeperAgent.loadMap(map_name)] orientation = "" @@ -349,8 +355,8 @@ class SweeperAgent: return orientation @staticmethod - def set_goal_orientation(): - temp_map = [list(item) for item in SweeperAgent.loadMap('goal_map.txt')] + def set_goal_orientation(map_name = 'goal_map.txt'): + temp_map = [list(item) for item in SweeperAgent.loadMap(map_name)] orientation = "" @@ -367,6 +373,10 @@ class SweeperAgent: return orientation + + + + @staticmethod def run_manual(self, given_orientation, @@ -395,6 +405,23 @@ class SweeperAgent: return len(self.plan_route(agent_position, goal_position, self.allowed_points, self.puddle_points)) + @staticmethod + def run_manual_map(self,start_map, goal_map): + self.orientation = SweeperAgent.set_orientation(start_map) + goal_orientation = SweeperAgent.set_goal_orientation(goal_map) + + SweeperAgent.set_allowed(self.allowed_points, start_map) + SweeperAgent.set_puddles(self.puddle_points, start_map) + + x, y = self.where_am_i(start_map) + x1, y1 = SweeperAgent.get_goal(goal_map) + + agent_position = AgentPosition(x, y, self.orientation) + goal_position = AgentPosition(x1, y1, goal_orientation) + + return self.plan_route(agent_position, goal_position, self.allowed_points, self.puddle_points) + + @staticmethod def run(self): diff --git a/main.py b/main.py index 7b33b1e..dd6f614 100644 --- a/main.py +++ b/main.py @@ -32,14 +32,17 @@ class Game: pg.key.set_repeat(500, 100) self.load_data() self.wentyl_bezpieczenstwa = 0 - #ExampleGenerator.generate(ExampleGenerator) + self.minefield = False + self.minefield_start_map = "Looper_maps/start_map" + # ExampleGenerator.generate(ExampleGenerator) - def load_data(self): + def load_data(self, map_name = "main_map.txt"): game_folder = path.dirname(__file__) self.map_data = [] - with open(path.join(game_folder, 'main_map.txt'), 'rt') as f: + with open(path.join(game_folder, map_name), 'rt') as f: for line in f: self.map_data.append(line) + def new(self): # initialize all variables and do all the setup for a new game @@ -168,8 +171,54 @@ class Game: self.player.ai_mode = "tree" self.player.check_bomb() self.wentyl_bezpieczenstwa = 1 + if event.key == pg.K_F11 and self.wentyl_bezpieczenstwa == 0: + self.player.check_bomb_neural() + self.wentyl_bezpieczenstwa = 1 + if event.key == pg.K_F12 and self.wentyl_bezpieczenstwa == 0: + g.load_data('Looper_maps/sprite_map') + g.new() + g.show_go_screen() + self.minefield = True + self.wentyl_bezpieczenstwa = 1 + if event.key == pg.K_F1 and self.wentyl_bezpieczenstwa == 0: + self.player.reset() + self.wentyl_bezpieczenstwa = 1 + if self.minefield == True: + if event.key == pg.K_1 and self.wentyl_bezpieczenstwa == 0: + agent = SweeperAgent() + player_moves = SweeperAgent.run_manual_map(agent, self.minefield_start_map, "Looper_maps/mine1_map") + self.minefield_start_map = "Looper_maps/mine1_map" + self.graph_move(player_moves) + self.wentyl_bezpieczenstwa = 1 + if event.key == pg.K_2 and self.wentyl_bezpieczenstwa == 0: + agent = SweeperAgent() + player_moves = SweeperAgent.run_manual_map(agent, self.minefield_start_map, "Looper_maps/mine2_map") + self.minefield_start_map = "Looper_maps/mine2_map" + self.graph_move(player_moves) + self.wentyl_bezpieczenstwa = 1 + if event.key == pg.K_3 and self.wentyl_bezpieczenstwa == 0: + agent = SweeperAgent() + player_moves = SweeperAgent.run_manual_map(agent, self.minefield_start_map, "Looper_maps/mine3_map") + self.minefield_start_map = "Looper_maps/mine3_map" + self.graph_move(player_moves) + self.wentyl_bezpieczenstwa = 1 + if event.key == pg.K_4 and self.wentyl_bezpieczenstwa == 0: + agent = SweeperAgent() + player_moves = SweeperAgent.run_manual_map(agent, self.minefield_start_map, "Looper_maps/mine4_map") + self.minefield_start_map = "Looper_maps/mine4_map" + self.graph_move(player_moves) + self.wentyl_bezpieczenstwa = 1 + if event.key == pg.K_F1 and self.wentyl_bezpieczenstwa == 0: + self.minefield_start_map = "Looper_maps/start_map" + self.wentyl_bezpieczenstwa = 1 + + + + if event.type == pg.KEYUP: + if event.key == pg.K_F1: + self.wentyl_bezpieczenstwa = 0 if event.key == pg.K_F2: self.wentyl_bezpieczenstwa = 0 if event.key == pg.K_F3: @@ -188,6 +237,24 @@ class Game: self.wentyl_bezpieczenstwa = 0 if event.key == pg.K_F10: self.wentyl_bezpieczenstwa = 0 + if event.key == pg.K_F11: + self.wentyl_bezpieczenstwa = 0 + if event.key == pg.K_F12: + self.wentyl_bezpieczenstwa = 0 + if self.minefield == True: + if event.key == pg.K_1: + self.wentyl_bezpieczenstwa = 0 + if event.key == pg.K_2: + self.wentyl_bezpieczenstwa == 0 + if event.key == pg.K_3: + self.wentyl_bezpieczenstwa == 0 + if event.key == pg.K_4: + self.wentyl_bezpieczenstwa == 0 + + + def minefield_looping(self): + pass + def graph_move(self, moves): diff --git a/sprites.py b/sprites.py index e5be0e9..e4f59ac 100644 --- a/sprites.py +++ b/sprites.py @@ -3,6 +3,7 @@ import enum import ctypes import ast import time +import copy from settings import * from maze import * from learning import * @@ -23,12 +24,21 @@ class Player(pg.sprite.Sprite): self.rect = self.image.get_rect() self.x = x self.y = y + self.resetX = copy.deepcopy(x) + self.resetY = copy.deepcopy(y) self.direction = direction + self.resetDirection = copy.deepcopy(direction) self.maze = Maze() self.moves = '' self.my_learning = Learning() self.ai_mode = "none" #self.decision_tree_learning() + + def reset(self): + self.x = copy.deepcopy(self.resetX) + self.y = copy.deepcopy(self.resetY) + self.direction = copy.deepcopy(self.resetDirection) + def set_direction(self, direction): self.direction = direction @@ -100,6 +110,8 @@ class Player(pg.sprite.Sprite): self.check_bomb() if str(self.ai_mode) == "neural": self.check_bomb_neural() + if str(self.ai_mode) == "off": + pass