From 2aab0280098f8b009cb3afba629543a862629e71 Mon Sep 17 00:00:00 2001 From: s464859 <@> Date: Thu, 7 Apr 2022 05:33:10 +0200 Subject: [PATCH] prawie dzialajacy bfs --- .gitignore | 1 + .idea/.gitignore | 8 ------- .idea/InteligentnySaper.iml | 10 --------- .../inspectionProfiles/profiles_settings.xml | 6 ----- .idea/misc.xml | 4 ---- .idea/modules.xml | 8 ------- .idea/vcs.xml | 6 ----- classes/ai.py | 22 +++++++++++++------ classes/bfs.py | 9 +++----- classes/node.py | 16 +++++++++++++- main.py | 3 +-- 11 files changed, 35 insertions(+), 58 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/InteligentnySaper.iml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 88315da..c7e1622 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] +.idea # C extensions *.so diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/InteligentnySaper.iml b/.idea/InteligentnySaper.iml deleted file mode 100644 index 74d515a..0000000 --- a/.idea/InteligentnySaper.iml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 8e1d7b1..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 55dbd56..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/classes/ai.py b/classes/ai.py index 4cfa032..7c1cae5 100644 --- a/classes/ai.py +++ b/classes/ai.py @@ -6,6 +6,8 @@ class AI: window:system.Window current_map:minesweeper.Map saper:minesweeper.Minesweeper + + the_way:list #jak True to można się poruszać strzałkami, jak False sam się porusza user_controlled=False @@ -23,7 +25,7 @@ class AI: print(goal_state) find_path = bfs.BFS(self.saper) - find_path.graphsearch([], [], bfs.BFS.successor, goal_state) + self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state) #co ma robić przy każdym FPS'ie def updateFPS(self): @@ -34,10 +36,10 @@ class AI: #aktualne pola (do debugu) sensor = self.saper.sensor() - print(sensor[0]) - print(sensor[1]) - print(sensor[2]) - print("-------") + #print(sensor[0]) + #print(sensor[1]) + #print(sensor[2]) + #print("-------") #podniesienie bomby jeśli jest jakaś na tym polu @@ -47,7 +49,7 @@ class AI: if self.user_controlled: self.minesweeper_controls() return - self.chaos_controls() # <--------------------------zamiast tego trzeba wstawić jakiś algorytm + self.way_controls() def minesweeper_controls(self): @@ -71,4 +73,10 @@ class AI: self.saper.rotate("W") elif dir==3: self.saper.rotate("E") - self.saper.move() \ No newline at end of file + self.saper.move() + + def way_controls(self): + if len(self.the_way)>0: + way = self.the_way.pop(0) + self.saper.rotate(way) + self.saper.move() \ No newline at end of file diff --git a/classes/bfs.py b/classes/bfs.py index 106896f..d06cf0a 100644 --- a/classes/bfs.py +++ b/classes/bfs.py @@ -66,19 +66,16 @@ class BFS: print(' we find node') # print(visited_position) - print(fringe) + #print(fringe) #break while tmp_node[1].get_parent() is not None: #print('sdfhdfg') #print(tmp_node[1].get_parent()) final_action_list.append(tmp_node[1].get_action()) - #print(final_action_list) tmp_node = tmp_node[1].get_parent() - final_action_list = reversed(final_action_list) + final_action_list.reverse() print(final_action_list) - break - - # return final_action_list + return final_action_list explored.append(tmp_node) diff --git a/classes/node.py b/classes/node.py index accc175..fa3b994 100644 --- a/classes/node.py +++ b/classes/node.py @@ -1,9 +1,23 @@ class Node: def __init__(self, parent, action, state_array): self.parent = parent - self.action = action self.position = state_array + if parent: + parent_pos = parent[1].get_position() + diff = [state_array[0] - parent_pos[0], state_array[1] - parent_pos[1]] + if diff[0]==1: + action="E" + elif diff[0]==-1: + action="W" + elif diff[1]==1: + action="S" + elif diff[1]==-1: + action="N" + self.action=action + else: + self.action=action + def get_position(self): return self.position diff --git a/main.py b/main.py index 0f94171..c9c829c 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import pygame #system - klasy związane z pygame #minesweeper - klasy związane z samym saperem #ai - klasa wykonująca ruchy sapera -from classes import system, minesweeper, ai, bfs +from classes import system, minesweeper, ai #ustalenie wielkości pojedyńczych kawałków mapy, oraz wielkości mapy TILE_SIZE = 64 @@ -38,7 +38,6 @@ def main(): saper = minesweeper.Minesweeper(0,0, TILE_SIZE) - #utworzenie objektu klasy AI AI = ai.AI(window, map, saper) #wykonanie funkcji ready() AI