Compare commits

..

No commits in common. "e60d18d3f653a1f0fb791ec098cd347e81db232f" and "3a8a7fdb14faf6574773872636e191e754afd772" have entirely different histories.

2 changed files with 27 additions and 43 deletions

View File

@ -6,36 +6,22 @@ class Node:
class Search:
def __init__(self, cell_size, cell_number):
def __init__(self, cell_size):
self.cell_size = cell_size
self.cell_number = cell_number
def succ(self, state):
# WARNING! IT EXCEEDS THE PLANE!!!
def succ(self, state): # successor function
x = state[0]
y = state[1]
angle = state[2]
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]]
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]]
def graphsearch(self, istate, goaltest):
x = istate[0]
@ -69,6 +55,8 @@ 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])
@ -76,3 +64,7 @@ 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,11 +1,12 @@
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:
@ -65,14 +66,12 @@ class Game:
# print(self.potato.get_soil_info().get_irrigation())
running = True
clock = pygame.time.Clock()
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)
# last_time = datetime.now()
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]:
@ -93,15 +92,8 @@ 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
@ -113,16 +105,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()