2022-04-07 17:13:50 +02:00
|
|
|
from math import dist, sqrt
|
2022-03-10 00:13:14 +01:00
|
|
|
import pygame
|
2022-04-07 00:55:36 +02:00
|
|
|
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 00:55:36 +02:00
|
|
|
|
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
|
2022-04-07 00:55:36 +02:00
|
|
|
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):
|
2022-04-07 00:55:36 +02:00
|
|
|
#aktualne pola (do debugu)
|
|
|
|
sensor = self.saper.sensor()
|
2022-04-07 01:05:49 +02:00
|
|
|
|
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
|
|
|
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
#podniesienie bomby jeśli jest jakaś na tym polu
|
2022-05-19 12:26:48 +02:00
|
|
|
self.saper.pick_up(model)
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
#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:
|
2022-04-07 00:55:36 +02:00
|
|
|
self.saper.rotate("N")
|
2022-03-10 00:13:14 +01:00
|
|
|
elif dir==1:
|
2022-04-07 00:55:36 +02:00
|
|
|
self.saper.rotate("S")
|
2022-03-10 00:13:14 +01:00
|
|
|
elif dir==2:
|
2022-04-07 00:55:36 +02:00
|
|
|
self.saper.rotate("W")
|
2022-03-10 00:13:14 +01:00
|
|
|
elif dir==3:
|
2022-04-07 00:55:36 +02:00
|
|
|
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-06-08 22:23:59 +02:00
|
|
|
def mojafunkcja(ileczasu, jakdaleko, jaktrudno, jakciezko):
|
|
|
|
return ((200-ileczasu)+(200-jakdaleko*10))-jaktrudno*jakciezko
|
|
|
|
|
|
|
|
def fitness(x, y, z, w): #funkcja do ewaluacji
|
|
|
|
ans = mojafunkcja(x, y, z, w)
|
|
|
|
return abs(ans-400)
|
2022-05-28 15:55:33 +02:00
|
|
|
dists = [] # how long way is to all encounters from start([0, 0])
|
2022-06-08 22:23:59 +02:00
|
|
|
ranked = []
|
2022-05-28 15:55:33 +02:00
|
|
|
for encounter in self.current_map.encounters:
|
|
|
|
dists.append(abs(self.saper.position_x - encounter.position_x) + abs(self.saper.position_y - encounter.position_y))
|
2022-06-08 22:23:59 +02:00
|
|
|
|
|
|
|
for a in self.current_map.mines:
|
|
|
|
ranked.append((fitness(a.explosion_timer, (abs(self.saper.position_x - encounter.position_x) + abs(self.saper.position_y - encounter.position_y)), a.difficulty, a.weight), a))
|
|
|
|
ranked.sort() #posortowana lista najlepszych wyborów dla min
|
|
|
|
if ranked:
|
|
|
|
print(ranked[0][0])
|
|
|
|
indx = ranked[0][1].position_x
|
|
|
|
indy = ranked[0][1].position_y
|
|
|
|
else:
|
|
|
|
ind = dists.index(min(dists))
|
|
|
|
indx = self.current_map.encounters[ind].position_x
|
|
|
|
indy = self.current_map.encounters[ind].position_y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
goal_state = [indx, indy]
|
2022-04-16 00:11:24 +02:00
|
|
|
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
|