InteligentnySaper/classes/ai.py

105 lines
3.2 KiB
Python
Raw Normal View History

2022-04-07 17:13:50 +02:00
from math import dist, sqrt
2022-03-10 00:13:14 +01:00
import pygame
from classes import minesweeper, system, bfs
2022-03-10 00:13:14 +01:00
from random import randrange
class AI:
window:system.Window
2022-03-24 11:28:11 +01:00
current_map:minesweeper.Map
2022-03-10 00:13:14 +01:00
saper:minesweeper.Minesweeper
2022-04-07 05:33:10 +02:00
the_way:list
2022-03-10 00:13:14 +01:00
#jak True to można się poruszać strzałkami, jak False sam się porusza
2022-04-16 20:15:33 +02:00
user_controlled=False
2022-03-10 00:13:14 +01:00
2022-03-24 11:28:11 +01:00
def __init__(self, window, current_map, saper):
2022-03-10 00:13:14 +01:00
self.window = window
2022-03-24 11:28:11 +01:00
self.current_map = current_map
2022-03-10 00:13:14 +01:00
self.saper = saper
#co ma zrobić tylko na początku
def ready(self):
2022-03-24 11:28:11 +01:00
self.saper.set_map(self.current_map)
2022-04-07 17:13:50 +02:00
self.bfs()
2022-03-10 00:13:14 +01:00
#co ma robić przy każdym FPS'ie
def updateFPS(self):
pass
#co ma zrobić przy każdym ruchu <------------------------- najważniejsze
2022-05-19 12:26:48 +02:00
def updateTile(self, model):
#aktualne pola (do debugu)
sensor = self.saper.sensor()
2022-04-07 05:33:10 +02:00
#print(sensor[0])
#print(sensor[1])
#print(sensor[2])
#print("-------")
2022-04-06 19:57:42 +02:00
#podniesienie bomby jeśli jest jakaś na tym polu
2022-05-19 12:26:48 +02:00
self.saper.pick_up(model)
#poruszenie się
2022-03-10 00:13:14 +01:00
if self.user_controlled:
self.minesweeper_controls()
return
2022-04-07 05:33:10 +02:00
self.way_controls()
2022-03-10 00:13:14 +01:00
def minesweeper_controls(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
2022-03-24 11:28:11 +01:00
self.saper.move(0)
2022-03-10 00:13:14 +01:00
elif keys[pygame.K_UP]:
2022-04-16 20:14:55 +02:00
self.saper.move(180)
2022-03-10 00:13:14 +01:00
elif keys[pygame.K_LEFT]:
2022-04-16 20:14:55 +02:00
self.saper.move(270)
2022-03-10 00:13:14 +01:00
elif keys[pygame.K_RIGHT]:
2022-04-16 20:14:55 +02:00
self.saper.move(90)
2022-03-10 00:13:14 +01:00
def chaos_controls(self):
dir = randrange(4)
if dir==0:
self.saper.rotate("N")
2022-03-10 00:13:14 +01:00
elif dir==1:
self.saper.rotate("S")
2022-03-10 00:13:14 +01:00
elif dir==2:
self.saper.rotate("W")
2022-03-10 00:13:14 +01:00
elif dir==3:
self.saper.rotate("E")
2022-04-07 05:33:10 +02:00
self.saper.move()
def way_controls(self):
2022-04-21 18:20:00 +02:00
if type(self.the_way) is list and len(self.the_way)>0:
2022-04-07 05:33:10 +02:00
way = self.the_way.pop(0)
2022-04-07 17:51:51 +02:00
if way=="move":
self.saper.move()
elif way=="left" or way=="right":
self.saper.rotate(way)
else:
self.saper.rotate(way)
self.saper.move()
2022-05-28 15:55:33 +02:00
elif len(self.current_map.encounters)!=0:
2022-04-07 17:13:50 +02:00
self.bfs()
def bfs(self):
2022-05-28 15:55:33 +02:00
dists = [] # how long way is to all encounters from start([0, 0])
for encounter in self.current_map.encounters:
2022-04-28 09:32:14 +02:00
# dists.append(sqrt((self.saper.position_x-mine.position_x)**2 + (self.saper.position_y-mine.position_y)**2))
2022-05-28 15:55:33 +02:00
dists.append(abs(self.saper.position_x - encounter.position_x) + abs(self.saper.position_y - encounter.position_y))
2022-04-28 13:40:35 +02:00
#print(dists)
ind = dists.index(min(dists)) # choose nearest mine
2022-05-28 15:55:33 +02:00
goal_state = [self.current_map.encounters[ind].position_x, self.current_map.encounters[ind].position_y]
print(f'We go to {goal_state}')
2022-04-07 16:47:58 +02:00
2022-04-07 18:27:35 +02:00
find_path = bfs.BFS(self.saper, self.window)
2022-04-28 09:32:14 +02:00
tmp = find_path.graphsearch([], [], bfs.BFS.successor, goal_state)
2022-04-28 13:40:35 +02:00
#print(f'tmp = {tmp}')
2022-04-28 09:32:14 +02:00
if tmp is None:
raise Exception("Error, path does not exist")
else:
self.the_way = tmp