WIP succ_limitfunction #2

Merged
s473558 merged 5 commits from succ_limitfunction into master 2023-04-17 16:25:13 +02:00
2 changed files with 43 additions and 27 deletions

View File

@ -6,22 +6,36 @@ class Node:
class Search:
def __init__(self, cell_size):
def __init__(self, cell_size, cell_number):
self.cell_size = cell_size
self.cell_number = cell_number
# WARNING! IT EXCEEDS THE PLANE!!!
def succ(self, state): # successor function
def succ(self, state):
x = state[0]
y = state[1]
angle = state[2]
if angle == 0:
return [['move', x, y - self.cell_size, 0], ['left', x, y, 270], ['right', x, y, 90]]
if angle == 90:
return [['move', x + self.cell_size, y, 90], ['left', x, y, 0], ['right', x, y, 180]]
if angle == 180:
return [['move', x, y + self.cell_size, 180], ['left', x, y, 90], ['right', x, y, 270]]
if angle == 270:
return [['move', x - self.cell_size, y, 270], ['left', x, y, 180], ['right', x, y, 0]]
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
match(angles[angle]):
case 'UP':
if y != 0:
return [['move', x, y - self.cell_size, 0], ['left', x, y, 270], ['right', x, y, 90]]
else:
return [['left', x, y, 270], ['right', x, y, 90]]
case 'RIGHT':
if x != self.cell_size*(self.cell_number-1):
return [['move', x + self.cell_size, y, 90], ['left', x, y, 0], ['right', x, y, 180]]
else:
return [['left', x, y, 0], ['right', x, y, 180]]
case 'DOWN':
if y != self.cell_size*(self.cell_number-1):
return [['move', x, y + self.cell_size, 180], ['left', x, y, 90], ['right', x, y, 270]]
else:
return [['left', x, y, 90], ['right', x, y, 270]]
case 'LEFT':
if x != 0:
return [['move', x - self.cell_size, y, 270], ['left', x, y, 180], ['right', x, y, 0]]
else:
return [['left', x, y, 180], ['right', x, y, 0]]
def graphsearch(self, istate, goaltest):
x = istate[0]
@ -55,8 +69,6 @@ class Search:
explored.append(elem.state)
for (action, state_x, state_y, state_angle) in self.succ(elem.state):
if state_x < 0 or state_y < 0: # check if any of the values are negative
continue
if [state_x, state_y, state_angle] not in fringe_state and \
[state_x, state_y, state_angle] not in explored:
x = Node([state_x, state_y, state_angle])
@ -64,7 +76,3 @@ class Search:
x.action = action
fringe.append(x)
fringe_state.append(x.state)
se = Search(50)
se.graphsearch(istate=[50, 50, 0], goaltest=[150, 250])

28
main.py
View File

@ -1,12 +1,11 @@
import os
import pygame
import random
import land
import tractor
import blocks
import graph_search
from pygame.locals import *
from datetime import datetime
class Game:
@ -66,12 +65,14 @@ class Game:
# print(self.potato.get_soil_info().get_irrigation())
running = True
clock = pygame.time.Clock()
# last_time = datetime.now()
move_tractor_event = pygame.USEREVENT + 1
pygame.time.set_timer(move_tractor_event, 1000) # tractor moves every 1000 ms
tractor_next_moves = []
graph_search_object = graph_search.Search(self.cell_size, self.cell_number)
while running:
clock.tick(60) # manual fps control not to overwork the computer
# time_now = datetime.now()
for event in pygame.event.get():
if event.type == KEYDOWN:
if pygame.key.get_pressed()[K_ESCAPE]:
@ -92,8 +93,15 @@ class Game:
if pygame.key.get_pressed()[K_q]:
self.tractor.harvest(self.fawn_seed_body, self.fawn_wheat_body, self.cell_size)
self.tractor.put_seed(self.fawn_soil_body, self.fawn_seed_body, self.cell_size)
if event.type == move_tractor_event:
if len(tractor_next_moves) == 0:
random_x = random.randrange(0, self.cell_number * self.cell_size, 50)
random_y = random.randrange(0, self.cell_number * self.cell_size, 50)
print("Generated target: ",random_x, random_y)
tractor_next_moves = graph_search_object.graphsearch(
[self.tractor.x, self.tractor.y, self.tractor.angle], [random_x, random_y])
else:
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
elif event.type == QUIT:
running = False
@ -105,16 +113,16 @@ class Game:
self.fawn_soil.place_soil(self.fawn_soil_body, 'fawn_soil')
self.fen_soil.place_soil(self.fen_soil_body, 'fen_soil')
#plants examples
# plants examples
self.blocks.place_blocks(self.surface, self.cell_size, self.dead_leaf_body, 'leaf')
self.blocks.place_blocks(self.surface, self.cell_size, self.green_leaf_body, 'alive')
self.blocks.place_blocks(self.surface, self.cell_size, self.stone_body, 'stone')
self.blocks.place_blocks(self.surface, self.cell_size, self.flower_body, 'flower')
#seeds
# seeds
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_seed_body, 'fawn_seed')
#wheat
# wheat
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_wheat_body, 'fawn_wheat')
self.tractor.draw()