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
|
|
|
|
user_controlled=False
|
|
|
|
|
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 16:50:53 +02:00
|
|
|
goal_state = [self.current_map.mines[0].position_x, self.current_map.mines[0].position_y]
|
2022-04-07 00:55:36 +02:00
|
|
|
print(goal_state)
|
|
|
|
|
|
|
|
find_path = bfs.BFS(self.saper)
|
2022-04-07 05:33:10 +02:00
|
|
|
self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state)
|
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
|
|
|
|
def updateTile(self):
|
|
|
|
#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
|
|
|
|
self.saper.pick_up()
|
|
|
|
|
|
|
|
#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-03-24 11:28:11 +01:00
|
|
|
self.saper.move(180)
|
2022-03-10 00:13:14 +01:00
|
|
|
elif keys[pygame.K_LEFT]:
|
2022-03-24 11:28:11 +01:00
|
|
|
self.saper.move(270)
|
2022-03-10 00:13:14 +01:00
|
|
|
elif keys[pygame.K_RIGHT]:
|
2022-03-24 11:28:11 +01: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):
|
|
|
|
if len(self.the_way)>0:
|
|
|
|
way = self.the_way.pop(0)
|
|
|
|
self.saper.rotate(way)
|
2022-04-07 16:47:58 +02:00
|
|
|
self.saper.move()
|
2022-04-07 16:50:53 +02:00
|
|
|
elif len(self.current_map.mines)!=0:
|
|
|
|
goal_state = [self.current_map.mines[0].position_x, self.current_map.mines[0].position_y]
|
2022-04-07 16:47:58 +02:00
|
|
|
print(goal_state)
|
|
|
|
|
|
|
|
find_path = bfs.BFS(self.saper)
|
|
|
|
self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state)
|