Add bfs path finding
Co-authored-by: Marcin Matoga <marmat35@st.amu.edu.pl> Co-authored-by: Sebastian Piotrowski <sebpio@st.amu.edu.pl> Co-authored-by: Ladislaus3III <Ladislaus3III@users.noreply.github.com>
This commit is contained in:
parent
dafa68f3fd
commit
4b6117779f
10
grid.py
10
grid.py
@ -1,7 +1,8 @@
|
|||||||
from copy import copy, deepcopy
|
#from copy import copy, deepcopy
|
||||||
|
|
||||||
|
"""
|
||||||
class Grid:
|
class Grid:
|
||||||
#def __init__(self, width, height, locationGrid=[[]], graph={}, translationGrid)=:
|
def __init__(self, width, height, locationGrid=[[]], graph={}, translationGrid)=:
|
||||||
def __init__(self, width, height):
|
def __init__(self, width, height):
|
||||||
self.width = width
|
self.width = width
|
||||||
self.heigth = height
|
self.heigth = height
|
||||||
@ -21,12 +22,12 @@ class Grid:
|
|||||||
|
|
||||||
def fillLocationGrid(self):
|
def fillLocationGrid(self):
|
||||||
pass
|
pass
|
||||||
"""self.locationGrid[0] =
|
self.locationGrid[0] =
|
||||||
self.locationGrid[1] =
|
self.locationGrid[1] =
|
||||||
self.locationGrid[2] =
|
self.locationGrid[2] =
|
||||||
self.locationGrid[3] =
|
self.locationGrid[3] =
|
||||||
self.locationGrid[4] =
|
self.locationGrid[4] =
|
||||||
self.locationGrid[5] = """
|
self.locationGrid[5] =
|
||||||
|
|
||||||
|
|
||||||
def printLocationGrid(self):
|
def printLocationGrid(self):
|
||||||
@ -53,3 +54,4 @@ class Grid:
|
|||||||
|
|
||||||
def setTranslationGridCell(self, x, y, value):
|
def setTranslationGridCell(self, x, y, value):
|
||||||
self.translationGrid[x][y] = value
|
self.translationGrid[x][y] = value
|
||||||
|
"""
|
37
main.py
37
main.py
@ -2,6 +2,7 @@ import pygame as pg
|
|||||||
import sys
|
import sys
|
||||||
import ctypes
|
import ctypes
|
||||||
from os import path
|
from os import path
|
||||||
|
from maze import *
|
||||||
from grid import *
|
from grid import *
|
||||||
from settings import *
|
from settings import *
|
||||||
from sprites import *
|
from sprites import *
|
||||||
@ -84,6 +85,38 @@ class Game:
|
|||||||
self.player.move(dy=-1, direction='Up')
|
self.player.move(dy=-1, direction='Up')
|
||||||
if event.key == pg.K_DOWN:
|
if event.key == pg.K_DOWN:
|
||||||
self.player.move(dy=1, direction='Down')
|
self.player.move(dy=1, direction='Down')
|
||||||
|
if event.key == pg.K_F2:
|
||||||
|
self.player.maze.run()
|
||||||
|
self.player.parse_maze_moves()
|
||||||
|
self.i_like_to_move_it()
|
||||||
|
|
||||||
|
def i_like_to_move_it(self):
|
||||||
|
for i in self.player.moves:
|
||||||
|
if i == 'Right':
|
||||||
|
self.player.move(dx=1, direction='Right')
|
||||||
|
|
||||||
|
if i == 'Turn Right':
|
||||||
|
self.player.move(0, 0, direction='Right')
|
||||||
|
|
||||||
|
if i == 'Left':
|
||||||
|
self.player.move(dx=-1, direction='Left')
|
||||||
|
|
||||||
|
if i == 'Turn Left':
|
||||||
|
self.player.move(0, 0, direction='Left')
|
||||||
|
|
||||||
|
if i == 'Down':
|
||||||
|
self.player.move(dy=1, direction='Down')
|
||||||
|
|
||||||
|
if i == 'Turn Down':
|
||||||
|
self.player.move(0, 0, direction='Down')
|
||||||
|
|
||||||
|
if i == 'Up':
|
||||||
|
self.player.move(dy=-1, direction='Up')
|
||||||
|
|
||||||
|
if i == 'Turn Up':
|
||||||
|
self.player.move(0, 0, direction='Up')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def show_start_screen(self):
|
def show_start_screen(self):
|
||||||
pass
|
pass
|
||||||
@ -94,6 +127,10 @@ class Game:
|
|||||||
# create the game object
|
# create the game object
|
||||||
g = Game()
|
g = Game()
|
||||||
g.show_start_screen()
|
g.show_start_screen()
|
||||||
|
|
||||||
|
#m = Maze()
|
||||||
|
#m.run()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
g.new()
|
g.new()
|
||||||
g.run()
|
g.run()
|
||||||
|
11
map.txt
11
map.txt
@ -1,4 +1,7 @@
|
|||||||
.A....
|
.A.....
|
||||||
..222.
|
.......
|
||||||
.222..
|
.......
|
||||||
......
|
.......
|
||||||
|
.......
|
||||||
|
....2..
|
||||||
|
.......
|
||||||
|
117
maze.py
Normal file
117
maze.py
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
import queue
|
||||||
|
from os import path
|
||||||
|
|
||||||
|
class Maze:
|
||||||
|
def __init__(self):
|
||||||
|
self.maze = []
|
||||||
|
self.moves = ""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def loadMaze(self):
|
||||||
|
maze_folder = path.dirname(__file__)
|
||||||
|
with open(path.join(maze_folder, 'map.txt'), 'rt') as f:
|
||||||
|
for line in f:
|
||||||
|
self.maze.append(line)
|
||||||
|
return self.maze
|
||||||
|
|
||||||
|
def printMaze(self, maze, path=""):
|
||||||
|
for x, pos in enumerate(maze[0]):
|
||||||
|
if pos == "A":
|
||||||
|
start = x
|
||||||
|
|
||||||
|
i = start
|
||||||
|
j = 0
|
||||||
|
pos = set()
|
||||||
|
for move in path:
|
||||||
|
if move == "L":
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
elif move == "R":
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
elif move == "U":
|
||||||
|
j -= 1
|
||||||
|
|
||||||
|
elif move == "D":
|
||||||
|
j += 1
|
||||||
|
pos.add((j,i))
|
||||||
|
|
||||||
|
for j, row in enumerate(maze):
|
||||||
|
for i, col in enumerate(row):
|
||||||
|
if (j, i) in pos:
|
||||||
|
print("+ ", end="")
|
||||||
|
else:
|
||||||
|
print(col + " ", end="")
|
||||||
|
print()
|
||||||
|
|
||||||
|
def valid(self, maze, moves):
|
||||||
|
for x, pos in enumerate(maze[0]):
|
||||||
|
if pos == "A":
|
||||||
|
start = x
|
||||||
|
|
||||||
|
i = start
|
||||||
|
j = 0
|
||||||
|
for move in moves:
|
||||||
|
if move == "L":
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
elif move == "R":
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
elif move == "U":
|
||||||
|
j -= 1
|
||||||
|
|
||||||
|
elif move == "D":
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
if not(0 <= i < len(maze[0]) and 0 <= j < len(maze)):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def findEnd(self, maze, moves):
|
||||||
|
for x, pos in enumerate(maze[0]):
|
||||||
|
if pos == "A":
|
||||||
|
start = x
|
||||||
|
|
||||||
|
i = start
|
||||||
|
j = 0
|
||||||
|
for move in moves:
|
||||||
|
if move == "L":
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
elif move == "R":
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
elif move == "U":
|
||||||
|
j -= 1
|
||||||
|
|
||||||
|
elif move == "D":
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
if maze[j][i] == "2":
|
||||||
|
print("Found: " + moves)
|
||||||
|
self.moves = moves
|
||||||
|
self.printMaze(maze, moves)
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
nums = queue.Queue()
|
||||||
|
nums.put("")
|
||||||
|
add = ""
|
||||||
|
self.loadMaze()
|
||||||
|
|
||||||
|
while not self.findEnd(self.maze, add):
|
||||||
|
add = nums.get()
|
||||||
|
for j in ["L", "R", "U", "D"]:
|
||||||
|
put = add + j
|
||||||
|
if self.valid(self.maze, put):
|
||||||
|
nums.put(put)
|
||||||
|
|
||||||
|
|
@ -8,7 +8,7 @@ RED = (255, 0, 0)
|
|||||||
YELLOW = (255, 255, 0)
|
YELLOW = (255, 255, 0)
|
||||||
|
|
||||||
# game settings
|
# game settings
|
||||||
MAP_SIZE = 6
|
MAP_SIZE = 7
|
||||||
TILESIZE = 100
|
TILESIZE = 100
|
||||||
WIDTH = TILESIZE * MAP_SIZE # 16 * 64 or 32 * 32 or 64 * 16
|
WIDTH = TILESIZE * MAP_SIZE # 16 * 64 or 32 * 32 or 64 * 16
|
||||||
HEIGHT = TILESIZE * MAP_SIZE # 16 * 48 or 32 * 24 or 64 * 12
|
HEIGHT = TILESIZE * MAP_SIZE # 16 * 48 or 32 * 24 or 64 * 12
|
||||||
|
84
sprites.py
84
sprites.py
@ -1,7 +1,9 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
import enum
|
import enum
|
||||||
import ctypes
|
import ctypes
|
||||||
|
import ast
|
||||||
from settings import *
|
from settings import *
|
||||||
|
from maze import *
|
||||||
|
|
||||||
class Player(pg.sprite.Sprite):
|
class Player(pg.sprite.Sprite):
|
||||||
def __init__(self, game, x, y, direction = 'Right'):
|
def __init__(self, game, x, y, direction = 'Right'):
|
||||||
@ -18,6 +20,8 @@ class Player(pg.sprite.Sprite):
|
|||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.direction = direction
|
self.direction = direction
|
||||||
|
self.maze = Maze()
|
||||||
|
self.moves = ''
|
||||||
|
|
||||||
def move(self, dx=0, dy=0, direction = ''):
|
def move(self, dx=0, dy=0, direction = ''):
|
||||||
if direction == self.direction:
|
if direction == self.direction:
|
||||||
@ -25,7 +29,9 @@ class Player(pg.sprite.Sprite):
|
|||||||
if dx > 0:
|
if dx > 0:
|
||||||
if self.check_border(dx):
|
if self.check_border(dx):
|
||||||
if self.collide_with_mines(dx):
|
if self.collide_with_mines(dx):
|
||||||
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
||||||
|
print("Mine Ahead!")
|
||||||
|
self.x += dx
|
||||||
else:
|
else:
|
||||||
self.x += dx
|
self.x += dx
|
||||||
|
|
||||||
@ -33,7 +39,9 @@ class Player(pg.sprite.Sprite):
|
|||||||
if dy < 0:
|
if dy < 0:
|
||||||
if self.check_border(0, dy):
|
if self.check_border(0, dy):
|
||||||
if self.collide_with_mines(0, dy):
|
if self.collide_with_mines(0, dy):
|
||||||
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
||||||
|
print("Mine Ahead!")
|
||||||
|
self.y += dy
|
||||||
else:
|
else:
|
||||||
self.y += dy
|
self.y += dy
|
||||||
|
|
||||||
@ -41,7 +49,9 @@ class Player(pg.sprite.Sprite):
|
|||||||
if dy > 0:
|
if dy > 0:
|
||||||
if self.check_border(0, dy):
|
if self.check_border(0, dy):
|
||||||
if self.collide_with_mines(0, dy):
|
if self.collide_with_mines(0, dy):
|
||||||
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
||||||
|
print("Mine Ahead!")
|
||||||
|
self.y += dy
|
||||||
else:
|
else:
|
||||||
self.y += dy
|
self.y += dy
|
||||||
|
|
||||||
@ -49,7 +59,9 @@ class Player(pg.sprite.Sprite):
|
|||||||
if dx < 0:
|
if dx < 0:
|
||||||
if self.check_border(dx):
|
if self.check_border(dx):
|
||||||
if self.collide_with_mines(dx):
|
if self.collide_with_mines(dx):
|
||||||
ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
||||||
|
print("Mine Ahead!")
|
||||||
|
self.x += dx
|
||||||
else:
|
else:
|
||||||
self.x += dx
|
self.x += dx
|
||||||
|
|
||||||
@ -78,6 +90,70 @@ class Player(pg.sprite.Sprite):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
right = 1
|
||||||
|
left = -1
|
||||||
|
if self.self.moves[n+1] != 'R':
|
||||||
|
if self.moves[n+1] == 'U':
|
||||||
|
module.insert(n, 'Turn Up')
|
||||||
|
if self.moves[n+1] == 'L':
|
||||||
|
module.insert(n, 'Turn Left')
|
||||||
|
if self.moves[n+1] == 'D':
|
||||||
|
module.insert(n, 'Turn Down')
|
||||||
|
"""
|
||||||
|
|
||||||
|
def parse_maze_moves(self):
|
||||||
|
self.moves = [char for char in self.maze.moves]
|
||||||
|
for n, i in enumerate(self.moves):
|
||||||
|
if i == 'R':
|
||||||
|
self.moves[n] = 'Right'
|
||||||
|
if n != len(self.moves)-1:
|
||||||
|
if i != self.moves[n+1]:
|
||||||
|
if self.moves[n+1] == 'D':
|
||||||
|
self.moves.insert(n+1, 'Turn Down')
|
||||||
|
if self.moves[n+1] == 'U':
|
||||||
|
self.moves.insert(n+1, 'Turn Up')
|
||||||
|
if self.moves[n+1] == 'L':
|
||||||
|
self.moves.insert(n+1, 'Turn Left')
|
||||||
|
else:
|
||||||
|
self.move(dx=-1, direction='Right')
|
||||||
|
if i == 'L':
|
||||||
|
self.moves[n] = 'Left'
|
||||||
|
if n != len(self.moves)-1:
|
||||||
|
if i != self.moves[n+1]:
|
||||||
|
if self.moves[n+1] == 'D':
|
||||||
|
self.moves.insert(n+1, 'Turn Down')
|
||||||
|
if self.moves[n+1] == 'U':
|
||||||
|
self.moves.insert(n+1, 'Turn Up')
|
||||||
|
if self.moves[n+1] == 'R':
|
||||||
|
self.moves.insert(n+1, 'Turn Right')
|
||||||
|
if i == 'D':
|
||||||
|
self.moves[n] = 'Down'
|
||||||
|
if n != len(self.moves)-1:
|
||||||
|
if i != self.moves[n+1]:
|
||||||
|
if self.moves[n+1] == 'R':
|
||||||
|
self.moves.insert(n+1, 'Turn Right')
|
||||||
|
if self.moves[n+1] == 'U':
|
||||||
|
self.moves.insert(n+1, 'Turn Up')
|
||||||
|
if self.moves[n+1] == 'L':
|
||||||
|
self.moves.insert(n+1, 'Turn Left')
|
||||||
|
if i == 'U':
|
||||||
|
self.moves[n] = 'Up'
|
||||||
|
if n != len(self.moves)-1:
|
||||||
|
if i != self.moves[n+1]:
|
||||||
|
if self.moves[n+1] == 'D':
|
||||||
|
self.moves.insert(n+1, 'Turn Down')
|
||||||
|
if self.moves[n+1] == 'R':
|
||||||
|
self.moves.insert(n+1, 'Turn Right')
|
||||||
|
if self.moves[n+1] == 'L':
|
||||||
|
self.moves.insert(n+1, 'Turn Left')
|
||||||
|
print(self.moves)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.rect.x = self.x * TILESIZE
|
self.rect.x = self.x * TILESIZE
|
||||||
self.rect.y = self.y * TILESIZE
|
self.rect.y = self.y * TILESIZE
|
||||||
|
Loading…
Reference in New Issue
Block a user