master #1
120
basic_grid.py
@ -2,23 +2,23 @@
|
||||
import pygame
|
||||
|
||||
# Import pygame.locals for easier access to key coordinates
|
||||
# Updated to conform to flake8 and black standards
|
||||
from pygame.locals import (
|
||||
K_UP,
|
||||
K_DOWN,
|
||||
K_LEFT,
|
||||
K_RIGHT,
|
||||
K_ESCAPE,
|
||||
K_SPACE,
|
||||
KEYDOWN,
|
||||
QUIT
|
||||
)
|
||||
|
||||
from field import *
|
||||
from tractor import *
|
||||
from plant import *
|
||||
from colors import *
|
||||
from dimensions import *
|
||||
# Import other files from project
|
||||
import field as F
|
||||
import tractor as T
|
||||
import plant as P
|
||||
import colors as C
|
||||
import dimensions as D
|
||||
import node as N
|
||||
import mapschema as maps
|
||||
|
||||
# Initialize pygame
|
||||
pygame.init()
|
||||
@ -28,32 +28,51 @@ pygame.display.set_caption("Inteligentny Traktor")
|
||||
|
||||
# Create the screen object
|
||||
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT))
|
||||
|
||||
# Define the map of the field
|
||||
mapschema = maps.createField()
|
||||
|
||||
# Create field array
|
||||
field = []
|
||||
|
||||
for row in range(GSIZE):
|
||||
# Populate the field array
|
||||
for row in range(D.GSIZE):
|
||||
field.append([])
|
||||
for column in range(GSIZE):
|
||||
fieldbit = Field(row, column)
|
||||
for column in range(D.GSIZE):
|
||||
fieldbit = F.Field(row, column, mapschema[column][row])
|
||||
field[row].append(fieldbit)
|
||||
|
||||
tractor = Tractor(field[0][0])
|
||||
# Create Tractor object
|
||||
tractor = T.Tractor(field, [0,0])
|
||||
|
||||
plant = Plant(field[1][1], "wheat")
|
||||
# Define the map of plants
|
||||
mapschema = maps.createPlants()
|
||||
|
||||
# Createt plants array
|
||||
plants = []
|
||||
|
||||
print(tractor.rect)
|
||||
print(field[0][0].rect)
|
||||
# Populate the plants array
|
||||
for row in range(D.GSIZE):
|
||||
plants.append([])
|
||||
for column in range(D.GSIZE):
|
||||
if mapschema[column][row] != 0:
|
||||
plantbit = P.Plant(field[row][column], mapschema[column][row])
|
||||
plants[row].append(plantbit)
|
||||
|
||||
# Create list for tractor instructions
|
||||
path = []
|
||||
|
||||
# Variable to keep the main loop running
|
||||
RUNNING = True
|
||||
|
||||
ticker = 0
|
||||
# Variable conroling timed eventes
|
||||
TICKER = 0
|
||||
|
||||
# Initialize clock
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
|
||||
# Main loop
|
||||
while RUNNING:
|
||||
|
||||
@ -68,37 +87,70 @@ while RUNNING:
|
||||
elif event.type == QUIT:
|
||||
RUNNING = False
|
||||
|
||||
# Get all keys pressed at a time
|
||||
# Create key Node that will be used to calculate tractor instructions
|
||||
processor = N.Node(field, tractor.position, tractor.direction)
|
||||
|
||||
# If path is empty or nonexistent, create new one
|
||||
if path is None or len(path) == 0:
|
||||
path = processor.findPathToPlant()
|
||||
|
||||
# control tractor by poping instructions from path list
|
||||
if path is not None:
|
||||
if path[0] == "move":
|
||||
tractor.move()
|
||||
path.pop(0)
|
||||
elif path[0] =="left":
|
||||
tractor.rotate_left()
|
||||
path.pop(0)
|
||||
elif path[0] == "right":
|
||||
tractor.rotate_right()
|
||||
path.pop(0)
|
||||
elif path[0] == "hydrate":
|
||||
tractor.hydrate(field)
|
||||
path.pop(0)
|
||||
else:
|
||||
path.pop(0)
|
||||
|
||||
# Get all keys pressed at a time CURRENTLY UNUSED
|
||||
pressed_keys = pygame.key.get_pressed()
|
||||
|
||||
# Update the state of tractor
|
||||
tractor.update(pressed_keys)
|
||||
tractor.hydrate(field, pressed_keys)
|
||||
# control tractor with pressed keys CURRENTLY UNUSED
|
||||
if pressed_keys[K_UP]:
|
||||
tractor.move()
|
||||
elif pressed_keys[K_LEFT]:
|
||||
tractor.rotate_left()
|
||||
elif pressed_keys[K_RIGHT]:
|
||||
tractor.rotate_right()
|
||||
|
||||
# Set the screen background
|
||||
screen.fill(DBROWN)
|
||||
screen.fill(C.DBROWN)
|
||||
|
||||
# Draw the field
|
||||
for row in range(GSIZE):
|
||||
for column in range(GSIZE):
|
||||
for row in range(D.GSIZE):
|
||||
for column in range(D.GSIZE):
|
||||
screen.blit(field[row][column].surf, field[row][column].rect)
|
||||
|
||||
# Draw the tactor
|
||||
screen.blit(tractor.surf, tractor.rect)
|
||||
|
||||
# Draw the player on the screen
|
||||
screen.blit(plant.surf, plant.rect)
|
||||
# Plants grow with every 10th tick, then they are drawn
|
||||
for row in plants:
|
||||
for plant in row:
|
||||
plant.tick()
|
||||
plant.grow()
|
||||
screen.blit(plant.surf, plant.rect)
|
||||
|
||||
if ticker == 0:
|
||||
for row in range(GSIZE):
|
||||
for column in range(GSIZE):
|
||||
# Field are drying with every 100th tick
|
||||
if TICKER == 0:
|
||||
for row in range(D.GSIZE):
|
||||
for column in range(D.GSIZE):
|
||||
field[row][column].dehydrate()
|
||||
|
||||
plant.grow()
|
||||
|
||||
ticker = (ticker + 1)%4
|
||||
# Increment ticker
|
||||
TICKER = (TICKER + 1)%100
|
||||
|
||||
# Update the screen
|
||||
pygame.display.flip()
|
||||
|
||||
# Ensure program maintains a rate of 15 frames per second
|
||||
clock.tick(4)
|
||||
# Ensure program maintains a stable framerate
|
||||
clock.tick(8)
|
||||
|
259
cases.py
Normal file
@ -0,0 +1,259 @@
|
||||
class Case:
|
||||
def __init__(self, values, attributes, Class):
|
||||
self.values = values
|
||||
self.attributes = attributes
|
||||
self.Class = Class
|
||||
|
||||
attributes = ["field.hydration", "field.fertility", "species", "ticks", "is_healthy", "field.tractor_there"]
|
||||
|
||||
cases = [Case([4, 0, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([4, 1, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([1, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([2, 1, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([2, 0, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([1, 1, "strawberry", 1, 0, 1], attributes, -1),
|
||||
Case([1, 0, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([2, 0, "wheat", 1, 0, 1], attributes, 0),
|
||||
Case([1, 1, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([3, 1, "potato", 1, 1, 0], attributes, 1),
|
||||
Case([2, 1, "strawberry", 1, 0, 0], attributes, 0),
|
||||
Case([4, 0, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([4, 1, "wheat", 1, 0, 1], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
|
||||
Case([1, 1, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([0, 0, "sorrel", 0, 0, 1], attributes, 0),
|
||||
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([0, 1, "sorrel", 0, 1, 1], attributes, 0),
|
||||
Case([0, 1, "strawberry", 1, 1, 0], attributes, -1),
|
||||
Case([2, 0, "sorrel", 0, 1, 1], attributes, 0),
|
||||
Case([4, 0, "wheat", 1, 0, 1], attributes, 0),
|
||||
Case([5, 0, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([0, 0, "strawberry", 1, 0, 0], attributes, -1),
|
||||
Case([4, 0, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([3, 0, "sorrel", 0, 0, 1], attributes, 0),
|
||||
Case([3, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([4, 1, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([1, 1, "wheat", 0, 1, 1], attributes, 0),
|
||||
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
|
||||
Case([2, 0, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([4, 1, "strawberry", 0, 0, 1], attributes, 0),
|
||||
Case([1, 0, "potato", 1, 1, 0], attributes, 0),
|
||||
Case([4, 1, "sorrel", 1, 1, 1], attributes, 0),
|
||||
Case([0, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
|
||||
Case([4, 0, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([5, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([3, 1, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([4, 0, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([5, 1, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([3, 0, "sorrel", 0, 0, 1], attributes, 0),
|
||||
Case([0, 0, "potato", 1, 1, 1], attributes, -1),
|
||||
Case([4, 0, "strawberry", 1, 1, 1], attributes, 0),
|
||||
Case([2, 1, "strawberry", 1, 0, 1], attributes, 0),
|
||||
Case([2, 1, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([2, 1, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([1, 0, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
|
||||
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([0, 0, "strawberry", 1, 0, 1], attributes, -1),
|
||||
Case([0, 1, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([1, 1, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([0, 0, "sorrel", 1, 1, 0], attributes, -1),
|
||||
Case([2, 0, "sorrel", 0, 0, 1], attributes, 0),
|
||||
Case([5, 1, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([2, 0, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([2, 1, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([3, 0, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([5, 0, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
|
||||
Case([0, 0, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([5, 0, "potato", 1, 1, 0], attributes, 1),
|
||||
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([3, 1, "sorrel", 0, 1, 0], attributes, 0),
|
||||
Case([2, 1, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([5, 0, "strawberry", 1, 1, 0], attributes, 1),
|
||||
Case([5, 0, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([5, 0, "wheat", 1, 1, 0], attributes, 1),
|
||||
Case([2, 0, "potato", 1, 0, 0], attributes, 0),
|
||||
Case([3, 1, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([3, 0, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([0, 1, "sorrel", 1, 1, 1], attributes, -1),
|
||||
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
|
||||
Case([2, 1, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
|
||||
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
|
||||
Case([4, 0, "strawberry", 1, 0, 1], attributes, 0),
|
||||
Case([3, 1, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([5, 1, "wheat", 1, 0, 1], attributes, 0),
|
||||
Case([5, 0, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([3, 0, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([1, 0, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([5, 0, "sorrel", 0, 1, 1], attributes, 0),
|
||||
Case([4, 0, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([0, 0, "strawberry", 0, 0, 0], attributes, 0),
|
||||
Case([5, 0, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([4, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([1, 1, "strawberry", 1, 1, 0], attributes, -1),
|
||||
Case([5, 0, "strawberry", 1, 0, 0], attributes, 0),
|
||||
Case([5, 1, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
|
||||
Case([3, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([1, 0, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
|
||||
Case([0, 0, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([1, 0, "potato", 0, 0, 0], attributes, 0),
|
||||
Case([1, 1, "sorrel", 0, 1, 0], attributes, 0),
|
||||
Case([0, 1, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([2, 0, "strawberry", 1, 1, 1], attributes, 0),
|
||||
Case([4, 1, "wheat", 1, 0, 0], attributes, 0),
|
||||
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
|
||||
Case([1, 0, "strawberry", 1, 1, 1], attributes, -1),
|
||||
Case([4, 0, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([4, 0, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([0, 0, "sorrel", 1, 0, 1], attributes, -1),
|
||||
Case([1, 0, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
|
||||
Case([2, 1, "potato", 0, 0, 0], attributes, 0),
|
||||
Case([3, 1, "strawberry", 1, 1, 0], attributes, 1),
|
||||
Case([1, 0, "sorrel", 1, 1, 1], attributes, 0),
|
||||
Case([5, 1, "strawberry", 1, 1, 0], attributes, 1),
|
||||
Case([2, 0, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([5, 1, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([1, 1, "wheat", 0, 1, 1], attributes, 0),
|
||||
Case([1, 1, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([4, 0, "potato", 1, 1, 0], attributes, 1),
|
||||
Case([2, 1, "strawberry", 0, 0, 1], attributes, 0),
|
||||
Case([0, 1, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([3, 0, "sorrel", 1, 0, 1], attributes, 0),
|
||||
Case([4, 0, "wheat", 1, 0, 1], attributes, 0),
|
||||
Case([5, 1, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([1, 0, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([5, 0, "sorrel", 1, 0, 1], attributes, 0),
|
||||
Case([2, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([4, 0, "wheat", 1, 1, 0], attributes, 1),
|
||||
Case([0, 0, "sorrel", 0, 1, 0], attributes, 0),
|
||||
Case([2, 0, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([0, 1, "potato", 1, 0, 1], attributes, -1),
|
||||
Case([0, 1, "sorrel", 1, 0, 1], attributes, -1),
|
||||
Case([1, 1, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([3, 0, "sorrel", 1, 1, 1], attributes, 0),
|
||||
Case([0, 0, "potato", 1, 0, 1], attributes, -1),
|
||||
Case([4, 0, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([0, 0, "strawberry", 1, 0, 0], attributes, -1),
|
||||
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
|
||||
Case([5, 0, "potato", 0, 1, 1], attributes, 0),
|
||||
Case([2, 0, "potato", 0, 0, 0], attributes, 0),
|
||||
Case([0, 1, "potato", 1, 0, 0], attributes, -1),
|
||||
Case([1, 0, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([4, 0, "sorrel", 1, 0, 1], attributes, 0),
|
||||
Case([1, 0, "potato", 1, 0, 0], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([2, 1, "wheat", 1, 0, 0], attributes, 0),
|
||||
Case([0, 1, "potato", 1, 1, 1], attributes, -1),
|
||||
Case([5, 1, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([3, 0, "strawberry", 1, 0, 0], attributes, 0),
|
||||
Case([0, 1, "strawberry", 0, 0, 1], attributes, 0),
|
||||
Case([0, 1, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
|
||||
Case([1, 0, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([1, 1, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([0, 1, "strawberry", 1, 0, 1], attributes, -1),
|
||||
Case([1, 1, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([0, 0, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([4, 1, "sorrel", 1, 1, 1], attributes, 0),
|
||||
Case([5, 1, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([5, 1, "strawberry", 0, 0, 0], attributes, 0),
|
||||
Case([4, 1, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([1, 1, "potato", 1, 1, 0], attributes, 0),
|
||||
Case([0, 1, "sorrel", 1, 0, 0], attributes, -1),
|
||||
Case([5, 0, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
|
||||
Case([0, 0, "potato", 0, 0, 0], attributes, 0),
|
||||
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
|
||||
Case([4, 0, "strawberry", 0, 0, 1], attributes, 0),
|
||||
Case([2, 0, "potato", 1, 0, 0], attributes, 0),
|
||||
Case([4, 0, "strawberry", 0, 0, 0], attributes, 0),
|
||||
Case([0, 1, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([1, 1, "wheat", 1, 1, 0], attributes, 0),
|
||||
Case([3, 0, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([1, 1, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([1, 1, "sorrel", 0, 0, 1], attributes, 0),
|
||||
Case([3, 1, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([3, 1, "wheat", 0, 0, 0], attributes, 0),
|
||||
Case([1, 0, "wheat", 0, 1, 1], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([5, 0, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([2, 1, "sorrel", 0, 1, 0], attributes, 0),
|
||||
Case([5, 0, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([5, 1, "wheat", 1, 1, 0], attributes, 1),
|
||||
Case([3, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([5, 0, "wheat", 0, 0, 1], attributes, 0),
|
||||
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([0, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 1, 0], attributes, 1),
|
||||
Case([2, 0, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([3, 0, "potato", 1, 1, 0], attributes, 0),
|
||||
Case([1, 1, "potato", 0, 0, 0], attributes, 0),
|
||||
Case([2, 1, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([2, 1, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([2, 0, "wheat", 1, 1, 1], attributes, 0),
|
||||
Case([5, 0, "wheat", 1, 1, 0], attributes, 1),
|
||||
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([0, 1, "potato", 0, 0, 0], attributes, 0),
|
||||
Case([5, 1, "strawberry", 1, 1, 1], attributes, 0),
|
||||
Case([4, 1, "wheat", 1, 0, 0], attributes, 0),
|
||||
Case([5, 1, "sorrel", 0, 0, 1], attributes, 0),
|
||||
Case([1, 1, "wheat", 1, 0, 0], attributes, 0),
|
||||
Case([5, 0, "strawberry", 1, 0, 1], attributes, 0),
|
||||
Case([5, 0, "wheat", 1, 0, 1], attributes, 0),
|
||||
Case([2, 0, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([3, 1, "wheat", 1, 0, 0], attributes, 0),
|
||||
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
|
||||
Case([0, 1, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([3, 0, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([3, 0, "potato", 0, 0, 1], attributes, 0),
|
||||
Case([2, 1, "strawberry", 1, 1, 0], attributes, 0),
|
||||
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([4, 1, "wheat", 1, 0, 1], attributes, 0),
|
||||
Case([2, 0, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([5, 0, "sorrel", 0, 1, 1], attributes, 0),
|
||||
Case([0, 1, "wheat", 1, 1, 0], attributes, -1),
|
||||
Case([5, 1, "wheat", 1, 0, 0], attributes, 0),
|
||||
Case([2, 0, "potato", 0, 0, 0], attributes, 0),
|
||||
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
|
||||
Case([4, 1, "potato", 0, 1, 1], attributes, 0),
|
||||
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
|
||||
Case([1, 1, "strawberry", 1, 0, 1], attributes, -1),
|
||||
Case([3, 0, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([5, 1, "wheat", 1, 0, 0], attributes, 0),
|
||||
Case([4, 0, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([2, 1, "sorrel", 1, 0, 0], attributes, 0),
|
||||
Case([0, 1, "wheat", 0, 1, 0], attributes, 0),
|
||||
Case([5, 0, "potato", 1, 1, 0], attributes, 1),
|
||||
Case([3, 1, "strawberry", 0, 1, 0], attributes, 0),
|
||||
Case([5, 1, "strawberry", 0, 0, 0], attributes, 0),
|
||||
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
|
||||
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
|
||||
Case([0, 0, "sorrel", 0, 0, 0], attributes, 0),
|
||||
Case([1, 1, "sorrel", 1, 1, 0], attributes, 1),
|
||||
Case([0, 1, "potato", 0, 1, 0], attributes, 0),
|
||||
Case([4, 1, "strawberry", 1, 1, 1], attributes, 0),
|
||||
Case([0, 0, "wheat", 0, 1, 1], attributes, 0),
|
||||
Case([3, 0, "wheat", 1, 1, 0], attributes, 1)]
|
12
colors.py
@ -1,8 +1,12 @@
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
GREEN = (0, 255, 0)
|
||||
RED = (255, 0, 0)
|
||||
BROWN = (140, 95, 65)
|
||||
BROWN0 = (180, 150, 90)
|
||||
BROWN1 = (160, 130, 70)
|
||||
BROWN2 = (140, 110, 55)
|
||||
BROWN3 = (110, 85, 40)
|
||||
BROWN4 = (80, 60, 20)
|
||||
DBROWN = (65, 50, 20)
|
||||
YELLOW = (255, 255, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
LBROWN = (108, 97, 62)
|
||||
BLUE = (18, 93, 156)
|
||||
GREY = (91, 91, 91)
|
||||
|
44
field.py
@ -3,41 +3,57 @@ from colors import *
|
||||
from dimensions import *
|
||||
|
||||
class Field(pygame.sprite.Sprite):
|
||||
def __init__(self, row, column):
|
||||
def __init__(self, row, column, field_type):
|
||||
super(Field, self).__init__()
|
||||
self.surf = pygame.Surface((WIDTH, HEIGHT))
|
||||
self.surf.fill(BROWN)
|
||||
self.field_type = field_type
|
||||
if self.field_type == "soil":
|
||||
self.moveCost = 3
|
||||
self.surf.fill(BROWN0)
|
||||
elif self.field_type == "rocks":
|
||||
self.moveCost = 5
|
||||
self.surf.fill(LBROWN)
|
||||
elif self.field_type == "road":
|
||||
self.moveCost = 1
|
||||
self.surf.fill(GREY)
|
||||
elif self.field_type == "pond":
|
||||
self.moveCost = 1000
|
||||
self.surf.fill(BLUE)
|
||||
self.rect = self.surf.get_rect(
|
||||
topleft=((MARGIN + WIDTH) * row + MARGIN, (MARGIN + HEIGHT) * column + MARGIN))
|
||||
self.position = [row, column]
|
||||
self.hydration = 0
|
||||
self.isRocky = False
|
||||
self.planted = 0
|
||||
self.ferility = 1
|
||||
self.fertility = 1
|
||||
self.tractor_there = False
|
||||
|
||||
def hydrate(self):
|
||||
if self.hydration <= 3:
|
||||
if self.field_type == "soil" and self.hydration <= 5:
|
||||
self.hydration += 1
|
||||
if self.hydration == 0:
|
||||
self.surf.fill(BROWN)
|
||||
self.surf.fill(BROWN0)
|
||||
if self.hydration == 1:
|
||||
self.surf.fill(YELLOW)
|
||||
self.surf.fill(BROWN1)
|
||||
if self.hydration == 2:
|
||||
self.surf.fill(GREEN)
|
||||
self.surf.fill(BROWN2)
|
||||
if self.hydration == 3:
|
||||
self.surf.fill(BLUE)
|
||||
self.surf.fill(BROWN3)
|
||||
if self.hydration == 4 or self.hydration == 5:
|
||||
self.surf.fill(BROWN4)
|
||||
|
||||
def dehydrate(self):
|
||||
if self.hydration > 0:
|
||||
if self.field_type == "soil" and self.hydration > 0:
|
||||
self.hydration -= 1
|
||||
if self.hydration == 0:
|
||||
self.surf.fill(BROWN)
|
||||
self.surf.fill(BROWN0)
|
||||
if self.hydration == 1:
|
||||
self.surf.fill(YELLOW)
|
||||
self.surf.fill(BROWN1)
|
||||
if self.hydration == 2:
|
||||
self.surf.fill(GREEN)
|
||||
self.surf.fill(BROWN2)
|
||||
if self.hydration == 3:
|
||||
self.surf.fill(BLUE)
|
||||
self.surf.fill(BROWN3)
|
||||
if self.hydration == 4 or self.hydration == 5:
|
||||
self.surf.fill(BROWN4)
|
||||
|
||||
def free(self):
|
||||
self.planted = 0
|
||||
|
65
grader.py
Normal file
@ -0,0 +1,65 @@
|
||||
import torch
|
||||
import torchvision
|
||||
import torchvision.transforms as transforms
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as f
|
||||
import torch.optim as optim
|
||||
import numpy as np
|
||||
from matplotlib.pyplot import imshow
|
||||
import os
|
||||
import PIL
|
||||
import numpy as np
|
||||
import neural_network
|
||||
from matplotlib.pyplot import imshow
|
||||
|
||||
# Create the model
|
||||
model = neural_network.Net()
|
||||
|
||||
# Load state_dict
|
||||
neural_network.load_network_from_structure(model)
|
||||
|
||||
# Create the preprocessing transformation here
|
||||
transform = transforms.Compose([neural_network.Negative(), transforms.ToTensor()])
|
||||
|
||||
# load your image(s)
|
||||
img = PIL.Image.open('test\\0_100.jpg')
|
||||
img2 = PIL.Image.open('test\\1_100.jpg')
|
||||
img3 = PIL.Image.open('test\\4_100.jpg')
|
||||
img4 = PIL.Image.open('test\\5_100.jpg')
|
||||
|
||||
# Transform
|
||||
input = transform(img)
|
||||
input2 = transform(img2)
|
||||
input3 = transform(img3)
|
||||
input4 = transform(img4)
|
||||
|
||||
# unsqueeze batch dimension, in case you are dealing with a single image
|
||||
input = input.unsqueeze(0)
|
||||
input2 = input2.unsqueeze(0)
|
||||
input3 = input3.unsqueeze(0)
|
||||
input4 = input4.unsqueeze(0)
|
||||
|
||||
# Set model to eval
|
||||
model.eval()
|
||||
|
||||
# Get prediction
|
||||
output = model(input)
|
||||
output2 = model(input2)
|
||||
output3 = model(input3)
|
||||
output4 = model(input4)
|
||||
|
||||
print(output)
|
||||
index = output.cpu().data.numpy().argmax()
|
||||
print(index)
|
||||
|
||||
print(output2)
|
||||
index = output2.cpu().data.numpy().argmax()
|
||||
print(index)
|
||||
|
||||
print(output3)
|
||||
index = output3.cpu().data.numpy().argmax()
|
||||
print(index)
|
||||
|
||||
print(output4)
|
||||
index = output4.cpu().data.numpy().argmax()
|
||||
print(index)
|
122
id3.py
Normal file
@ -0,0 +1,122 @@
|
||||
from cases import *
|
||||
from collections import Counter
|
||||
import operator
|
||||
from types import prepare_class
|
||||
import numpy as np
|
||||
import copy
|
||||
|
||||
class Node:
|
||||
def __init__(self, Class, tag=None):
|
||||
self.Class = Class
|
||||
self.childs = []
|
||||
|
||||
def classes_of_cases (cases):
|
||||
classes = []
|
||||
for case in cases:
|
||||
if case.Class not in classes:
|
||||
classes.append(case.Class)
|
||||
return classes
|
||||
|
||||
def count_classes (cases):
|
||||
classes = []
|
||||
for case in cases:
|
||||
classes.append(case.Class)
|
||||
c = Counter(classes)
|
||||
return max(c.items(), key=operator.itemgetter(1))[0]
|
||||
|
||||
def chose_attribute (cases, attributes):
|
||||
a = ""
|
||||
max = float("-inf")
|
||||
for attribute in attributes:
|
||||
if I(cases) - E(cases, attribute) >= max:
|
||||
max = I(cases) - E(cases, attribute)
|
||||
a = attribute
|
||||
return a
|
||||
|
||||
def I (cases):
|
||||
i = 0
|
||||
all = len(cases)
|
||||
classes = classes_of_cases(cases)
|
||||
for Class in classes:
|
||||
noc = 0
|
||||
for case in cases:
|
||||
if case.Class == Class:
|
||||
noc += 1
|
||||
i -= (noc/all)*np.log2(noc/all)
|
||||
return i
|
||||
|
||||
def E(cases, attribute):
|
||||
e = 0
|
||||
values = []
|
||||
index = cases[0].attributes.index(attribute)
|
||||
for case in cases:
|
||||
if case.values[index] not in values:
|
||||
values.append(case.values[index])
|
||||
for value in values:
|
||||
ei = []
|
||||
for case in cases:
|
||||
if case.values[index] == value:
|
||||
ei.append(case)
|
||||
e += (len(ei)/len(cases))*I(ei)
|
||||
return e
|
||||
|
||||
|
||||
def treelearn(cases, attributes, default_class):
|
||||
if cases == []:
|
||||
t = Node(default_class)
|
||||
return t
|
||||
if len(classes_of_cases(cases)) == 1:
|
||||
t = Node(cases[0].Class)
|
||||
return t
|
||||
if attributes == []:
|
||||
t = Node(count_classes(cases))
|
||||
return t
|
||||
A = chose_attribute(cases, attributes)
|
||||
t = Node(A)
|
||||
new_default_class = count_classes(cases)
|
||||
|
||||
values = []
|
||||
index = attributes.index(A)
|
||||
for case in cases:
|
||||
if case.values[index] not in values:
|
||||
values.append(case.values[index])
|
||||
|
||||
for value in values:
|
||||
new_cases = []
|
||||
for case in cases:
|
||||
if case.values[index] == value:
|
||||
new_case = copy.deepcopy(case)
|
||||
new_case.values = case.values[:index] + case.values[index+1:]
|
||||
new_case.attributes = case.attributes[:index] + case.attributes[index+1:]
|
||||
new_cases.append(new_case)
|
||||
new_attributes = attributes[:index] + attributes[index+1 :]
|
||||
child = treelearn(new_cases, new_attributes, new_default_class)
|
||||
t.childs.append([child, value])
|
||||
|
||||
return t
|
||||
|
||||
def pretty_print(root, n):
|
||||
if len(root.childs) == 0:
|
||||
for _ in range(n):
|
||||
print(" ", end="")
|
||||
print("return " + str(root.Class))
|
||||
for child in root.childs:
|
||||
for _ in range(n):
|
||||
print(" ", end="")
|
||||
if child != root.childs[0]:
|
||||
print("el", end= "")
|
||||
if len(str(child[1])) > 1:
|
||||
print("if self." + str(root.Class) + " == \"" + str(child[1]) + "\":")
|
||||
else:
|
||||
print("if self." + str(root.Class) + " == " + str(child[1]) + ":")
|
||||
pretty_print(child[0], n+1)
|
||||
|
||||
|
||||
tree = treelearn(cases, attributes, 0)
|
||||
pretty_print(tree, 0)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
27
mapschema.py
Normal file
@ -0,0 +1,27 @@
|
||||
def createField():
|
||||
field = [["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"],
|
||||
["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"],
|
||||
["soil", "soil", "soil", "soil", "soil", "road", "road", "road", "road", "road"],
|
||||
["rocks", "rocks", "rocks", "rocks", "soil", "road", "soil", "soil", "rocks", "soil"],
|
||||
["soil", "soil", "soil", "soil", "soil", "road", "rocks", "rocks", "soil", "soil"],
|
||||
["soil", "soil", "soil", "pond", "rocks", "road", "rocks", "soil", "soil", "rocks"],
|
||||
["rocks", "pond", "pond", "pond", "pond", "road", "rocks", "soil", "soil", "rocks"],
|
||||
["road", "road", "road", "road", "road", "road", "rocks", "soil", "soil", "soil"],
|
||||
["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks"],
|
||||
["soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks", "soil"]
|
||||
]
|
||||
return field
|
||||
|
||||
def createPlants():
|
||||
field = [["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"],
|
||||
["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"],
|
||||
["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0],
|
||||
["wheat", "wheat", "wheat", 0, 0, 0, 0, "potato", "potato", 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, "potato", "potato", 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, "potato", "potato", "potato"],
|
||||
["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, "potato", 0, 0],
|
||||
["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, 0, 0, 0]
|
||||
]
|
||||
return field
|
103
neural_network.py
Normal file
@ -0,0 +1,103 @@
|
||||
import torch
|
||||
import torchvision
|
||||
import torchvision.transforms as transforms
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as f
|
||||
import torch.optim as optim
|
||||
import numpy as np
|
||||
from matplotlib.pyplot import imshow
|
||||
import os
|
||||
import PIL
|
||||
import numpy as np
|
||||
from matplotlib.pyplot import imshow
|
||||
|
||||
def to_negative(img):
|
||||
img = PIL.ImageOps.invert(img)
|
||||
return img
|
||||
|
||||
class Negative(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def __call__(self, img):
|
||||
return to_negative(img)
|
||||
|
||||
def plotdigit(image):
|
||||
img = np.reshape(image, (-1, 100))
|
||||
imshow(img, cmap='Greys')
|
||||
|
||||
transform = transforms.Compose([Negative(), transforms.ToTensor()])
|
||||
train_set = torchvision.datasets.ImageFolder(root='train', transform=transform)
|
||||
classes = ("apple", "potato")
|
||||
|
||||
BATCH_SIZE = 2
|
||||
train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE, shuffle=True, num_workers=0)
|
||||
|
||||
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
||||
|
||||
class Net(nn.Module):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.flatten = nn.Flatten()
|
||||
self.linear_relu_stack = nn.Sequential(
|
||||
nn.Linear(3*100*100, 512),
|
||||
nn.ReLU(),
|
||||
nn.Linear(512, 512),
|
||||
nn.ReLU(),
|
||||
nn.Linear(512, 2),
|
||||
nn.ReLU()
|
||||
)
|
||||
self.linear_relu_stack = self.linear_relu_stack.to(device)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.flatten(x).to(device)
|
||||
logits = self.linear_relu_stack(x).to(device)
|
||||
return logits
|
||||
|
||||
def training_network():
|
||||
net = Net()
|
||||
net = net.to(device)
|
||||
|
||||
criterion = nn.CrossEntropyLoss()
|
||||
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
|
||||
|
||||
for epoch in range(4):
|
||||
running_loss = 0.0
|
||||
for i, data in enumerate(train_loader, 0):
|
||||
inputs, labels = data[0].to(device), data[1].to(device)
|
||||
optimizer.zero_grad()
|
||||
outputs = net(inputs.to(device))
|
||||
loss = criterion(outputs, labels)
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
|
||||
running_loss += loss.item()
|
||||
if i % 2000 == 1999:
|
||||
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss))
|
||||
running_loss = 0.0
|
||||
|
||||
print("Finished training")
|
||||
save_network_to_file(net)
|
||||
|
||||
|
||||
def result_from_network(net, loaded_image):
|
||||
image = PIL.Image.open(loaded_image)
|
||||
pil_to_tensor = transforms.ToTensor()(image.convert("RGB")).unsqueeze_(0)
|
||||
outputs = net(pil_to_tensor.to(device))
|
||||
|
||||
return classes[torch.max(outputs, 1)[1]]
|
||||
|
||||
|
||||
def save_network_to_file(network):
|
||||
torch.save(network.state_dict(), 'network_model.pth')
|
||||
print("Network saved to file")
|
||||
|
||||
|
||||
def load_network_from_structure(network):
|
||||
network.load_state_dict(torch.load('network_model.pth'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(torch.cuda.is_available())
|
||||
training_network()
|
||||
|
169
node.py
Normal file
@ -0,0 +1,169 @@
|
||||
from dimensions import *
|
||||
import heapq
|
||||
|
||||
|
||||
def getTotalCost(x):
|
||||
return x.totalCost
|
||||
|
||||
def showPath(node, goal):
|
||||
path = node.findPath(goal)
|
||||
for x in path:
|
||||
print(x.position, end=" ")
|
||||
print(x.rotation, end=" ")
|
||||
print(x.action)
|
||||
print("***")
|
||||
|
||||
|
||||
def succesor (node):
|
||||
succesors = []
|
||||
if node.position[0]+node.rotation[0] in range(0,GSIZE) and node.position[1]+node.rotation[1] in range(0,GSIZE):
|
||||
child = Node(node.field, [node.position[0]+node.rotation[0], node.position[1]+node.rotation[1]], node.rotation)
|
||||
child.action = "move"
|
||||
succesors.append(child)
|
||||
if node.rotation == [1,0]:
|
||||
child = Node(node.field, node.position, [0,-1])
|
||||
child.action = "left"
|
||||
succesors.append(child)
|
||||
child = Node(node.field, node.position, [0,1])
|
||||
child.action = "right"
|
||||
succesors.append(child)
|
||||
if node.rotation == [0,1]:
|
||||
child = Node(node.field, node.position, [-1, 0])
|
||||
succesors.append(child)
|
||||
child.action = "right"
|
||||
child = Node(node.field, node.position, [1, 0])
|
||||
child.action = "left"
|
||||
succesors.append(child)
|
||||
if node.rotation == [-1,0]:
|
||||
child = Node(node.field, node.position, [0,-1])
|
||||
succesors.append(child)
|
||||
child.action = "right"
|
||||
child = Node(node.field, node.position, [0,1])
|
||||
child.action = "left"
|
||||
succesors.append(child)
|
||||
if node.rotation == [0,-1]:
|
||||
child = Node(node.field, node.position, [-1, 0])
|
||||
child.action = "left"
|
||||
succesors.append(child)
|
||||
child = Node(node.field, node.position, [1, 0])
|
||||
child.action = "right"
|
||||
succesors.append(child)
|
||||
return succesors
|
||||
|
||||
class Node():
|
||||
def __init__(self, field, position, rotation):
|
||||
self.parent = 0
|
||||
self.startCost = 0
|
||||
self.heuristic = 0
|
||||
self.totalCost = 0
|
||||
self.position = position
|
||||
self.rotation = rotation
|
||||
self.action = 0
|
||||
self.field = field
|
||||
|
||||
def __lt__(self, other):
|
||||
return self.totalCost < other.totalCost
|
||||
|
||||
def findPath(self, goal):
|
||||
startNode = Node(self.field, self.position, self.rotation)
|
||||
goalNode = goal
|
||||
|
||||
openList = []
|
||||
closedList = []
|
||||
|
||||
startNode.parent = None
|
||||
|
||||
openList.append(startNode)
|
||||
|
||||
while len(openList) > 0:
|
||||
openList.sort(key=getTotalCost)
|
||||
|
||||
currentNode = openList.pop(0)
|
||||
closedList.append(currentNode)
|
||||
|
||||
if currentNode.position == goalNode:
|
||||
path = []
|
||||
current = currentNode
|
||||
while current is not None:
|
||||
path.append(current)
|
||||
current = current.parent
|
||||
return path[::-1]
|
||||
|
||||
children = succesor(currentNode)
|
||||
|
||||
perm = 0
|
||||
for child in children:
|
||||
for closedChild in closedList:
|
||||
if child.position == closedChild.position and child.rotation == closedChild.rotation and child.action == closedChild.action:
|
||||
perm = 1
|
||||
break
|
||||
if perm == 1:
|
||||
perm = 0
|
||||
continue
|
||||
child.parent = currentNode
|
||||
child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost
|
||||
child.heuristic = abs(goal[0]-child.position[0]) + abs(goal[1]-child.position[1])
|
||||
child.totalCost = child.startCost+child.heuristic
|
||||
|
||||
for openNode in openList:
|
||||
if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost > openNode.startCost:
|
||||
perm = 1
|
||||
break
|
||||
|
||||
if perm == 1:
|
||||
perm = 0
|
||||
continue
|
||||
|
||||
openList.append(child)
|
||||
|
||||
def findPathToPlant(self):
|
||||
startNode = Node(self.field, self.position, self.rotation)
|
||||
|
||||
openList = []
|
||||
closedList = []
|
||||
|
||||
startNode.parent = None
|
||||
|
||||
heapq.heappush(openList, startNode)
|
||||
|
||||
while len(openList) > 0:
|
||||
currentNode = heapq.heappop(openList)
|
||||
|
||||
closedList.append(currentNode)
|
||||
|
||||
if currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration < 2:
|
||||
path = []
|
||||
for _ in range(currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration, 4):
|
||||
path.append("hydrate")
|
||||
current = currentNode
|
||||
while current is not None:
|
||||
path.append(current.action)
|
||||
current = current.parent
|
||||
return path[::-1]
|
||||
|
||||
children = succesor(currentNode)
|
||||
|
||||
perm = 0
|
||||
for child in children:
|
||||
for closedChild in closedList:
|
||||
if child.position == closedChild.position and child.rotation == closedChild.rotation and child.action == closedChild.action:
|
||||
perm = 1
|
||||
break
|
||||
if perm == 1:
|
||||
perm = 0
|
||||
continue
|
||||
child.parent = currentNode
|
||||
child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost
|
||||
child.heuristic = abs(startNode.position[0]-child.position[0]) + abs(startNode.position[1]-child.position[1])
|
||||
child.totalCost = child.startCost+child.heuristic
|
||||
|
||||
for openNode in openList:
|
||||
if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost >= openNode.startCost:
|
||||
perm = 1
|
||||
break
|
||||
|
||||
if perm == 1:
|
||||
perm = 0
|
||||
continue
|
||||
|
||||
heapq.heappush(openList, child)
|
159
plant.py
@ -6,39 +6,150 @@ from sprites import *
|
||||
class Plant(pygame.sprite.Sprite):
|
||||
def __init__(self, field, species):
|
||||
super(Plant, self).__init__()
|
||||
self.surf = plant_img_0
|
||||
self.species = species
|
||||
if self.species == "wheat":
|
||||
self.growth_speed = 1.5
|
||||
self.humidity_needed = 2
|
||||
self.img0 = wheat_img_0
|
||||
self.img1 = wheat_img_1
|
||||
self.img2 = wheat_img_2
|
||||
self.img3 = wheat_img_3
|
||||
elif self.species == "potato":
|
||||
self.growth_speed = 1
|
||||
self.humidity_needed = 1
|
||||
self.img0 = potato_img_0
|
||||
self.img1 = potato_img_1
|
||||
self.img2 = potato_img_2
|
||||
self.img3 = potato_img_3
|
||||
elif self.species == "strawberry":
|
||||
self.growth_speed = 0.8
|
||||
self.humidity_needed = 1
|
||||
self.img0 = strawberry_img_0
|
||||
self.img1 = strawberry_img_1
|
||||
self.img2 = strawberry_img_2
|
||||
self.img3 = strawberry_img_3
|
||||
self.surf = self.img0
|
||||
self.position = field.position
|
||||
self.field = field
|
||||
self.rect = self.surf.get_rect(
|
||||
topleft=((MARGIN + WIDTH) * self.position[0] + MARGIN, (MARGIN + HEIGHT) * self.position[1] + MARGIN))
|
||||
self.growth = 0
|
||||
self.isHealthy = True
|
||||
self.species = species
|
||||
if self.species == "beetroot":
|
||||
self.growth_speed = 1.5
|
||||
self.humidity_needed = 2
|
||||
elif self.species == "wheat":
|
||||
self.growth_speed = 1
|
||||
self.humidity_needed = 1
|
||||
elif self.species == "cotton":
|
||||
self.growth_speed = 0.8
|
||||
self.humidity_needed = 1
|
||||
self.is_healthy = True
|
||||
field.planted = True
|
||||
self.tickscount = 0
|
||||
self.ticks = 0
|
||||
|
||||
def dtree(self):
|
||||
if self.field.hydration == 4:
|
||||
if self.is_healthy == 1:
|
||||
if self.field.tractor_there == 0:
|
||||
if self.ticks == 0:
|
||||
return 0
|
||||
elif self.ticks == 1:
|
||||
return 1
|
||||
elif self.field.tractor_there == 1:
|
||||
return 0
|
||||
elif self.is_healthy == 0:
|
||||
return 0
|
||||
elif self.field.hydration == 2:
|
||||
if self.species == "sorrel":
|
||||
if self.ticks == 1:
|
||||
if self.is_healthy == 1:
|
||||
return 1
|
||||
elif self.is_healthy == 0:
|
||||
return 0
|
||||
elif self.ticks == 0:
|
||||
return 0
|
||||
elif self.species == "potato":
|
||||
return 0
|
||||
elif self.species == "wheat":
|
||||
return 0
|
||||
elif self.species == "strawberry":
|
||||
return 0
|
||||
elif self.field.hydration == 1:
|
||||
if self.species == "potato":
|
||||
return 0
|
||||
elif self.species == "strawberry":
|
||||
if self.ticks == 1:
|
||||
return -1
|
||||
elif self.ticks == 0:
|
||||
return 0
|
||||
elif self.species == "wheat":
|
||||
return 0
|
||||
elif self.species == "sorrel":
|
||||
if self.is_healthy == 0:
|
||||
return 0
|
||||
elif self.is_healthy == 1:
|
||||
if self.field.tractor_there == 0:
|
||||
if self.ticks == 0:
|
||||
return 0
|
||||
elif self.ticks == 1:
|
||||
return 1
|
||||
elif self.field.tractor_there == 1:
|
||||
return 0
|
||||
elif self.field.hydration == 3:
|
||||
if self.ticks == 1:
|
||||
if self.field.tractor_there == 0:
|
||||
if self.is_healthy == 1:
|
||||
if self.species == "potato":
|
||||
if self.field.fertility == 1:
|
||||
return 1
|
||||
elif self.field.fertility == 0:
|
||||
return 0
|
||||
elif self.species == "strawberry":
|
||||
return 1
|
||||
elif self.species == "sorrel":
|
||||
return 1
|
||||
elif self.species == "wheat":
|
||||
return 1
|
||||
elif self.is_healthy == 0:
|
||||
return 0
|
||||
elif self.field.tractor_there == 1:
|
||||
return 0
|
||||
elif self.ticks == 0:
|
||||
return 0
|
||||
elif self.field.hydration == 5:
|
||||
if self.field.tractor_there == 1:
|
||||
return 0
|
||||
elif self.field.tractor_there == 0:
|
||||
if self.is_healthy == 0:
|
||||
return 0
|
||||
elif self.is_healthy == 1:
|
||||
if self.ticks == 1:
|
||||
return 1
|
||||
elif self.ticks == 0:
|
||||
return 0
|
||||
elif self.field.hydration == 0:
|
||||
if self.ticks == 0:
|
||||
return 0
|
||||
elif self.ticks == 1:
|
||||
return -1
|
||||
|
||||
def update(self):
|
||||
if self.growth == 0:
|
||||
self.surf = self.img0
|
||||
if self.growth == 1:
|
||||
self.surf = self.img1
|
||||
if self.growth == 2:
|
||||
self.surf = self.img2
|
||||
if self.growth == 3:
|
||||
self.surf = self.img3
|
||||
|
||||
def grow(self):
|
||||
if self.field.hydration >= self.humidity_needed:
|
||||
self.growth += self.growth_speed
|
||||
if self.field.hydration == 0:
|
||||
self.growth -= self.growth_speed
|
||||
if self.dtree() == 1:
|
||||
self.growth += 1
|
||||
self.ticks = 0
|
||||
elif self.dtree() == -1:
|
||||
self.growth -= 1
|
||||
self.ticks = 0
|
||||
if self.growth > 4:
|
||||
self.growth = 4
|
||||
if self.growth < 0:
|
||||
self.growth = 0
|
||||
if self.growth == 0:
|
||||
self.surf = plant_img_0
|
||||
elif self.growth == 1:
|
||||
self.surf = plant_img_1
|
||||
elif self.growth == 2:
|
||||
self.surf = plant_img_2
|
||||
elif self.growth == 3:
|
||||
self.surf = plant_img_3
|
||||
self.update()
|
||||
|
||||
def tick(self):
|
||||
self.tickscount += 1
|
||||
if self.tickscount >= 25:
|
||||
self.tickscount = 0
|
||||
self.ticks = 1
|
||||
|
23
sprites.py
@ -1,11 +1,22 @@
|
||||
import pygame
|
||||
import random
|
||||
import os
|
||||
import pygame
|
||||
|
||||
# set up asset folders
|
||||
project_folder = os.path.dirname(__file__)
|
||||
sprites_folder = os.path.join(project_folder, 'sprites')
|
||||
plant_img_0 = pygame.image.load(os.path.join(sprites_folder, 'plant0.png'))
|
||||
plant_img_1 = pygame.image.load(os.path.join(sprites_folder, 'plant1.png'))
|
||||
plant_img_2 = pygame.image.load(os.path.join(sprites_folder, 'plant2.png'))
|
||||
plant_img_3 = pygame.image.load(os.path.join(sprites_folder, 'plant3.png'))
|
||||
wheat_img_0 = pygame.image.load(os.path.join(sprites_folder, 'wheat0.png'))
|
||||
wheat_img_1 = pygame.image.load(os.path.join(sprites_folder, 'wheat1.png'))
|
||||
wheat_img_2 = pygame.image.load(os.path.join(sprites_folder, 'wheat2.png'))
|
||||
wheat_img_3 = pygame.image.load(os.path.join(sprites_folder, 'wheat3.png'))
|
||||
tractor_img_0 = pygame.image.load(os.path.join(sprites_folder, 'tractor0.png'))
|
||||
tractor_img_1 = pygame.image.load(os.path.join(sprites_folder, 'tractor1.png'))
|
||||
tractor_img_2 = pygame.image.load(os.path.join(sprites_folder, 'tractor2.png'))
|
||||
tractor_img_3 = pygame.image.load(os.path.join(sprites_folder, 'tractor3.png'))
|
||||
potato_img_0 = pygame.image.load(os.path.join(sprites_folder, 'potato0.png'))
|
||||
potato_img_1 = pygame.image.load(os.path.join(sprites_folder, 'potato1.png'))
|
||||
potato_img_2 = pygame.image.load(os.path.join(sprites_folder, 'potato2.png'))
|
||||
potato_img_3 = pygame.image.load(os.path.join(sprites_folder, 'potato3.png'))
|
||||
strawberry_img_0 = pygame.image.load(os.path.join(sprites_folder, 'strawberry0.png'))
|
||||
strawberry_img_1 = pygame.image.load(os.path.join(sprites_folder, 'strawberry1.png'))
|
||||
strawberry_img_2 = pygame.image.load(os.path.join(sprites_folder, 'strawberry2.png'))
|
||||
strawberry_img_3 = pygame.image.load(os.path.join(sprites_folder, 'strawberry3.png'))
|
||||
|
BIN
sprites/potato0.png
Normal file
After Width: | Height: | Size: 1.9 KiB |
BIN
sprites/potato1.png
Normal file
After Width: | Height: | Size: 3.0 KiB |
BIN
sprites/potato2.png
Normal file
After Width: | Height: | Size: 4.1 KiB |
BIN
sprites/potato3.png
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
sprites/strawberry0.png
Normal file
After Width: | Height: | Size: 1.9 KiB |
BIN
sprites/strawberry1.png
Normal file
After Width: | Height: | Size: 2.5 KiB |
BIN
sprites/strawberry2.png
Normal file
After Width: | Height: | Size: 3.2 KiB |
BIN
sprites/strawberry3.png
Normal file
After Width: | Height: | Size: 3.6 KiB |
BIN
sprites/tractor0.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
sprites/tractor1.png
Normal file
After Width: | Height: | Size: 2.6 KiB |
BIN
sprites/tractor2.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
sprites/tractor3.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
sprites/wheat0.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
sprites/wheat1.png
Normal file
After Width: | Height: | Size: 2.9 KiB |
BIN
sprites/wheat2.png
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
sprites/wheat3.png
Normal file
After Width: | Height: | Size: 5.9 KiB |
BIN
test/0_100.jpg
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
test/1_100.jpg
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
test/4_100.jpg
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
test/5_100.jpg
Normal file
After Width: | Height: | Size: 5.2 KiB |
91
tractor.py
@ -14,47 +14,82 @@ from pygame.locals import (
|
||||
|
||||
from dimensions import *
|
||||
from colors import *
|
||||
from sprites import *
|
||||
|
||||
class Tractor(pygame.sprite.Sprite):
|
||||
def __init__(self, field):
|
||||
def __init__(self, field, position):
|
||||
super(Tractor, self).__init__()
|
||||
self.surf = pygame.Surface((WIDTH, HEIGHT))
|
||||
self.surf.fill(RED)
|
||||
self.position = field.position
|
||||
self.surf = tractor_img_0
|
||||
self.position = position
|
||||
self.field = field
|
||||
self.rect = self.surf.get_rect(
|
||||
topleft=((MARGIN + WIDTH) * self.position[0] + MARGIN, (MARGIN + HEIGHT) * self.position[1] + MARGIN))
|
||||
self.field = field
|
||||
self.direction = [1, 0]
|
||||
self.field[self.position[0]][self.position[1]].tractor_there = True
|
||||
|
||||
def update(self, pressed_keys):
|
||||
if pressed_keys[K_UP]:
|
||||
self.rect.move_ip(0, -(HEIGHT + MARGIN))
|
||||
self.position[1] -= 1
|
||||
if self.rect.top <= MARGIN:
|
||||
def move(self):
|
||||
self.field[self.position[0]][self.position[1]].tractor_there = False
|
||||
self.rect.move_ip(self.direction[0]*(WIDTH + MARGIN), self.direction[1]*(HEIGHT + MARGIN))
|
||||
self.position[0] += self.direction[0]
|
||||
self.position[1] += self.direction[1]
|
||||
if self.position[0] >= GSIZE:
|
||||
self.position[0] = GSIZE-1
|
||||
if self.position[1] >= GSIZE:
|
||||
self.position[1] = GSIZE-1
|
||||
if self.position[0] < 0:
|
||||
self.position[0] = 0
|
||||
if self.position[1] < 0:
|
||||
self.position[1] = 0
|
||||
|
||||
if self.rect.top <= MARGIN:
|
||||
self.rect.top = MARGIN
|
||||
self.position[1] = 0
|
||||
if pressed_keys[K_DOWN]:
|
||||
self.rect.move_ip(0, HEIGHT + MARGIN)
|
||||
self.position[1] += 1
|
||||
if self.rect.bottom >= SCREEN_HEIGHT-MARGIN:
|
||||
if self.rect.bottom >= SCREEN_HEIGHT-MARGIN:
|
||||
self.rect.bottom = SCREEN_HEIGHT-MARGIN
|
||||
self.position[1] = GSIZE-1
|
||||
if pressed_keys[K_LEFT]:
|
||||
self.rect.move_ip(-(WIDTH + MARGIN), 0)
|
||||
self.position[0] -= 1
|
||||
if self.rect.left < MARGIN:
|
||||
if self.rect.left < MARGIN:
|
||||
self.rect.left = MARGIN
|
||||
self.position[0] = 0
|
||||
if pressed_keys[K_RIGHT]:
|
||||
self.rect.move_ip(WIDTH + MARGIN, 0)
|
||||
self.position[0] += 1
|
||||
if self.rect.right > SCREEN_WIDTH-MARGIN:
|
||||
if self.rect.right > SCREEN_WIDTH-MARGIN:
|
||||
self.rect.right = SCREEN_WIDTH-MARGIN
|
||||
self.position[0] = GSIZE-1
|
||||
|
||||
def hydrate(self, field, pressed_keys):
|
||||
if pressed_keys[K_SPACE]:
|
||||
field[self.position[0]][self.position[1]].hydrate()
|
||||
self.field[self.position[0]][self.position[1]].tractor_there = True
|
||||
|
||||
|
||||
def rotate_right(self):
|
||||
if self.direction == [1, 0]:
|
||||
self.direction = [0, 1]
|
||||
self.surf = tractor_img_2
|
||||
elif self.direction == [0, 1]:
|
||||
self.direction = [-1, 0]
|
||||
self.surf = tractor_img_1
|
||||
elif self.direction == [-1, 0]:
|
||||
self.direction = [0, -1]
|
||||
self.surf = tractor_img_3
|
||||
elif self.direction == [0, -1]:
|
||||
self.direction = [1, 0]
|
||||
self.surf = tractor_img_0
|
||||
|
||||
def rotate_left(self):
|
||||
if self.direction == [1, 0]:
|
||||
self.direction = [0, -1]
|
||||
self.surf = tractor_img_3
|
||||
elif self.direction == [0, -1]:
|
||||
self.direction = [-1, 0]
|
||||
self.surf = tractor_img_1
|
||||
elif self.direction == [-1, 0]:
|
||||
self.direction = [0, 1]
|
||||
self.surf = tractor_img_2
|
||||
elif self.direction == [0, 1]:
|
||||
self.direction = [1, 0]
|
||||
self.surf = tractor_img_0
|
||||
|
||||
def hydrate(self, field):
|
||||
field[self.position[0]][self.position[1]].hydrate()
|
||||
|
||||
def cut(self, field, pressed_keys):
|
||||
if pressed_keys[K_c]:
|
||||
field[self.position[0]][self.position[1]].free()
|
||||
|
||||
def plant(self, field, plant, pressed_keys):
|
||||
if field.planted == 0:
|
||||
field.planted = plant
|
||||
plant.field = field
|
||||
|
BIN
train/apple/156_100.jpg
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
train/apple/165_100.jpg
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
train/apple/18_100.jpg
Normal file
After Width: | Height: | Size: 5.0 KiB |
BIN
train/apple/31_100.jpg
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
train/apple/r_105_100.jpg
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
train/apple/r_118_100.jpg
Normal file
After Width: | Height: | Size: 5.1 KiB |
BIN
train/apple/r_120_100.jpg
Normal file
After Width: | Height: | Size: 5.1 KiB |
BIN
train/apple/r_140_100.jpg
Normal file
After Width: | Height: | Size: 5.0 KiB |
BIN
train/apple/r_157_100.jpg
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
train/apple/r_15_100.jpg
Normal file
After Width: | Height: | Size: 5.0 KiB |
BIN
train/potato/143_100.jpg
Normal file
After Width: | Height: | Size: 4.3 KiB |
BIN
train/potato/145_100.jpg
Normal file
After Width: | Height: | Size: 4.3 KiB |
BIN
train/potato/156_100.jpg
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
train/potato/21_100.jpg
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
train/potato/r2_146_100.jpg
Normal file
After Width: | Height: | Size: 4.3 KiB |
BIN
train/potato/r2_175_100.jpg
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
train/potato/r2_9_100.jpg
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
train/potato/r_133_100.jpg
Normal file
After Width: | Height: | Size: 4.5 KiB |
BIN
train/potato/r_191_100.jpg
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
train/potato/r_77_100.jpg
Normal file
After Width: | Height: | Size: 4.4 KiB |