From 077d2b801559c4190d66502f97c27c936a2d5991 Mon Sep 17 00:00:00 2001 From: s464859 <@> Date: Thu, 7 Apr 2022 17:13:50 +0200 Subject: [PATCH] naprawiony bfs 4 --- classes/ai.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/classes/ai.py b/classes/ai.py index cc4c164..beeadca 100644 --- a/classes/ai.py +++ b/classes/ai.py @@ -1,3 +1,4 @@ +from math import dist, sqrt import pygame from classes import minesweeper, system, bfs from random import randrange @@ -21,11 +22,7 @@ class AI: def ready(self): self.saper.set_map(self.current_map) - goal_state = [self.current_map.mines[0].position_x, self.current_map.mines[0].position_y] - print(goal_state) - - find_path = bfs.BFS(self.saper) - self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state) + self.bfs() #co ma robić przy każdym FPS'ie def updateFPS(self): @@ -81,8 +78,16 @@ class AI: self.saper.rotate(way) self.saper.move() elif len(self.current_map.mines)!=0: - goal_state = [self.current_map.mines[0].position_x, self.current_map.mines[0].position_y] - print(goal_state) + self.bfs() + + + def bfs(self): + dists = [] + 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)) + ind = dists.index(min(dists)) + goal_state = [self.current_map.mines[ind].position_x, self.current_map.mines[ind].position_y] + print(goal_state) - find_path = bfs.BFS(self.saper) - self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state) \ No newline at end of file + find_path = bfs.BFS(self.saper) + self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state) \ No newline at end of file