updated version of the game. new aim to the plant. new algorithm that looking for the closest plant, new models for the plant-block and the field-block etc

This commit is contained in:
Aliaksei Brown 2023-06-15 19:33:40 +02:00
parent edf1e43d33
commit 294c0fbf73
3036 changed files with 262 additions and 544 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -1,10 +0,0 @@
# si23traktor
Projekt zaliczeniowy przedmiotu Sztuczna Inteligencja w semestrze letnim 2023.
Skład zespołu:
- Jakub Chmielecki
- Mikołaj Mazur
- Szymon Szczubkowski
- Tobiasz Przybylski
- Aliaksei Shauchenka

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,85 +0,0 @@
import pygame
import random
from pygame.math import Vector2
import soil
class Blocks:
def __init__(self, parent_screen,cell_size):
self.parent_screen = parent_screen
self.flower_image = pygame.image.load(r'resources/flower.png').convert_alpha()
self.flower_image = pygame.transform.scale(self.flower_image, (cell_size, cell_size))
self.stone_image = pygame.image.load(r'resources/stone.png').convert_alpha()
self.stone_image = pygame.transform.scale(self.stone_image, (cell_size, cell_size))
self.leaf_image = pygame.image.load(r'resources/dead.png').convert_alpha()
self.leaf_image = pygame.transform.scale(self.leaf_image, (cell_size, cell_size))
self.alive_leaf_image = pygame.image.load(r'resources/alive.png').convert_alpha()
self.alive_leaf_image = pygame.transform.scale(self.alive_leaf_image, (cell_size, cell_size))
self.fawn_seed_image = pygame.image.load(r'resources/fawn_seed.png').convert_alpha()
self.fawn_seed_image = pygame.transform.scale(self.fawn_seed_image, (cell_size, cell_size))
self.fawn_wheat_image = pygame.image.load(r'resources/fawn_wheat.png').convert_alpha()
self.fawn_wheat_image = pygame.transform.scale(self.fawn_wheat_image, (cell_size, cell_size))
self.red_image = pygame.image.load(r'resources/redBush.png').convert_alpha()
self.red_image = pygame.transform.scale(self.red_image, (cell_size, cell_size))
self.soil = soil.Soil()
def locate_blocks(self, blocks_number, cell_number, body):
for i in range(blocks_number):
self.x = random.randint(0, cell_number-1)
self.y = random.randint(0, cell_number-1)
self.pos = [self.x,self.y]
body.append(self.pos)
#entire_block.update({self.x : 1}) # for now it may lay on each other,
#print(entire_block)
def place_blocks(self, parent_screen, cell_size, body, color): #drawing blocks
for block in body:
x = int(block[0] * cell_size)
y = int(block[1] * cell_size)
if color == 'leaf':
self.parent_screen.blit(self.leaf_image, (x, y))
if color == 'alive':
self.parent_screen.blit(self.alive_leaf_image, (x, y))
if color == 'stone':
self.parent_screen.blit(self.stone_image, (x, y))
if color == 'flower':
self.parent_screen.blit(self.flower_image, (x, y))
if color == 'fawn_seed':
self.parent_screen.blit(self.fawn_seed_image, (x, y))
if color == 'fawn_wheat':
self.parent_screen.blit(self.fawn_wheat_image, (x, y))
if color == 'red':
self.parent_screen.blit(self.red_image, (x, y))
# if color == 'potato':
# pass
def locate_soil(self, name, acidity, irrigation, blocks_number):
# for block in blocks_number:
self.soil.set_name(name)
self.soil.set_irrigation(irrigation)
self.soil.set_acidity(acidity)
def get_soil_info(self):
return self.soil
def draw_lines(self, parent_screen, cell_size): # background lines
for i in range(1, 10):
pygame.draw.line(self.parent_screen, (228, 253, 227), (cell_size * i, 0), (cell_size * i, parent_screen), 1)
pygame.draw.line(self.parent_screen, (228, 253, 227), (0, cell_size * i), (parent_screen, cell_size * i), 1)

Binary file not shown.

View File

@ -1,10 +1,10 @@
import pygame
import random
class Tractor:
def __init__(self, parent_screen, cell_size):
self.parent_screen = parent_screen
self.lastVisitedBlocks = [] # tractor stores last 3 visited blocks
class Chicken:
def __init__(self, surface, cell_size, cell_number):
self.surface = surface
self.lastVisitedBlocks = [] # Chicken stores last 3 visited blocks
self.up = pygame.image.load(r'resources/up.png').convert_alpha()
self.down = pygame.image.load(r'resources/down.png').convert_alpha()
@ -16,30 +16,26 @@ class Tractor:
self.left = pygame.transform.scale(self.left, (cell_size+2, cell_size+2))
self.right = pygame.transform.scale(self.right, (cell_size+4, cell_size+1))
self.x = cell_size*2 # to-check: start pos may be written explicit
self.y = cell_size*2
#self.pos = Vector2(self.x, self.y)
self.x = 1 # to-check: start pos may be written explicit
self.y = 1
self.angle = 180
self.direction = 'up'
self.image = self.down
self.step = 0
self.lastVisitedBlocks = [] # as tractor moves it stores last 3 coordinates
def draw(self):
self.parent_screen.blit(self.image, (self.x, self.y)) # rotate tractor
def move(self, direction, cell_size, cell_number):
self.cell_size = cell_size
self.cell_number = cell_number
def move(self, direction):
print('MOVE')
if direction == 'move':
if self.angle == 0 and self.y != 0:
self.y -= cell_size
if self.angle == 90 and self.x != (cell_number-1)*cell_size:
self.x += cell_size
if self.angle == 180 and self.y != (cell_number-1)*cell_size:
self.y += cell_size
self.y -= 1
if self.angle == 90 and self.x != (self.cell_number-1):
self.x += 1
if self.angle == 180 and self.y != (self.cell_number-1):
self.y += 1
if self.angle == 270 and self.x != 0:
self.x -= cell_size
self.x -= 1
if direction == 'right':
self.angle += 90
if self.angle == 360:
@ -62,35 +58,13 @@ class Tractor:
print(self.x, self.y)
print(self.step)
def water(self, body_before, body_after, cell_size):
self.pos = [self.x/cell_size, self.y/cell_size]
if self.pos in body_before:
body_before.remove(self.pos)
body_after.append(self.pos)
print('HERE!')
#print(body)
def put_seed(self, body, seed_body, cell_size):
#self.step = 0
self.pos = [self.x/cell_size, self.y/cell_size]
if self.pos in body:
#body.remove(self.pos)
seed_body.append(self.pos)
print('HERE IS THE SEED!')
def harvest(self, seed_body, wheat_body, cell_size):
self.pos = [self.x/cell_size, self.y/cell_size]
if self.pos in seed_body:
seed_body.remove(self.pos)
wheat_body.append(self.pos)
print('HERE IS THE WHEAT!')
def water(self, xy , plant_list):
for obj in plant_list:
if obj.xy == xy:
obj.state = 1
def walk(self):
choice = ['up', 'down', 'left', 'right']
if self.x == 450:
choice.pop(3)
if self.x == 0:
@ -99,6 +73,8 @@ class Tractor:
choice.pop(0)
if self.y == 450:
choice.pop(1)
self.direction = random.choice(choice)
self.move(self.direction)
self.move(self.direction)
def draw(self):
self.surface.blit(self.image, (self.x * self.cell_size, self.y * self.cell_size)) # rotate tractor

View File

@ -1,73 +0,0 @@
class Node:
def __init__(self, state, parent='', action=''):
self.state = state
self.parent = parent
self.action = action
class Search:
def __init__(self, cell_size, cell_number):
self.cell_size = cell_size
self.cell_number = cell_number
def succ(self, state):
x = state[0]
y = state[1]
angle = state[2]
match(angle):
case 'UP':
possible = [['left', x, y, 'LEFT'], ['right', x, y, 'RIGHT']]
if y != 0: possible.append(['move', x, y - self.cell_size, 'UP'])
return possible
case 'RIGHT':
possible = [['left', x, y, 'UP'], ['right', x, y, 'DOWN']]
if x != self.cell_size*(self.cell_number-1): possible.append(['move', x + self.cell_size, y, 'RIGHT'])
return possible
case 'DOWN':
possible = [['left', x, y, 'RIGHT'], ['right', x, y, 'LEFT']]
if y != self.cell_size*(self.cell_number-1): possible.append(['move', x, y + self.cell_size, 'DOWN'])
return possible
case 'LEFT':
possible = [['left', x, y, 'DOWN'], ['right', x, y, 'UP']]
if x != 0: possible.append(['move', x - self.cell_size, y, 'LEFT'])
return possible
def graphsearch(self, istate, goaltest):
x = istate[0]
y = istate[1]
angle = istate[2]
fringe = [Node([x, y, angle])] # queue (moves/states to check)
fringe_state = [fringe[0].state]
explored = []
while True:
if len(fringe) == 0:
return False
elem = fringe.pop(0)
fringe_state.pop(0)
# if goal_test(elem.state):
# return
# print(elem.state[0], elem.state[1], elem.state[2])
if elem.state[0] == goaltest[0] and elem.state[1] == goaltest[1]: # checks if we reached the given point
steps = []
while elem.parent:
steps.append([elem.action, elem.state[0], elem.state[1]]) # should return only elem.action in prod
elem = elem.parent
steps.reverse()
print(steps) # only for dev
return steps
explored.append(elem.state)
for (action, state_x, state_y, state_angle) in self.succ(elem.state):
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])
x.parent = elem
x.action = action
fringe.append(x)
fringe_state.append(x.state)

69
land.py
View File

@ -1,69 +0,0 @@
import pygame
import random
class Land:
def __init__(self, parent_screen, cell_size, cell_number, all_soil_body, irrigation):
self.parent_screen = parent_screen
self.cell_number = cell_number
self.cell_size = cell_size
self._irrigation = irrigation
self.all_soil_body = all_soil_body
self.grass_image = pygame.image.load(r'resources/grass.png').convert_alpha()
self.grass_image = pygame.transform.scale(self.grass_image, (cell_size, cell_size))
# self.bad_grass_image = pygame.image.load(r'resources/bad_grass.png').convert()
# self.bad_grass_image = pygame.transform.scale(self.bad_grass_image, (cell_size, cell_size))
self.black_earth_image = pygame.image.load(r'resources/grass.png').convert()
self.black_earth_image = pygame.transform.scale(self.black_earth_image, (cell_size, cell_size))
self.green_earth_image = pygame.image.load(r'resources/grass.png').convert()
self.green_earth_image = pygame.transform.scale(self.green_earth_image, (cell_size, cell_size))
self.fawn_soil_image = pygame.image.load(r'resources/fawn_soil.png').convert()
self.fawn_soil_image = pygame.transform.scale(self.fawn_soil_image, (cell_size, cell_size))
self.fen_soil_image = pygame.image.load(r'resources/grass.png').convert()
self.fen_soil_image = pygame.transform.scale(self.fen_soil_image, (cell_size, cell_size))
def locate_soil(self, soil_body): # finds free places(coordinates) for soil and adds them to soil_body[]
number_of_blocs_for_each_soil = 50
if number_of_blocs_for_each_soil > (self.cell_number * self.cell_number) // 4:
number_of_blocs_for_each_soil = (self.cell_number * self.cell_number) // 4
print('Number of soil blocks exceeds the number of fields!')
for i in range(number_of_blocs_for_each_soil): # can't be more than: (cell_number * cell_number) // soil_types
while True:
rand_x = random.randint(0, self.cell_number - 1) # to-check
rand_y = random.randint(0, self.cell_number - 1)
if [rand_x, rand_y] not in self.all_soil_body:
self.all_soil_body.append([rand_x, rand_y])
soil_body.append([rand_x, rand_y])
break
def place_soil(self, soil_body, soil_name):
for body in soil_body:
x = int(body[0] * self.cell_size)
y = int(body[1] * self.cell_size)
if soil_name == 'black_earth':
self.parent_screen.blit(self.black_earth_image, (x, y))
if soil_name == 'green_earth':
self.parent_screen.blit(self.green_earth_image, (x, y))
if soil_name == 'fawn_soil':
self.parent_screen.blit(self.fawn_soil_image, (x, y))
if soil_name == 'fen_soil':
self.parent_screen.blit(self.fen_soil_image, (x, y))
def set_and_place_block_of_grass(self, name):
for i in range(0, self.cell_number):
for k in range(0, self.cell_number):
if [k, i] not in self.all_soil_body:
x = int(k * self.cell_size)
y = int(i * self.cell_size)
if name == 'good':
self.parent_screen.blit(self.grass_image, (x, y))
if name == 'bad':
self.parent_screen.blit(self.bad_grass_image, (x, y))

View File

@ -1,51 +0,0 @@
from collections import Counter
def tree_learn(examples, attributes, default_class):
if len(examples) == 0:
return default_class
if all(examples[0][-1] == example[-1] for example in examples):
return examples[0][-1]
if len(attributes) == 0:
class_counts = Counter(example[-1] for example in examples)
majority_class = class_counts.most_common(1)[0][0]
return majority_class
# Choose the attribute A as the root of the decision tree
A = select_attribute(attributes, examples)
tree = {A: {}}
new_attributes = [attr for attr in attributes if attr != A]
new_default_class = Counter(example[-1] for example in examples).most_common(1)[0][0]
for value in get_attribute_values(A):
new_examples = [example for example in examples if example[attributes.index(A)] == value]
subtree = tree_learn(new_examples, new_attributes, new_default_class)
tree[A][value] = subtree
return tree
# Helper function: Select the best attribute based on a certain criterion (e.g., information gain)
def select_attribute(attributes, examples):
# Implement your attribute selection criterion here
pass
# Helper function: Get the possible values of an attribute from the examples
def get_attribute_values(attribute):
# Implement your code to retrieve the attribute values from the examples here
pass
# Example usage with coordinates
examples = [
[1, 2, 'A'],
[3, 4, 'A'],
[5, 6, 'B'],
[7, 8, 'B']
]
attributes = ['x', 'y']
default_class = 'unknown'
decision_tree = tree_learn(examples, attributes, default_class)
print(decision_tree)

239
main.py
View File

@ -1,160 +1,64 @@
import os
import pygame
import random
import land
import tractor
import blocks
import astar_search
import neural_network.inference
import chicken.chicken as chick
from pygame.locals import *
examples = [
['piasek', 'sucha', 'jalowa', 'żółty'],
['czarnoziem', 'wilgotna', 'bogata', 'brazowa'],
['rzedzina', 'wilgotna', 'bogata', 'zielona'],
['gleby murszowe', 'wilgotna', 'bogata', 'szara'],
['pustynne gleby', 'sucha', 'jalowa', 'pomarańczowa'],
['torfowiska', 'sucha', 'jalowa', 'czerwona']
]
attributes = ['typ_gleby', 'wilgotność', 'zawartość_składników', 'kolor']
# Tworzenie obiektu TreeLearn i nauka drzewa decyzyjnego
# tree_learner = TreeLearn()
# default_class = 'nieznane'
# tree_learner.train(examples, attributes, default_class)
class TreeLearn:
def __init__(self):
self.tree = None
def train(self, examples, attributes, default_class):
self.tree = self.build_tree(examples, attributes, default_class)
def build_tree(self, examples, attributes, default_class):
if not examples:
return Node(default_class)
if self.all_same_class(examples):
return Node(examples[0][-1])
if not attributes:
class_counts = self.get_class_counts(examples)
default_class = max(class_counts, key=class_counts.get)
return Node(default_class)
best_attribute = self.choose_attribute(examples, attributes)
root = Node(best_attribute)
attribute_values = self.get_attribute_values(examples, best_attribute)
for value in attribute_values:
new_examples = self.filter_examples(examples, best_attribute, value)
new_attributes = attributes[:]
new_attributes.remove(best_attribute)
new_default_class = max(self.get_class_counts(new_examples), key=lambda k: class_counts.get(k, 0))
subtree = self.build_tree(new_examples, new_attributes, new_default_class)
root.add_child(value, subtree)
return root
def all_same_class(self, examples):
return len(set([example[-1] for example in examples])) == 1
def get_class_counts(self, examples):
class_counts = {}
for example in examples:
class_label = example[-1]
class_counts[class_label] = class_counts.get(class_label, 0) + 1
return class_counts
def choose_attribute(self, examples, attributes):
# Placeholder for attribute selection logic
return attributes[0]
def get_attribute_values(self, examples, attribute):
return list(set([example[attribute] for example in examples]))
def filter_examples(self, examples, attribute, value):
return [example for example in examples if example[attribute] == value]
from models import plant
from methods import field_settings
import neural_network.inference
import models.plant as plant
import methods.plants_settings as plants_settings
import methods.graph_search as graph_search
class Node:
def __init__(self, label):
self.label = label
self.children = {}
def add_child(self, value, child):
self.children[value] = child
#import models.field_block as field_block
class Game:
cell_size = 50
cell_number = 15 # horizontally
blocks_number = 20
dry_grass_number = 50
wet_grass_number = (cell_number*cell_number) - dry_grass_number
def __init__(self):
self.dead_leaf_body = []
self.green_leaf_body = []
self.stone_body = []
self.flower_body = []
self.dead_grass_body = []
self.grass_body = []
self.red_block = [] #aim block
self.fawn_seed_body = []
self.fawn_wheat_body = []
self.black_earth_body = []
self.green_earth_body = []
self.fawn_soil_body = []
self.fen_soil_body = []
self.allBodyPos = []
self.entire_block = {}
# initialize a window
pygame.init()
self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number))
# finds places for every type soil and grass
self.black_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.black_earth.locate_soil(self.black_earth_body)
self.green_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.green_earth.locate_soil(self.green_earth_body)
self.fawn_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.fawn_soil.locate_soil(self.fawn_soil_body)
self.fen_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.fen_soil.locate_soil(self.fen_soil_body)
self.grass = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.grass_list = []
self.plant_list = []
self.stone_list = []
self.aim_list = []
self.blocks = blocks.Blocks(self.surface, self.cell_size)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.dead_leaf_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.stone_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.flower_body)
self.Field = field_settings.FieldSettings(self.surface, self.cell_size, self.cell_number)
self.Field.locate_field(self.grass_list, 0, self.wet_grass_number) # wet grass
self.Field.locate_field(self.grass_list, 1, self.dry_grass_number) # dry grass
#self.blocks.locate_blocks(1, self.cell_number, self.red_block)
self.Plants = plants_settings.PlantsSettings(self.surface, self.cell_size, self.cell_number)
self.Plants.locate_plant(self.plant_list, 'wheat', self.blocks_number)
self.Plants.locate_plant(self.plant_list, 'flower', self.blocks_number)
self.Plants.locate_plant(self.plant_list, 'bush', self.blocks_number)
# self.potato = blocks.Blocks(self.surface, self.cell_size)
# self.potato.locate_soil('black earth', 6, 1, [])
#self.Plants.locate_plant(self.plant_list, 'aim', 1)
self.tractor = tractor.Tractor(self.surface, self.cell_size)
self.tractor.draw()
self.Plants.locate_aim(self.aim_list, 0, 0)
self.Plants.locate_plant(self.stone_list, 'stone', self.blocks_number)
#self.image_wheat = self.Plants.wheat_watered()
self.chicken = chick.Chicken(self.surface, self.cell_size, self.cell_number)
self.chicken.draw()
def run(self):
# print(self.potato.get_soil_info().get_name())
# print(self.potato.get_soil_info().get_acidity())
# print(self.potato.get_soil_info().get_irrigation())
running = True
clock = pygame.time.Clock()
move_chicken_event = pygame.USEREVENT + 1
pygame.time.set_timer(move_chicken_event, 500) # chicken moves every 1000 ms
self.search_object = graph_search.Search(self.cell_size, self.cell_number)
chicken_next_moves = []
move_tractor_event = pygame.USEREVENT + 1
pygame.time.set_timer(move_tractor_event, 500) # tractor moves every 1000 ms
tractor_next_moves = []
astar_search_object = astar_search.Search(self.cell_size, self.cell_number)
veggies = dict()
veggies_debug = dict()
@ -165,35 +69,29 @@ class Game:
if event.type == KEYDOWN:
if pygame.key.get_pressed()[K_ESCAPE]:
running = False
# in case we want to use keyboard
if pygame.key.get_pressed()[K_UP]:
# self.tractor.move('up', self.cell_size, self.cell_number)
self.tractor.move('move', self.cell_size, self.cell_number)
# self.chicken.move('up', self.cell_size, self.cell_number)
self.chicken.move('move', self.cell_size, self.cell_number)
# if pygame.key.get_pressed()[K_DOWN]:
# self.tractor.move('down', self.cell_size, self.cell_number)
# self.chicken.move('down', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_LEFT]:
self.tractor.move('left', self.cell_size, self.cell_number)
self.chicken.move('left', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_RIGHT]:
self.tractor.move('right', self.cell_size, self.cell_number)
self.chicken.move('right', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_SPACE]:
self.tractor.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size)
# self.tractor.water(self.grass_body, self.dead_grass_body, self.cell_size)
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)
if self.red_block:
self.red_block.pop()
self.red_block.append([random_x/50, random_y/50])
# below line should be later moved into tractor.py
self.chicken.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size)
if event.type == move_chicken_event:
if len(chicken_next_moves) == 0:
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
#bandaid to know about stones
tractor_next_moves = astar_search_object.astarsearch(
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y], self.stone_body, self.flower_body)
closest_wheat = self.search_object.closest_point(self.chicken.x, self.chicken.y, 'wheat', self.plant_list)
self.aim_list[0].xy[0] = closest_wheat[0]
self.aim_list[0].xy[1] = closest_wheat[1]
chicken_next_moves = self.search_object.astarsearch(
[self.chicken.x, self.chicken.y, angles[self.chicken.angle]], [closest_wheat[0], closest_wheat[1]], self.stone_list, self.plant_list)
#neural_network
current_veggie = next(os.walk('./neural_network/images/test'))[1][random.randint(0, len(next(os.walk('./neural_network/images/test'))[1])-1)]
if(current_veggie in veggies_debug):
veggies_debug[current_veggie]+=1
@ -207,34 +105,23 @@ class Game:
else:
veggies[predicted_veggie] = 1
print("Debug veggies: ", veggies_debug, "Predicted veggies: ", veggies)
else:
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
self.chicken.move(chicken_next_moves.pop(0)[0])
if len(chicken_next_moves) == 0:
self.chicken.water([self.aim_list[0].xy[0], self.aim_list[0].xy[1]], self.plant_list)
print(self.chicken.x, self.chicken.y)
elif event.type == QUIT:
running = False
self.surface.fill((123, 56, 51)) # background color
self.grass.set_and_place_block_of_grass('good')
self.black_earth.place_soil(self.black_earth_body, 'black_earth')
self.green_earth.place_soil(self.green_earth_body, 'green_earth')
self.fawn_soil.place_soil(self.fawn_soil_body, 'fawn_soil')
self.fen_soil.place_soil(self.fen_soil_body, 'fen_soil')
self.Field.draw_grass(self.grass_list)
self.Plants.draw_plant(self.plant_list)
self.Plants.draw_plant(self.stone_list)
self.Plants.draw_aim(self.aim_list)
# 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')
self.blocks.place_blocks(self.surface, self.cell_size, self.red_block, 'red')
# seeds
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_seed_body, 'fawn_seed')
# wheat
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_wheat_body, 'fawn_wheat')
self.tractor.draw()
self.chicken.draw()
pygame.display.update()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

41
methods/field_settings.py Normal file
View File

@ -0,0 +1,41 @@
import pygame
import random
import models.field_block as field_block
class FieldSettings:
def __init__(self, surface, cell_size, cell_number):
self.wet_grass_image = pygame.image.load(r'resources/grass.png').convert_alpha()
self.wet_grass_image = pygame.transform.scale(self.wet_grass_image, (cell_size, cell_size))
self.dry_grass_image = pygame.image.load(r'resources/dry_grass.png').convert()
self.dry_grass_image = pygame.transform.scale(self.dry_grass_image, (cell_size, cell_size))
self.cell_number = cell_number
self.cell_size = cell_size
self.surface = surface
self.all_blocks = []
def locate_field(self, field_list, state, num_of_blocks): # finds open space (coordinates)
for i in range(num_of_blocks):
while True:
rand_x = random.randint(0, self.cell_number - 1) # to-check
rand_y = random.randint(0, self.cell_number - 1)
if [rand_x, rand_y] not in self.all_blocks:
self.all_blocks.append([rand_x, rand_y])
if state == 0:
self.block = field_block.FieldBlock(
i, 'wet', 0, self.wet_grass_image, rand_x, rand_y
)
if state == 1:
self.block = field_block.FieldBlock(
i, 'dry', 1, self.dry_grass_image, rand_x, rand_y
)
field_list.append(self.block)
break
def draw_grass(self, field_list):
for obj in field_list:
self.surface.blit(obj.image, (int(obj.xy[0] * self.cell_size), int(obj.xy[1] * self.cell_size)))

View File

@ -17,19 +17,19 @@ class Search:
match(angle):
case 'UP':
possible = [['left', x, y, 'LEFT'], ['right', x, y, 'RIGHT']]
if y != 0: possible.append(['move', x, y - self.cell_size, 'UP'])
if y != 0: possible.append(['move', x, y - 1, 'UP'])
return possible
case 'RIGHT':
possible = [['left', x, y, 'UP'], ['right', x, y, 'DOWN']]
if x != self.cell_size*(self.cell_number-1): possible.append(['move', x + self.cell_size, y, 'RIGHT'])
if x != (self.cell_number-1): possible.append(['move', x + 1, y, 'RIGHT'])
return possible
case 'DOWN':
possible = [['left', x, y, 'RIGHT'], ['right', x, y, 'LEFT']]
if y != self.cell_size*(self.cell_number-1): possible.append(['move', x, y + self.cell_size, 'DOWN'])
if y != (self.cell_number-1): possible.append(['move', x, y + 1, 'DOWN'])
return possible
case 'LEFT':
possible = [['left', x, y, 'DOWN'], ['right', x, y, 'UP']]
if x != 0: possible.append(['move', x - self.cell_size, y, 'LEFT'])
if x != 0: possible.append(['move', x - 1, y, 'LEFT'])
return possible
def cost(self, node, stones, goal, flowers):
@ -48,7 +48,7 @@ class Search:
return abs(node.state[0] - goal[0]) + abs(node.state[1] - goal[1])
#bandaid to know about stones
def astarsearch(self, istate, goaltest, cStones, cFlowers):
def astarsearch(self, istate, goaltest, stone_list, plant_list):
#to be expanded
def cost_old(x, y):
@ -56,14 +56,21 @@ class Search:
return 10
else:
return 1
x = istate[0]
y = istate[1]
angle = istate[2]
stones = []
flowers = []
stones = [(x*50, y*50) for (x, y) in cStones]
flowers = [(x*50, y*50) for (x, y) in cFlowers]
for obj in stone_list:
stones.append((obj.xy[0]*50, obj.xy[1]*50))
for obj in plant_list:
if obj.name == 'flower':
flowers.append((obj.xy[0]*50, obj.xy[1]*50))
# stones = [(x*50, y*50) for (x, y) in stone_list]
# flowers = [(x*50, y*50) for (x, y) in plant_list]
print(stones)
@ -113,3 +120,17 @@ class Search:
if fringe[i][0].state == x.state:
if fringe[i][1] > priority:
fringe[i] = (x, priority)
def closest_point(self, x, y, name, plant_list):
self.max_distance = self.cell_number*self.cell_number
for obj in plant_list:
if obj.name == name:
if obj.state == 0:
self.distance = (abs(obj.xy[0] - x) + abs(obj.xy[1] - y))
if self.distance <= self.max_distance:
self.max_distance = self.distance
x_close = obj.xy[0]
y_close = obj.xy[1]
#print("distance: ",self.distance, obj.xy[0], "+", obj.xy[1], "-" ,x, "+",y)
return (x_close, y_close)

View File

@ -0,0 +1,86 @@
import pygame
import random
import models.field_block as field_block
import models.plant as plant
class PlantsSettings:
def __init__(self, surface, cell_size, cell_number):
self.cell_number = cell_number
self.cell_size = cell_size
self.surface = surface
self.all_blocks = []
self.flower_image = pygame.image.load(r'resources/flower.png').convert_alpha()
self.flower_image = pygame.transform.scale(self.flower_image, (self.cell_size, self.cell_size))
self.stone_image = pygame.image.load(r'resources/stone.png').convert_alpha()
self.stone_image = pygame.transform.scale(self.stone_image, (self.cell_size, self.cell_size))
self.bush_image = pygame.image.load(r'resources/bush.png').convert_alpha()
self.bush_image = pygame.transform.scale(self.bush_image, (self.cell_size, self.cell_size))
self.ivy_image = pygame.image.load(r'resources/ivy.png').convert_alpha()
self.ivy_image = pygame.transform.scale(self.ivy_image, (self.cell_size, self.cell_size))
self.wheat_dead_image = pygame.image.load(r'resources/wheat_dead.png').convert_alpha()
self.wheat_dead_image = pygame.transform.scale(self.wheat_dead_image, (self.cell_size, self.cell_size))
self.wheat_image = pygame.image.load(r'resources/wheat.png').convert_alpha()
self.wheat_image = pygame.transform.scale(self.wheat_image, (self.cell_size, self.cell_size))
self.aim_image = pygame.image.load(r'resources/aim.png').convert_alpha()
self.aim_image = pygame.transform.scale(self.aim_image, (self.cell_size, self.cell_size))
def locate_plant(self, field_list, name, num_of_blocks): # finds open space (coordinates)
for i in range(num_of_blocks):
while True:
rand_x = random.randint(0, self.cell_number - 1) # to check
rand_y = random.randint(0, self.cell_number - 1)
if [rand_x, rand_y] not in self.all_blocks:
self.all_blocks.append([rand_x, rand_y])
if name == 'wheat':
self.block = plant.Plant(
i, name, 0, self.wheat_dead_image, self.wheat_image, rand_x, rand_y, False
)
if name == 'ivy':
self.block = plant.Plant(
i, name, 1, self.ivy_image, self.ivy_image, rand_x, rand_y, False
)
if name == 'flower':
self.block = plant.Plant(
i, name, 1, self.flower_image, self.flower_image, rand_x, rand_y, False
)
if name == 'stone':
self.block = plant.Plant(
i, name, 1, self.stone_image, self.stone_image, rand_x, rand_y, False
)
if name == 'buch':
self.block = plant.Plant(
i, name, 1, self.bush_image, self.bush_image, rand_x, rand_y, False
)
if name == 'aim':
self.block = plant.Plant(
999, name, 1, self.aim_image, self.aim_image, rand_x, rand_y, False
)
field_list.append(self.block)
break
def locate_aim(self, field_list, x, y):
self.block = plant.Plant(
999, 'aim', 1, self.aim_image, self.aim_image, x, y, False
)
field_list.append(self.block)
def draw_plant(self, field_list):
for obj in field_list:
if obj.state == 0:
self.surface.blit(obj.image_state_zero, (int(obj.xy[0] * self.cell_size), int(obj.xy[1] * self.cell_size)))
if obj.state == 1:
self.surface.blit(obj.image_state_one, (int(obj.xy[0] * self.cell_size), int(obj.xy[1] * self.cell_size)))
def draw_aim(self, aim_list):
x = int(aim_list[0].xy[0] * self.cell_size)
y = int(aim_list[0].xy[1] * self.cell_size)
self.surface.blit(self.aim_image, (x, y)) # rotate tractor

Binary file not shown.

Binary file not shown.

8
models/field_block.py Normal file
View File

@ -0,0 +1,8 @@
class FieldBlock:
def __init__(self, id, name, state, image, x, y):
self.id = id
self.name = name
self.state = state
self.image = image
self.xy = [x, y]

10
models/plant.py Normal file
View File

@ -0,0 +1,10 @@
class Plant:
def __init__(self, id, name, state, image_zero, image_one, x, y, empty):
self.id = id
self.name = name
self.state = state
self.image_state_zero = image_zero
self.image_state_one = image_one
self.xy = [x, y]
self.empty = empty

BIN
neural_network/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
neural_network/images/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Some files were not shown because too many files have changed in this diff Show More