storage #4

Merged
s473603 merged 2 commits from storage into master 2023-05-26 12:10:07 +02:00
2 changed files with 77 additions and 49 deletions

View File

@ -1,9 +1,12 @@
import asyncio
import random
import time
from heapq import *
from enum import Enum, IntEnum
from queue import PriorityQueue
from collections import deque
from threading import Thread
from IC3 import tree
import pygame
@ -22,8 +25,7 @@ WATER_TANK_CAPACITY = 10
GAS_TANK_CAPACITY = 250
SPAWN_POINT = (0, 0)
SKLEP_POINT = (14, 14)
TIMEOUT = 1
tractor_image = pygame.transform.scale(pygame.image.load("images/tractor_image.png"), (BLOCK_SIZE, BLOCK_SIZE))
rock_image = pygame.transform.scale(pygame.image.load("images/rock_image.png"), (BLOCK_SIZE, BLOCK_SIZE))
@ -32,7 +34,10 @@ carrot_image = pygame.transform.scale(pygame.image.load("images/carrot.png"), (B
broccoli_image = pygame.transform.scale(pygame.image.load("images/broccoli.png"), (BLOCK_SIZE, BLOCK_SIZE))
onion_image = pygame.transform.scale(pygame.image.load("images/onion.png"), (BLOCK_SIZE, BLOCK_SIZE))
gas_station_image = pygame.transform.scale(pygame.image.load("images/gas_station.png"), (BLOCK_SIZE, BLOCK_SIZE))
sklep_station_image = pygame.transform.scale(pygame.image.load("images/storage.png"), (BLOCK_SIZE, BLOCK_SIZE))
gas_station_closed_image = pygame.transform.scale(pygame.image.load("images/gas_station_closed.png"), (BLOCK_SIZE, BLOCK_SIZE))
sklep_station_image = pygame.transform.scale(pygame.image.load("images/storage_open.png"), (BLOCK_SIZE, BLOCK_SIZE))
sklep_closed_station_image = pygame.transform.scale(pygame.image.load("images/storage_closed.png"),
(BLOCK_SIZE, BLOCK_SIZE))
font = pygame.font.Font('freesansbold.ttf', BLOCK_SIZE // 2)
@ -55,7 +60,6 @@ def get_click_mouse_pos():
return (grid_x, grid_y, Direction.RIGHT) if click[0] else False
def draw_interface():
def returnFun():
for y, row in enumerate(grid.grid):
@ -86,9 +90,9 @@ def draw_interface():
grid = Grid(BOARD_SIZE, BOARD_SIZE, BLOCK_SIZE)
graph1 = Graph(grid)
graph1.initialize_graph(grid)
t2 = Thread(target=close_open, args=(grid,))
t2.setDaemon(True)
t2.start()
fl_running = True
while fl_running:
draw_grid()
@ -115,6 +119,11 @@ def draw_interface():
# movement(tractor, grid, b)
updateDisplay(tractor, grid)
# graph1.initialize_graph(grid)
class Direction(IntEnum):
UP = 0
@ -146,6 +155,8 @@ class Grid:
self.block_size = block_size
self.grid = [[types.EMPTY for col in range(BOARD_SIZE)] for row in range(BOARD_SIZE)]
self.initialize_grid()
self.is_gas_station_closed = False
self.is_storage_closed = False
def add_object(self, x, y, type_of_object: types):
if self.grid[x][y] == types.EMPTY:
@ -179,15 +190,15 @@ class Grid:
class Graph:
def __init__(self, grid: Grid):
self.graph = {}
self.initialize_graph(grid)
# self.initialize_graph(grid)
def initialize_graph(self, grid: Grid):
for y, row in enumerate(grid.grid):
for x, col in enumerate(row):
for direction in Direction:
self.graph[(x, y, direction)] = get_next_nodes(x, y, direction, grid)
# def initialize_graph(self, grid: Grid):
# for y, row in enumerate(grid.grid):
# for x, col in enumerate(row):
# for direction in Direction:
# self.graph[(x, y, direction)] = get_next_nodes(x, y, direction, grid)
def a_star(self, start, goal):
def a_star(self, start, goal, grid: Grid):
# not finished yet https://www.youtube.com/watch?v=abHftC1GU6w
queue = PriorityQueue()
queue.put((0, start))
@ -195,7 +206,8 @@ class Graph:
visited = {start: None}
returnGoal = goal
h = lambda start, goal: abs(start[0] - goal[0]) + abs(start[1] - goal[1]) #heuristic function (manhattan distance)
h = lambda start, goal: abs(start[0] - goal[0]) + abs(
start[1] - goal[1]) # heuristic function (manhattan distance)
while not queue.empty():
cur_cost, cur_node = queue.get()
@ -203,7 +215,7 @@ class Graph:
returnGoal = cur_node
break
next_nodes = self.graph[cur_node]
next_nodes = get_next_nodes(cur_node[0], cur_node[1], cur_node[2], grid)
for next_node in next_nodes:
neigh_cost, neigh_node = next_node
@ -258,7 +270,9 @@ class Tractor:
def get_next_nodes(x, y, direction: Direction, grid: Grid):
check_next_node = lambda x, y: True if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE else False
way = [0, -1] if direction == Direction.UP else [1, 0] if direction == Direction.RIGHT else [0, 1] if direction == Direction.DOWN else [-1, 0]
way = [0, -1] if direction == Direction.UP else [1, 0] if direction == Direction.RIGHT else [0,
1] if direction == Direction.DOWN else [
-1, 0]
next_nodes = []
for new_direction in Direction:
if new_direction != direction:
@ -291,6 +305,8 @@ def movement(tractor: Tractor, grid: Grid, road):
else:
tractor.rot_center(Direction.LEFT)
updateDisplay(tractor, grid)
def getCost(tractor: Tractor, grid: Grid, road):
n = len(road)
cost = 0
@ -314,6 +330,7 @@ def getCost(tractor: Tractor, grid: Grid, road):
cost += 1
return cost
def getRoad(start, goal, visited):
arr = []
aFrom = goal
@ -340,7 +357,14 @@ def updateDisplay(tractor: Tractor, grid: Grid):
elif grid.grid[x][y] == types.ROCK:
sc.blit(rock_image, (x * BLOCK_SIZE, y * BLOCK_SIZE))
sc.blit(gas_station_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE))
if grid.is_storage_closed:
sc.blit(sklep_closed_station_image, (SKLEP_POINT[0] * BLOCK_SIZE, SKLEP_POINT[1] * BLOCK_SIZE))
else:
sc.blit(sklep_station_image, (SKLEP_POINT[0] * BLOCK_SIZE, SKLEP_POINT[1] * BLOCK_SIZE))
if grid.is_gas_station_closed:
sc.blit(gas_station_closed_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE))
else:
sc.blit(gas_station_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE))
# region text
vegetables_text = font.render(
@ -363,8 +387,8 @@ def updateDisplay(tractor: Tractor, grid: Grid):
pygame.time.Clock().tick(60)
def decisionTree(startpoint, endpoint, tractor, grid, graph1):
def decisionTree(startpoint, endpoint, tractor, grid, graph1):
one = "can it get to the next point"
two = "will it be able to get to the gas station"
three = "will it be able to get to the gas station after arriving at the next point"
@ -383,28 +407,26 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
arr.append(seven)
arr.append(eight)
a1, c1 = graph1.a_star(startpoint, endpoint)
a1, c1 = graph1.a_star(startpoint, endpoint, grid)
b1 = getRoad(startpoint, c1, a1)
cost1 = getCost(tractor, grid, b1)
a2, c2 = graph1.a_star(startpoint, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT))
a2, c2 = graph1.a_star(startpoint, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT), grid)
b2 = getRoad(startpoint, c2, a2)
cost2 = getCost(tractor, grid, b2)
a3, c3 = graph1.a_star(startpoint, (SKLEP_POINT[0], SKLEP_POINT[1], Direction.RIGHT))
a3, c3 = graph1.a_star(startpoint, (SKLEP_POINT[0], SKLEP_POINT[1], Direction.RIGHT), grid)
b3 = getRoad(startpoint, c3, a3)
cost3 = getCost(tractor, grid, b3)
a4, c4 = graph1.a_star(c1, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT))
a4, c4 = graph1.a_star(c1, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT), grid)
b4 = getRoad(c1, c4, a4)
cost4 = getCost(tractor, grid, b4)
a5, c5 = graph1.a_star(c3, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT))
a5, c5 = graph1.a_star(c3, (SPAWN_POINT[0], SPAWN_POINT[1], Direction.RIGHT), grid)
b5 = getRoad(c3, c5, a5)
cost5 = getCost(tractor, grid, b5)
if tractor.gas - cost1 > 0:
can_it_get_to_the_next_point = 1
else:
@ -438,9 +460,8 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
else:
will_it_be_able_to_get_to_the_gas_station_after_it_arrives_at_the_vegetable_warehouse = 0
is_the_vegetable_warehouse_closed=0
is_the_gas_station_closed=0
is_the_vegetable_warehouse_closed = grid.is_storage_closed
is_the_gas_station_closed = grid.is_gas_station_closed
brr = []
brr.append(can_it_get_to_the_next_point)
@ -462,6 +483,7 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
return predict(tree[root_node][feature_value])
else:
return None
decision = predict(tree)
print(decision)
if decision == 1:
@ -475,3 +497,9 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
if decision == 5:
print("GAME OVER")
def close_open(grid: Grid):
while True:
time.sleep(TIMEOUT)
grid.is_gas_station_closed = bool(random.getrandbits(1))
grid.is_storage_closed = bool(random.getrandbits(1))

BIN
images/storage_open.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB