zrobione obracanie i jedzie tylko do przodu
This commit is contained in:
parent
02a833e0c3
commit
62c3409aa1
@ -87,12 +87,13 @@ class AI:
|
|||||||
|
|
||||||
|
|
||||||
def bfs(self):
|
def bfs(self):
|
||||||
dists = []
|
dists = [] # how long way is to all mines from start([0, 0])
|
||||||
for mine in self.current_map.mines:
|
for mine in self.current_map.mines:
|
||||||
dists.append(sqrt((self.saper.position_x-mine.position_x)**2 + (self.saper.position_y-mine.position_y)**2))
|
dists.append(sqrt((self.saper.position_x-mine.position_x)**2 + (self.saper.position_y-mine.position_y)**2))
|
||||||
ind = dists.index(min(dists))
|
# print(dists)
|
||||||
|
ind = dists.index(min(dists)) # choose nearest mine
|
||||||
goal_state = [self.current_map.mines[ind].position_x, self.current_map.mines[ind].position_y]
|
goal_state = [self.current_map.mines[ind].position_x, self.current_map.mines[ind].position_y]
|
||||||
print(goal_state)
|
print(f'We go to {goal_state}')
|
||||||
|
|
||||||
find_path = bfs.BFS(self.saper, self.window)
|
find_path = bfs.BFS(self.saper, self.window)
|
||||||
self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state)
|
self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state)
|
153
classes/bfs.py
153
classes/bfs.py
@ -15,19 +15,108 @@ class BFS:
|
|||||||
def successor(self, current_position):
|
def successor(self, current_position):
|
||||||
new_nodes = []
|
new_nodes = []
|
||||||
neighbours_list = self.agent.sensor(current_position[0], current_position[1])
|
neighbours_list = self.agent.sensor(current_position[0], current_position[1])
|
||||||
|
#print(f'Current position {current_position}')
|
||||||
#print(neighbours_list[0])
|
#print(neighbours_list[0])
|
||||||
#print(neighbours_list[1])
|
#print(neighbours_list[1])
|
||||||
#print(neighbours_list[2])
|
#print(neighbours_list[2])
|
||||||
|
|
||||||
|
|
||||||
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
# if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
new_nodes.append([current_position[0] - 1, current_position[1]])
|
# new_nodes.append([current_position[0] - 1, current_position[1]])
|
||||||
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
# if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
new_nodes.append([current_position[0], current_position[1] + 1])
|
# new_nodes.append([current_position[0], current_position[1] + 1])
|
||||||
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
# if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
new_nodes.append([current_position[0] + 1, current_position[1]])
|
# new_nodes.append([current_position[0] + 1, current_position[1]])
|
||||||
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
# if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
new_nodes.append([current_position[0],current_position[1] - 1])
|
# new_nodes.append([current_position[0],current_position[1] - 1])
|
||||||
|
|
||||||
|
# if current_position[2] == 0: # jesli patrzy na polnoc
|
||||||
|
# if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('forward', [current_position[0] - 1, current_position[1], 0])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('left', [current_position[0],current_position[1], 270])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('right', [current_position[0], current_position[1], 90])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
#
|
||||||
|
# if current_position[2] == 90: # jesli patrzy na wschod
|
||||||
|
# if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('forward', [current_position[0], current_position[1] + 1, 90])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('left', [current_position[0], current_position[1], 0])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('right', [current_position[0], current_position[1], 180])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
#
|
||||||
|
# if current_position[2] == 180: # jesli patczy na poludzie
|
||||||
|
# if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('forward', [current_position[0] + 1, current_position[1], 180])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('left', [current_position[0], current_position[1], 90])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('right', [current_position[0],current_position[1], 270])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
#
|
||||||
|
# if current_position[2] == 270: # jesli patczy na wschod
|
||||||
|
# if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('forward', [current_position[0],current_position[1] - 1, 270])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('left', [current_position[0], current_position[1], 180])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
# if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
# tmp = ('right', [current_position[0], current_position[1], 0])
|
||||||
|
# new_nodes.append(tmp)
|
||||||
|
|
||||||
|
if current_position[2] == 180: # jesli patrzy na polnoc
|
||||||
|
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('forward', [current_position[0], current_position[1] - 1, 180])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('left', [current_position[0],current_position[1], 270])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('right', [current_position[0], current_position[1], 90])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
|
||||||
|
if current_position[2] == 90: # jesli patrzy na wschod
|
||||||
|
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('forward', [current_position[0] + 1, current_position[1], 90])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('left', [current_position[0], current_position[1], 180])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('right', [current_position[0], current_position[1], 0])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
|
||||||
|
if current_position[2] == 0: # jesli patczy na poludzie
|
||||||
|
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('forward', [current_position[0], current_position[1] + 1, 0])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('left', [current_position[0], current_position[1], 90])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('right', [current_position[0],current_position[1], 270])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
|
||||||
|
if current_position[2] == 270: # jesli patczy na wschod
|
||||||
|
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('forward', [current_position[0] - 1,current_position[1], 270])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('left', [current_position[0], current_position[1], 0])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
||||||
|
tmp = ('right', [current_position[0], current_position[1], 180])
|
||||||
|
new_nodes.append(tmp)
|
||||||
|
|
||||||
return new_nodes
|
return new_nodes
|
||||||
|
|
||||||
@ -42,12 +131,13 @@ class BFS:
|
|||||||
def graphsearch(self, fringe, explored, succ, goaltest):
|
def graphsearch(self, fringe, explored, succ, goaltest):
|
||||||
self.window.pause(True)
|
self.window.pause(True)
|
||||||
|
|
||||||
positiont_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy
|
positiont_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy
|
||||||
final_action_list = []
|
print(f'Position {positiont_at_beginning}')
|
||||||
root = node.Node(None, None, positiont_at_beginning[:2]) # parent, action, position
|
final_action_list = [] # lista co ma robic zeby dojechac do miny
|
||||||
|
root = node.Node(None, None, positiont_at_beginning) # parent, action, position
|
||||||
counter = 0
|
counter = 0
|
||||||
heapq.heappush(fringe, (counter, root))
|
heapq.heappush(fringe, (counter, root))
|
||||||
visited_position = []
|
#visited_position = []
|
||||||
|
|
||||||
|
|
||||||
while len(fringe) != 0:
|
while len(fringe) != 0:
|
||||||
@ -56,26 +146,18 @@ class BFS:
|
|||||||
|
|
||||||
if len(fringe) == 0:
|
if len(fringe) == 0:
|
||||||
return False
|
return False
|
||||||
break
|
|
||||||
|
|
||||||
tmp_node = heapq.heappop(fringe) # node
|
tmp_node = heapq.heappop(fringe) # node
|
||||||
#print(f'my parent is {tmp_node[1].get_parent()}')
|
tmp_node_position = tmp_node[1].get_position() # x, y , gdzie patczy
|
||||||
tmp_node_position = tmp_node[1].get_position()
|
#print(f'Position before succ {tmp_node_position}')
|
||||||
|
|
||||||
#print(f'Position of our node {tmp_node_position[:2]}')
|
#visited_position.append(tmp_node_position)
|
||||||
|
explored.append(tmp_node_position)
|
||||||
visited_position.append(tmp_node_position[:2])
|
|
||||||
#print(f'visited position: {visited_position}')
|
|
||||||
|
|
||||||
if tmp_node_position[:2] == goaltest:
|
if tmp_node_position[:2] == goaltest:
|
||||||
#print(' we find node')
|
print('Find')
|
||||||
# print(visited_position)
|
|
||||||
|
|
||||||
#print(fringe)
|
|
||||||
#break
|
|
||||||
while tmp_node[1].get_parent() is not None:
|
while tmp_node[1].get_parent() is not None:
|
||||||
#print('sdfhdfg')
|
|
||||||
#print(tmp_node[1].get_parent()[1].get_position())
|
|
||||||
final_action_list.append(tmp_node[1].get_action())
|
final_action_list.append(tmp_node[1].get_action())
|
||||||
tmp_node = tmp_node[1].get_parent()
|
tmp_node = tmp_node[1].get_parent()
|
||||||
final_action_list.reverse()
|
final_action_list.reverse()
|
||||||
@ -83,21 +165,18 @@ class BFS:
|
|||||||
self.window.pause(False)
|
self.window.pause(False)
|
||||||
return final_action_list
|
return final_action_list
|
||||||
|
|
||||||
|
# nie wiem na razie po co to
|
||||||
|
# chyba visited_position lepszy odpowiednik
|
||||||
|
# explored.append(tmp_node)
|
||||||
|
|
||||||
explored.append(tmp_node)
|
|
||||||
|
|
||||||
neighbours_list_of_our_node = self.successor(tmp_node_position[:2])
|
neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij
|
||||||
#print(neighbours_list_of_our_node)
|
#print(neighbours_list_of_our_node)
|
||||||
|
|
||||||
for node_ in neighbours_list_of_our_node:
|
for node_ in neighbours_list_of_our_node:
|
||||||
# jesli pozucja wezla nie jest w fringe i nie jest w explored
|
# jesli pozucja wezla nie jest w fringe i nie jest w visited
|
||||||
if [node_[0], node_[1]] not in visited_position and node_[0] >= 0 and node_[1] >=0:
|
if [node_[1][0], node_[1][1], node_[1][2]] not in explored and node_[1][0] >= 0 and node_[1][1] >=0:
|
||||||
counter += 1
|
counter += 1
|
||||||
#print([node_[0], node_[1]])
|
x = node.Node(tmp_node, node_[0], node_[1]) # action
|
||||||
x = node.Node(tmp_node, None, [node_[0], node_[1]]) # action
|
|
||||||
heapq.heappush(fringe, (counter, x))
|
heapq.heappush(fringe, (counter, x))
|
||||||
self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[0], node_[1]])
|
self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[1][0], node_[1][1]])
|
||||||
#time.sleep(0.01)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,22 +1,23 @@
|
|||||||
class Node:
|
class Node:
|
||||||
def __init__(self, parent, action, state_array):
|
def __init__(self, parent, action, state_array):
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
|
self.action = action
|
||||||
self.position = state_array
|
self.position = state_array
|
||||||
|
|
||||||
if parent:
|
# if parent:
|
||||||
parent_pos = parent[1].get_position()
|
# parent_pos = parent[1].get_position()
|
||||||
diff = [state_array[0] - parent_pos[0], state_array[1] - parent_pos[1]]
|
# diff = [state_array[0] - parent_pos[0], state_array[1] - parent_pos[1]]
|
||||||
if diff[0]==1:
|
# if diff[0]==1:
|
||||||
action="E"
|
# action="E"
|
||||||
elif diff[0]==-1:
|
# elif diff[0]==-1:
|
||||||
action="W"
|
# action="W"
|
||||||
elif diff[1]==1:
|
# elif diff[1]==1:
|
||||||
action="S"
|
# action="S"
|
||||||
elif diff[1]==-1:
|
# elif diff[1]==-1:
|
||||||
action="N"
|
# action="N"
|
||||||
self.action=action
|
# self.action=action
|
||||||
else:
|
# else:
|
||||||
self.action=action
|
# self.action=action
|
||||||
|
|
||||||
def get_position(self):
|
def get_position(self):
|
||||||
return self.position
|
return self.position
|
||||||
|
48
main.py
48
main.py
@ -1,71 +1,71 @@
|
|||||||
#pygame - biblioteka do symulacji graficznych
|
# pygame - biblioteka do symulacji graficznych
|
||||||
import pygame
|
import pygame
|
||||||
#system - klasy związane z pygame
|
# system - klasy związane z pygame
|
||||||
#minesweeper - klasy związane z samym saperem
|
# minesweeper - klasy związane z samym saperem
|
||||||
#ai - klasa wykonująca ruchy sapera
|
# ai - klasa wykonująca ruchy sapera
|
||||||
from classes import system, minesweeper, ai
|
from classes import system, minesweeper, ai
|
||||||
|
|
||||||
#ustalenie wielkości pojedyńczych kawałków mapy, oraz wielkości mapy
|
# ustalenie wielkości pojedyńczych kawałków mapy, oraz wielkości mapy
|
||||||
TILE_SIZE = 64
|
TILE_SIZE = 64
|
||||||
TILES_X = int(12)
|
TILES_X = int(12)
|
||||||
TILES_Y = int(10)
|
TILES_Y = int(10)
|
||||||
|
|
||||||
#wł/wył muzyki
|
# wł/wył muzyki
|
||||||
MUSIC=True
|
MUSIC = True
|
||||||
|
|
||||||
#ustalenie FPS
|
# ustalenie FPS
|
||||||
FPS = 60
|
FPS = 60
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if MUSIC:
|
if MUSIC:
|
||||||
pygame.mixer.init()
|
pygame.mixer.init()
|
||||||
pygame.mixer.Channel(0).play(pygame.mixer.Sound("assets/music.ogg"), -1)
|
pygame.mixer.Channel(0).play(pygame.mixer.Sound("assets/music.ogg"), -1)
|
||||||
|
|
||||||
#utworzenie okna do gry
|
# utworzenie okna do gry
|
||||||
window = system.Window(TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y, "Intelligent Minesweeper", "icon.png")
|
window = system.Window(TILE_SIZE * TILES_X, TILE_SIZE * TILES_Y, "Intelligent Minesweeper", "icon.png")
|
||||||
|
|
||||||
|
# utworzenie objektu mapy, wygenerowanie jej i narysowanie na ekranie
|
||||||
#utworzenie objektu mapy, wygenerowanie jej i narysowanie na ekranie
|
|
||||||
map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y)
|
map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y)
|
||||||
map.generate()
|
map.generate()
|
||||||
map.draw_tiles()
|
map.draw_tiles()
|
||||||
|
|
||||||
#utworzenie sapera
|
# utworzenie sapera
|
||||||
saper = minesweeper.Minesweeper(0,0, TILE_SIZE)
|
saper = minesweeper.Minesweeper(0, 0, TILE_SIZE)
|
||||||
|
|
||||||
#pierwszy render
|
# pierwszy render
|
||||||
map.draw_tiles()
|
map.draw_tiles()
|
||||||
map.draw_objects()
|
map.draw_objects()
|
||||||
saper.draw(window.window, 0.1)
|
saper.draw(window.window, 0.1)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
#utworzenie objektu klasy AI
|
# utworzenie objektu klasy AI
|
||||||
AI = ai.AI(window, map, saper)
|
AI = ai.AI(window, map, saper)
|
||||||
#wykonanie funkcji ready() AI
|
# wykonanie funkcji ready() AI
|
||||||
AI.ready()
|
AI.ready()
|
||||||
|
|
||||||
#główna pętla
|
# główna pętla
|
||||||
game_loop = True
|
game_loop = True
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
while game_loop:
|
while game_loop:
|
||||||
#wdrożenie FPS, delta - czas od ostatniej klatki
|
# wdrożenie FPS, delta - czas od ostatniej klatki
|
||||||
delta = clock.tick(FPS)
|
delta = clock.tick(FPS)
|
||||||
|
|
||||||
#wykonanie funkcji update() AI
|
# wykonanie funkcji update() AI
|
||||||
AI.updateFPS()
|
AI.updateFPS()
|
||||||
|
|
||||||
if saper.offset_x==0 and saper.offset_y==0:
|
if saper.offset_x == 0 and saper.offset_y == 0:
|
||||||
AI.updateTile()
|
AI.updateTile()
|
||||||
|
|
||||||
#narysowanie terenu i obiektów
|
# narysowanie terenu i obiektów
|
||||||
map.draw_tiles()
|
map.draw_tiles()
|
||||||
map.draw_objects()
|
map.draw_objects()
|
||||||
saper.draw(window.window, delta)
|
saper.draw(window.window, delta)
|
||||||
|
|
||||||
#odświeżenie ekranu
|
# odświeżenie ekranu
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
#sprawdzanie różnych interakcji użytkownika
|
# sprawdzanie różnych interakcji użytkownika
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
game_loop = False
|
game_loop = False
|
||||||
|
Loading…
Reference in New Issue
Block a user