Add decision trees and some refactor
340
.gitignore
vendored
Normal file
@ -0,0 +1,340 @@
|
||||
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/python,venv,intellij,pycharm
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=python,venv,intellij,pycharm
|
||||
|
||||
### Intellij ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### Intellij Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
# https://plugins.jetbrains.com/plugin/7973-sonarlint
|
||||
.idea/**/sonarlint/
|
||||
|
||||
# SonarQube Plugin
|
||||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
|
||||
.idea/**/sonarIssues.xml
|
||||
|
||||
# Markdown Navigator plugin
|
||||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
|
||||
.idea/**/markdown-navigator.xml
|
||||
.idea/**/markdown-navigator-enh.xml
|
||||
.idea/**/markdown-navigator/
|
||||
|
||||
# Cache file creation bug
|
||||
# See https://youtrack.jetbrains.com/issue/JBR-2257
|
||||
.idea/$CACHE_FILE$
|
||||
|
||||
# CodeStream plugin
|
||||
# https://plugins.jetbrains.com/plugin/12206-codestream
|
||||
.idea/codestream.xml
|
||||
|
||||
### PyCharm ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
|
||||
# Generated files
|
||||
|
||||
# Sensitive or high-churn files
|
||||
|
||||
# Gradle
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
|
||||
# Mongo Explorer plugin
|
||||
|
||||
# File-based project format
|
||||
|
||||
# IntelliJ
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
|
||||
# JIRA plugin
|
||||
|
||||
# Cursive Clojure plugin
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
|
||||
# Editor-based Rest Client
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
|
||||
### PyCharm Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
# https://plugins.jetbrains.com/plugin/7973-sonarlint
|
||||
|
||||
# SonarQube Plugin
|
||||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
|
||||
|
||||
# Markdown Navigator plugin
|
||||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
|
||||
|
||||
# Cache file creation bug
|
||||
# See https://youtrack.jetbrains.com/issue/JBR-2257
|
||||
|
||||
# CodeStream plugin
|
||||
# https://plugins.jetbrains.com/plugin/12206-codestream
|
||||
|
||||
### Python ###
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
pip-wheel-metadata/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
pytestdebug.log
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
doc/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
#poetry.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
# .env
|
||||
.env/
|
||||
.venv/
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
pythonenv*
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# operating system-related files
|
||||
# file properties cache/storage on macOS
|
||||
*.DS_Store
|
||||
# thumbnail cache on Windows
|
||||
Thumbs.db
|
||||
|
||||
# profiling data
|
||||
.prof
|
||||
|
||||
|
||||
### venv ###
|
||||
# Virtualenv
|
||||
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
|
||||
[Bb]in
|
||||
[Ii]nclude
|
||||
[Ll]ib
|
||||
[Ll]ib64
|
||||
[Ll]ocal
|
||||
[Ss]cripts
|
||||
pyvenv.cfg
|
||||
.venv
|
||||
pip-selfcheck.json
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/python,venv,intellij,pycharm
|
||||
|
||||
.idea/**
|
23
README.txt
@ -1,8 +1,15 @@
|
||||
required pygame library installed
|
||||
change img directory in main.py line 13
|
||||
|
||||
board is represented as 2D lists of square objects, where first index of the list points to the row and second to the column
|
||||
of the square(piece object) location
|
||||
|
||||
each square(piece object) contains own x and y identifiers and isSbThere atribute which stores info if detective is standing on
|
||||
piece location
|
||||
required pygame library installed
|
||||
change img directory in main.py line 13
|
||||
|
||||
board is represented as 2D lists of square objects, where first index of the list points to the row and second to the column
|
||||
of the square(piece object) location
|
||||
|
||||
each square(piece object) contains own x and y identifiers and isSbThere atribute which stores info if detective is standing on
|
||||
piece location
|
||||
|
||||
a_star algorithm:
|
||||
-goal: nearest mushroom without posion, x,y finded by bfs
|
||||
-get_cost: return summary cost of parent node and cost of current node (predictable heuristic cost and cost of field)
|
||||
-heuristic function: summary of differences between node location and goal location
|
||||
|
||||
|
||||
|
@ -1,87 +0,0 @@
|
||||
from container import board
|
||||
from container.node import Node
|
||||
from container.mushroom import Mushroom
|
||||
from container.tree import Tree
|
||||
from operator import attrgetter
|
||||
|
||||
|
||||
def a_star(board, fringe, explored, istate, successor, get_cost, goaltest):
|
||||
|
||||
agent_x = istate[0]
|
||||
agent_y = istate[1]
|
||||
agent_angle = istate[2]
|
||||
fringe: List[Node] = [Node(agent_x, agent_y, agent_angle)]
|
||||
|
||||
while fringe:
|
||||
|
||||
if not fringe:
|
||||
return False, False
|
||||
|
||||
node = min(fringe, key=lambda x: x.cost)
|
||||
fringe.remove(node)
|
||||
|
||||
if (node.x, node.y) == goaltest:
|
||||
actions = []
|
||||
while node.parent != None:
|
||||
actions.append(node.action)
|
||||
node = node.parent
|
||||
actions.reverse()
|
||||
return actions
|
||||
|
||||
explored.append(node)
|
||||
|
||||
for action, x, y, angle in successor(board, node.x, node.y, node.angle):
|
||||
next_node = Node(x, y, angle)
|
||||
next_node.parent = node
|
||||
next_node.action = action
|
||||
next_node.cost = get_cost(next_node, goaltest,board) + heuristic_function(node, goaltest)
|
||||
|
||||
if next_node not in fringe and next_node not in explored:
|
||||
fringe.append(next_node)
|
||||
else:
|
||||
for checked_node in fringe:
|
||||
if(checked_node == next_node and (next_node.cost) < (checked_node.cost)):
|
||||
fringe[fringe.index(checked_node)] = next_node
|
||||
|
||||
|
||||
|
||||
def successor(board,x,y,angle):
|
||||
|
||||
result = []
|
||||
|
||||
result.append(("rotate_left", x, y, (angle + 1) % 4))
|
||||
result.append(("rotate_right", x, y, (angle - 1) % 4))
|
||||
|
||||
if angle == 1:
|
||||
if x > 0:
|
||||
result.append(("move", x-1, y, angle))
|
||||
elif angle == 0:
|
||||
if y > 0:
|
||||
result.append(("move", x, y-1, angle))
|
||||
elif angle == 3:
|
||||
if x < board.col - 1:
|
||||
result.append(("move", x+1, y, angle))
|
||||
elif angle == 2:
|
||||
if y < board.row - 1:
|
||||
result.append(("move", x, y+1, angle))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_cost(node, goal: Mushroom, board):
|
||||
|
||||
if (node.parent.x, node.parent.y) == (node.x, node.y):
|
||||
sum = 1
|
||||
elif(isinstance((board.board[node.x][node.y]),Tree)):
|
||||
sum = 1000
|
||||
elif(isinstance((board.board[node.x][node.y]),Mushroom) and (board.board[node.x][node.y].poison) == True):
|
||||
sum = 1000
|
||||
else:
|
||||
sum = 2
|
||||
|
||||
return sum + node.parent.cost
|
||||
|
||||
|
||||
def heuristic_function(node, goal):
|
||||
return abs(node.x - goal[0]) + abs(node.y - goal[1])
|
||||
|
@ -1,25 +0,0 @@
|
||||
from container.piece import Piece
|
||||
import pygame
|
||||
|
||||
class Mushroom(Piece):
|
||||
def __init__(self,x_y, img = 0, poison = False, grow = 100):
|
||||
self.name = "Mushroom"
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.poison = poison
|
||||
self.grow = grow
|
||||
|
||||
|
||||
self.img = pygame.image.load(r'container\mushrooms\m'+str(img)+'.png')
|
||||
if poison: self.img = pygame.image.load(r'container\mushrooms\pm'+str(img)+'.png')
|
||||
|
||||
self.points = img+1
|
||||
if poison: self.points *= -1
|
||||
|
||||
def draw(self, win, square_size):
|
||||
x = (self.col+0.5*(100-self.grow)/100)*square_size[0]
|
||||
y = (self.row+0.5*(100-self.grow)/100)*square_size[1]
|
||||
win.blit(pygame.transform.scale(self.img, (int(square_size[0]/100*self.grow), int(square_size[1]/100*self.grow))), ((x, y)))
|
||||
|
||||
|
||||
|
@ -1,13 +0,0 @@
|
||||
from container.piece import Piece
|
||||
import pygame
|
||||
|
||||
class Tree(Piece):
|
||||
def __init__(self,x_y, img = 0):
|
||||
self.name = "Tree"
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.img = pygame.image.load(r'container\trees\tree'+str(img)+'.png')
|
||||
|
||||
|
||||
|
||||
|
122
core/a_star.py
Normal file
@ -0,0 +1,122 @@
|
||||
from typing import List
|
||||
|
||||
from core.mushroom import Mushroom
|
||||
from core.node import Node
|
||||
from core.tree import Tree
|
||||
|
||||
|
||||
def a_star(board, fringe, explored, istate, successor, get_cost, goaltest):
|
||||
agent_x = istate[0]
|
||||
agent_y = istate[1]
|
||||
agent_angle = istate[2]
|
||||
fringe: List[Node] = [Node(agent_x, agent_y, agent_angle)]
|
||||
|
||||
while fringe:
|
||||
|
||||
if not fringe:
|
||||
return False, False
|
||||
|
||||
node = min(fringe, key=lambda x: x.cost)
|
||||
fringe.remove(node)
|
||||
|
||||
if (node.x, node.y) == goaltest:
|
||||
actions = []
|
||||
while node.parent is not None:
|
||||
actions.append(node.action)
|
||||
node = node.parent
|
||||
actions.reverse()
|
||||
return actions
|
||||
|
||||
explored.append(node)
|
||||
|
||||
for action, x, y, angle in successor(board, node.x, node.y, node.angle):
|
||||
next_node = Node(x, y, angle)
|
||||
next_node.parent = node
|
||||
next_node.action = action
|
||||
next_node.cost = get_cost(next_node, goaltest, board) + heuristic_function(next_node, goaltest, board)
|
||||
|
||||
if next_node not in fringe and next_node not in explored:
|
||||
fringe.append(next_node)
|
||||
else:
|
||||
for checked_node in fringe:
|
||||
if checked_node == next_node and next_node.cost < checked_node.cost:
|
||||
fringe[fringe.index(checked_node)] = next_node
|
||||
|
||||
|
||||
def successor(board, x, y, angle):
|
||||
result = []
|
||||
|
||||
result.append(("rotate_left", x, y, (angle + 1) % 4))
|
||||
result.append(("rotate_right", x, y, (angle - 1) % 4))
|
||||
|
||||
if angle == 1:
|
||||
if x > 0:
|
||||
result.append(("move", x - 1, y, angle))
|
||||
elif angle == 0:
|
||||
if y > 0:
|
||||
result.append(("move", x, y - 1, angle))
|
||||
elif angle == 3:
|
||||
if x < board.col - 1:
|
||||
result.append(("move", x + 1, y, angle))
|
||||
elif angle == 2:
|
||||
if y < board.row - 1:
|
||||
result.append(("move", x, y + 1, angle))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_cost(node, goal: Mushroom, board):
|
||||
if (node.parent.x, node.parent.y) == (node.x, node.y):
|
||||
weight = 1
|
||||
elif isinstance((board.board[node.x][node.y]), Tree):
|
||||
weight = 1000
|
||||
elif isinstance((board.board[node.x][node.y]), Mushroom) and board.dt.predict(board.board[node.x][node.y]):
|
||||
weight = 1000
|
||||
else:
|
||||
weight = 2
|
||||
|
||||
return weight + node.parent.cost
|
||||
|
||||
|
||||
def heuristic_function(node, goal, board):
|
||||
try:
|
||||
return abs(node.x - goal[0]) + abs(node.y - goal[1])
|
||||
except:
|
||||
print("No more edible mushrooms on the board. Score: " + str(board.agent.points))
|
||||
exit(0)
|
||||
|
||||
|
||||
def bfs(istate, successor, board):
|
||||
fringe = []
|
||||
explored = []
|
||||
fringe_states = set()
|
||||
explored_states = set()
|
||||
|
||||
agent_x = istate[0]
|
||||
agent_y = istate[1]
|
||||
agent_angle = istate[2]
|
||||
fringe.append(Node(agent_x, agent_y, agent_angle))
|
||||
fringe_states.add((agent_x, agent_y, agent_angle))
|
||||
|
||||
while fringe:
|
||||
|
||||
if not fringe:
|
||||
return False, False
|
||||
|
||||
node = fringe.pop(0)
|
||||
fringe_states.remove((node.x, node.y, node.angle))
|
||||
|
||||
if isinstance((board.board[node.x][node.y]), Mushroom):
|
||||
if not board.dt.predict(board.board[node.x][node.y]):
|
||||
return node.x, node.y
|
||||
|
||||
explored.append(node)
|
||||
explored_states.add((node.x, node.y, node.angle))
|
||||
|
||||
for action, x, y, angle in successor(board, node.x, node.y, node.angle):
|
||||
if ((x, y, angle) not in explored_states and (x, y, angle) not in fringe_states):
|
||||
next_node = Node(x, y, angle)
|
||||
next_node.parent = node
|
||||
next_node.action = action
|
||||
fringe.append(next_node)
|
||||
fringe_states.add((x, y, angle))
|
@ -1,96 +1,104 @@
|
||||
import pygame
|
||||
from container.piece import Piece
|
||||
|
||||
|
||||
class Agent(Piece):
|
||||
def __init__(self,x_y, square_size):
|
||||
self.name = "Agent"
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.angle = 0
|
||||
self.moving = 0
|
||||
self.rotating = 0
|
||||
self.move_to = (self.row, self.col)
|
||||
|
||||
self.points = 0
|
||||
self.img = pygame.image.load(r'container\new_detective.png')
|
||||
self.square_size = square_size
|
||||
self.img = pygame.transform.scale(self.img, (int(self.square_size[0]), int(self.square_size[1])))
|
||||
|
||||
|
||||
def draw(self, win):
|
||||
x = (self.col + 0.5 + (self.move_to[0] - self.col)/10*self.moving)*self.square_size[0]
|
||||
y = (self.row + 0.5 + (self.move_to[1] - self.row)/10*self.moving)*self.square_size[1]
|
||||
|
||||
#win.blit(self.img, (x, y))
|
||||
|
||||
self.blitRotate(win, self.img, (x, y), (self.square_size[0]/2, self.square_size[1]/2), self.angle*90 + self.rotating*90/10)
|
||||
|
||||
def rotate(self, angle):
|
||||
if self.moving == self.rotating == 0:
|
||||
self.rotating += angle
|
||||
|
||||
def move(self):
|
||||
if self.moving == self.rotating == 0:
|
||||
move_to = (0,-1)
|
||||
if self.angle == 1: move_to = (-1,0)
|
||||
if self.angle == 2: move_to = (0,1)
|
||||
if self.angle == 3: move_to = (1,0)
|
||||
|
||||
self.move_to = (self.col + move_to[0], self.row + move_to[1])
|
||||
|
||||
self.moving += 1
|
||||
|
||||
def update_animation(self):
|
||||
if self.moving > 0:
|
||||
self.moving += 1
|
||||
if self.moving >= 10:
|
||||
self.moving = 0
|
||||
self.col, self.row = self.move_to
|
||||
|
||||
if self.rotating > 0:
|
||||
self.rotating += 1
|
||||
if self.rotating >= 10:
|
||||
self.rotating = 0
|
||||
self.angle = (self.angle + 1) % 4
|
||||
if self.rotating < 0:
|
||||
self.rotating -= 1
|
||||
if self.rotating <= -10:
|
||||
self.rotating = 0
|
||||
self.angle = (self.angle - 1) % 4
|
||||
|
||||
|
||||
def blitRotate(self, surf, image, pos, originPos, angle):
|
||||
|
||||
# calcaulate the axis aligned bounding box of the rotated image
|
||||
w, h = image.get_size()
|
||||
box = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
|
||||
box_rotate = [p.rotate(angle) for p in box]
|
||||
min_box = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
|
||||
max_box = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])
|
||||
|
||||
# calculate the translation of the pivot
|
||||
pivot = pygame.math.Vector2(originPos[0], -originPos[1])
|
||||
pivot_rotate = pivot.rotate(angle)
|
||||
pivot_move = pivot_rotate - pivot
|
||||
|
||||
# calculate the upper left origin of the rotated image
|
||||
origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])
|
||||
|
||||
# get a rotated image
|
||||
rotated_image = pygame.transform.rotate(image, angle)
|
||||
|
||||
# rotate and blit the image
|
||||
surf.blit(rotated_image, origin)
|
||||
|
||||
# draw rectangle around the image
|
||||
#pygame.draw.rect(surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import os
|
||||
|
||||
import pygame
|
||||
from core.piece import Piece
|
||||
|
||||
|
||||
class Agent(Piece):
|
||||
def __init__(self,x_y, square_size):
|
||||
self.name = "Agent"
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.angle = 0
|
||||
self.current_angle = 0
|
||||
self.moving = 0
|
||||
self.rotating = 0
|
||||
self.move_to = (self.row, self.col)
|
||||
|
||||
self.points = 0
|
||||
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep +
|
||||
'agent' + os.path.sep + 'new_detective.png')
|
||||
self.square_size = square_size
|
||||
self.img = pygame.transform.scale(self.img, (int(self.square_size[0]), int(self.square_size[1])))
|
||||
|
||||
|
||||
|
||||
|
||||
def draw(self, win):
|
||||
|
||||
x = (self.col + 0.5 + (self.move_to[0] - self.col)/10*self.moving)*self.square_size[0]
|
||||
y = (self.row + 0.5 + (self.move_to[1] - self.row)/10*self.moving)*self.square_size[1]
|
||||
|
||||
#win.blit(self.img, (x, y))
|
||||
|
||||
self.blitRotate(win, self.img, (x, y), (self.square_size[0]/2, self.square_size[1]/2), self.angle*90 + self.rotating*90/10)
|
||||
|
||||
def rotate(self, angle):
|
||||
if self.moving == self.rotating == 0:
|
||||
self.rotating += angle
|
||||
#print(self.rotating)
|
||||
|
||||
def move(self):
|
||||
if self.moving == self.rotating == 0:
|
||||
move_to = (0,-1)
|
||||
if self.angle == 1: move_to = (-1,0)
|
||||
if self.angle == 2: move_to = (0,1)
|
||||
if self.angle == 3: move_to = (1,0)
|
||||
|
||||
self.move_to = (self.col + move_to[0], self.row + move_to[1])
|
||||
|
||||
self.moving += 1
|
||||
|
||||
def update_animation(self):
|
||||
if self.moving > 0:
|
||||
self.moving += 1
|
||||
if self.moving >= 10:
|
||||
self.moving = 0
|
||||
self.col, self.row = self.move_to
|
||||
|
||||
if self.rotating > 0:
|
||||
self.rotating += 1
|
||||
if self.rotating >= 10:
|
||||
self.rotating = 0
|
||||
self.angle = (self.angle + 1) % 4
|
||||
if self.rotating < 0:
|
||||
self.rotating -= 1
|
||||
if self.rotating <= -10:
|
||||
self.rotating = 0
|
||||
self.angle = (self.angle - 1) % 4
|
||||
|
||||
|
||||
def blitRotate(self, surf, image, pos, originPos, angle):
|
||||
|
||||
# calcaulate the axis aligned bounding box of the rotated image
|
||||
w, h = image.get_size()
|
||||
box = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
|
||||
box_rotate = [p.rotate(angle) for p in box]
|
||||
min_box = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
|
||||
max_box = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])
|
||||
|
||||
# calculate the translation of the pivot
|
||||
pivot = pygame.math.Vector2(originPos[0], -originPos[1])
|
||||
pivot_rotate = pivot.rotate(angle)
|
||||
pivot_move = pivot_rotate - pivot
|
||||
|
||||
# calculate the upper left origin of the rotated image
|
||||
origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])
|
||||
|
||||
# get a rotated image
|
||||
rotated_image = pygame.transform.rotate(image, angle)
|
||||
|
||||
# rotate and blit the image
|
||||
surf.blit(rotated_image, origin)
|
||||
|
||||
# draw rectangle around the image
|
||||
#pygame.draw.rect(surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,156 +1,158 @@
|
||||
import pygame
|
||||
import random
|
||||
|
||||
from container.constans import *
|
||||
from container.tree import Tree
|
||||
from container.mushroom import Mushroom
|
||||
from container.agent import Agent
|
||||
from container.a_star import a_star,successor, get_cost
|
||||
|
||||
class Board:
|
||||
def __init__(self, x = 0, y = 0, width = WIDTH, height = HEIGHT, row = ROWS, col = COLUMNS):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.row = row
|
||||
self.col = col
|
||||
self.square_size = (self.width / self.col, self.height / self.row)
|
||||
self.board = [[False for j in range(self.row)] for i in range(self.col)]
|
||||
self.free_spaces = []
|
||||
self.update_free_spaces()
|
||||
|
||||
self.agent = Agent(self.free_spaces.pop(), self.square_size)
|
||||
|
||||
print(f"Board {col}x{row} with agent on ({self.agent.col},{self.agent.row}) {self.agent.angle}")
|
||||
|
||||
self.surface = pygame.Surface((self.width, self.height))
|
||||
|
||||
for i in range(TREES):
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Tree(x_y, random.randint(0, 8))
|
||||
|
||||
|
||||
for i in range(MUSHROOMS_START):
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
|
||||
|
||||
for i in range(POISON_MUSHROOMS_START):
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True)
|
||||
|
||||
|
||||
self.grow_next = 0
|
||||
|
||||
self.path = []
|
||||
self.actions = []
|
||||
|
||||
def update_free_spaces(self):
|
||||
self.free_spaces = []
|
||||
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] == False: self.free_spaces.append((x,y))
|
||||
|
||||
random.shuffle(self.free_spaces)
|
||||
|
||||
def draw_squares(self,win):
|
||||
self.surface.fill(GREEN_2)
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if (x+y) % 2 == 0:
|
||||
pygame.draw.rect(self.surface,GREEN,(x*self.square_size[0], y*self.square_size[1], self.square_size[0], self.square_size[1]))
|
||||
|
||||
win.blit(self.surface, (self.x, self.y))
|
||||
|
||||
def draw_agent(self,win):
|
||||
self.agent.draw(self.surface)
|
||||
win.blit(self.surface, (self.x, self.y))
|
||||
|
||||
def draw_pieces(self,win):
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] != False:
|
||||
self.board[x][y].draw(self.surface, self.square_size)
|
||||
win.blit(self.surface, (self.x, self.y))
|
||||
|
||||
def draw_info(self,win):
|
||||
myfont = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
textsurface = myfont.render(str(self.agent.points), False, (0, 0, 0))
|
||||
win.blit(textsurface,(self.x+self.width-textsurface.get_width()-5,self.y-40))
|
||||
|
||||
# kolumna agneta
|
||||
textsurface = myfont.render("c: "+str(self.agent.col), False, (0, 0, 0))
|
||||
win.blit(textsurface,(self.x+1,self.y-40))
|
||||
|
||||
# wiersz agneta
|
||||
textsurface = myfont.render("r: "+str(self.agent.row), False, (0, 0, 0))
|
||||
win.blit(textsurface,(self.x+100,self.y-40))
|
||||
|
||||
# kierunek agenta
|
||||
textsurface = myfont.render("a: "+str(self.agent.angle), False, (0, 0, 0))
|
||||
win.blit(textsurface,(self.x+200,self.y-40))
|
||||
|
||||
|
||||
|
||||
def update_agent(self):
|
||||
self.agent.update_animation()
|
||||
|
||||
|
||||
if self.actions and self.agent.moving == self.agent.rotating == 0:
|
||||
action = self.actions.pop(0)
|
||||
|
||||
if action == "move": self.agent.move()
|
||||
elif action == "rotate_left": self.agent.rotate(1)
|
||||
else: self.agent.rotate(-1)
|
||||
|
||||
|
||||
#zebranie grzyba
|
||||
if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][self.agent.row].name == "Mushroom" and self.board[self.agent.col][self.agent.row].grow == 100:
|
||||
self.agent.points += self.board[self.agent.col][self.agent.row].points
|
||||
self.board[self.agent.col][self.agent.row] = False
|
||||
self.free_spaces.append((self.agent.col, self.agent.row))
|
||||
|
||||
def grow_mushrooms(self):
|
||||
numer = 0
|
||||
numer_poison = 0
|
||||
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] != False and self.board[x][y].name == "Mushroom":
|
||||
if self.board[x][y].grow < 100: self.board[x][y].grow += 1
|
||||
|
||||
if self.board[x][y].poison: numer_poison += 1
|
||||
else: numer += 1
|
||||
|
||||
if self.grow_next < 100: self.grow_next += 1
|
||||
elif random.random() > 0.5 and numer < MUSHROOMS_MAX:
|
||||
self.grow_next = 0
|
||||
self.update_free_spaces()
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), False, 0)
|
||||
elif numer_poison < POISON_MUSHROOMS_MAX:
|
||||
self.grow_next = 0
|
||||
self.update_free_spaces()
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True, 0)
|
||||
|
||||
def a_starxd(self):
|
||||
print(self.agent.angle)
|
||||
fringe = []
|
||||
explored = []
|
||||
self.actions = a_star(self,
|
||||
fringe,
|
||||
explored,
|
||||
(self.agent.col,self.agent.row, self.agent.angle),
|
||||
successor,
|
||||
get_cost,
|
||||
(bfs((self.agent.col,self.agent.row,self.agent.angle),successor,self))
|
||||
)
|
||||
|
||||
print(self.actions)
|
||||
|
||||
return self.actions
|
||||
|
||||
|
||||
|
||||
import random
|
||||
|
||||
from core.constans import *
|
||||
from core.decision_tree import DecisionTree
|
||||
from core.tree import Tree
|
||||
from core.mushroom import Mushroom
|
||||
from core.agent import Agent
|
||||
from core.a_star import a_star, bfs, successor, get_cost
|
||||
|
||||
|
||||
class Board:
|
||||
def __init__(self, x=0, y=0, width=WIDTH, height=HEIGHT, row=ROWS, col=COLUMNS):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.row = row
|
||||
self.col = col
|
||||
self.square_size = (self.width / self.col, self.height / self.row)
|
||||
self.board = [[False for j in range(self.row)] for i in range(self.col)]
|
||||
self.free_spaces = []
|
||||
self.update_free_spaces()
|
||||
self.dt = DecisionTree(0.05)
|
||||
self.dt.learn()
|
||||
|
||||
self.agent = Agent(self.free_spaces.pop(), self.square_size)
|
||||
|
||||
print(f"Board {col}x{row} with agent on ({self.agent.col},{self.agent.row}) {self.agent.angle}")
|
||||
|
||||
self.surface = pygame.Surface((self.width, self.height))
|
||||
|
||||
for i in range(TREES):
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Tree(x_y, random.randint(0, 8))
|
||||
|
||||
for i in range(MUSHROOMS_START):
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
|
||||
|
||||
for i in range(POISON_MUSHROOMS_START):
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
|
||||
|
||||
self.grow_next = 0
|
||||
|
||||
self.path = []
|
||||
self.actions = []
|
||||
|
||||
def update_free_spaces(self):
|
||||
self.free_spaces = []
|
||||
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] == False: self.free_spaces.append((x, y))
|
||||
|
||||
random.shuffle(self.free_spaces)
|
||||
|
||||
def draw_squares(self, win):
|
||||
self.surface.fill(GREEN_2)
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if (x + y) % 2 == 0:
|
||||
pygame.draw.rect(self.surface, GREEN, (
|
||||
x * self.square_size[0], y * self.square_size[1], self.square_size[0], self.square_size[1]))
|
||||
|
||||
win.blit(self.surface, (self.x, self.y))
|
||||
|
||||
def draw_agent(self, win):
|
||||
self.agent.draw(self.surface)
|
||||
win.blit(self.surface, (self.x, self.y))
|
||||
|
||||
def draw_pieces(self, win):
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] != False:
|
||||
self.board[x][y].draw(self.surface, self.square_size)
|
||||
win.blit(self.surface, (self.x, self.y))
|
||||
|
||||
def draw_info(self, win):
|
||||
myfont = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
textsurface = myfont.render(str(self.agent.points), False, (0, 0, 0))
|
||||
win.blit(textsurface, (self.x + self.width - textsurface.get_width() - 5, self.y - 40))
|
||||
|
||||
# kolumna agneta
|
||||
textsurface = myfont.render("c: " + str(self.agent.col), False, (0, 0, 0))
|
||||
win.blit(textsurface, (self.x + 1, self.y - 40))
|
||||
|
||||
# wiersz agneta
|
||||
textsurface = myfont.render("r: " + str(self.agent.row), False, (0, 0, 0))
|
||||
win.blit(textsurface, (self.x + 100, self.y - 40))
|
||||
|
||||
# kierunek agenta
|
||||
textsurface = myfont.render("a: " + str(self.agent.angle), False, (0, 0, 0))
|
||||
win.blit(textsurface, (self.x + 200, self.y - 40))
|
||||
|
||||
def update_agent(self):
|
||||
self.agent.update_animation()
|
||||
|
||||
if self.actions and self.agent.moving == self.agent.rotating == 0:
|
||||
action = self.actions.pop(0)
|
||||
|
||||
if action == "move":
|
||||
self.agent.move()
|
||||
elif action == "rotate_left":
|
||||
self.agent.rotate(1)
|
||||
else:
|
||||
self.agent.rotate(-1)
|
||||
|
||||
# zebranie grzyba
|
||||
if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][
|
||||
self.agent.row].name == "Mushroom" and self.board[self.agent.col][self.agent.row].grow == 100:
|
||||
self.agent.points += self.board[self.agent.col][self.agent.row].points
|
||||
self.board[self.agent.col][self.agent.row] = False
|
||||
self.free_spaces.append((self.agent.col, self.agent.row))
|
||||
|
||||
def grow_mushrooms(self):
|
||||
numer = 0
|
||||
# numer_poison = 0
|
||||
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] != False and self.board[x][y].name == "Mushroom":
|
||||
if self.board[x][y].grow < 100: self.board[x][y].grow += 1
|
||||
|
||||
# if self.board[x][y].poison:
|
||||
# numer_poison += 1
|
||||
# else:
|
||||
numer += 1
|
||||
|
||||
if self.grow_next < 100:
|
||||
self.grow_next += 1
|
||||
elif random.random() > 0.5 and numer < MUSHROOMS_MAX:
|
||||
self.grow_next = 0
|
||||
self.update_free_spaces()
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), 0)
|
||||
# elif numer_poison < POISON_MUSHROOMS_MAX:
|
||||
# self.grow_next = 0
|
||||
# self.update_free_spaces()
|
||||
# x_y = self.free_spaces.pop()
|
||||
# self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), 0)
|
||||
|
||||
def a_starxd(self):
|
||||
print(self.agent.angle)
|
||||
fringe = []
|
||||
explored = []
|
||||
self.actions = a_star(self,
|
||||
fringe,
|
||||
explored,
|
||||
(self.agent.col, self.agent.row, self.agent.angle),
|
||||
successor,
|
||||
get_cost,
|
||||
(bfs((self.agent.col, self.agent.row, self.agent.angle), successor, self))
|
||||
)
|
||||
|
||||
print(self.actions)
|
||||
|
||||
return self.actions
|
@ -1,21 +1,21 @@
|
||||
import pygame
|
||||
|
||||
#board size variables
|
||||
WIDTH = 1000
|
||||
HEIGHT = 700
|
||||
COLUMNS = 20
|
||||
ROWS = 14
|
||||
|
||||
#number of mushrooms
|
||||
|
||||
MUSHROOMS_START = 5
|
||||
MUSHROOMS_MAX = 10
|
||||
POISON_MUSHROOMS_START = 5
|
||||
POISON_MUSHROOMS_MAX = 40
|
||||
TREES = 80
|
||||
|
||||
|
||||
#colors in game in rgb
|
||||
GREEN = (0,230,0)
|
||||
GREEN_2 = (0,200,0)
|
||||
WHITE = (255,255,255)
|
||||
import pygame
|
||||
|
||||
#board size variables
|
||||
WIDTH = 1000
|
||||
HEIGHT = 700
|
||||
COLUMNS = 20
|
||||
ROWS = 14
|
||||
|
||||
#amount of mushrooms on the board
|
||||
|
||||
MUSHROOMS_START = 5
|
||||
MUSHROOMS_MAX = 10
|
||||
POISON_MUSHROOMS_START = 5
|
||||
POISON_MUSHROOMS_MAX = 40
|
||||
TREES = 80
|
||||
|
||||
|
||||
#colors of board in game in rgb
|
||||
GREEN = (0,230,0)
|
||||
GREEN_2 = (0,200,0)
|
||||
#WHITE = (255,255,255)
|
32
core/decision_tree.py
Normal file
@ -0,0 +1,32 @@
|
||||
import os
|
||||
|
||||
import pandas as pd
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
|
||||
class DecisionTree:
|
||||
def __init__(self, precision):
|
||||
self.model = DecisionTreeClassifier(criterion='gini')
|
||||
self.learningDataSize = precision
|
||||
|
||||
def learn(self):
|
||||
dataset = pd.read_csv(r'core' + os.path.sep + 'resources' + os.path.sep + 'data' +
|
||||
os.path.sep + 'data_tree.csv')
|
||||
input_x = dataset.iloc[:, 0:8]
|
||||
input_y = dataset.iloc[:, 8]
|
||||
|
||||
x_train, x_test, y_train, y_test = train_test_split(input_x, input_y, test_size=(1-self.learningDataSize))
|
||||
self.model.fit(x_train, y_train)
|
||||
|
||||
def predict(self, mushroom):
|
||||
return bool(self.model.predict([[
|
||||
mushroom.isRed,
|
||||
mushroom.hasRing,
|
||||
mushroom.hasEmptyCore,
|
||||
mushroom.hasDots,
|
||||
mushroom.hasLamella,
|
||||
mushroom.isShiftingColor,
|
||||
mushroom.hasUnpleasantSmell,
|
||||
mushroom.hasSlime,
|
||||
]])[0])
|
42
core/mushroom.py
Normal file
@ -0,0 +1,42 @@
|
||||
import os
|
||||
import random
|
||||
|
||||
from core.piece import Piece
|
||||
import pygame
|
||||
|
||||
|
||||
class Mushroom(Piece):
|
||||
def __init__(self, x_y, img=0, grow=100):
|
||||
self.name = "Mushroom"
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.grow = grow
|
||||
|
||||
self.isRed = bool(random.getrandbits(1))
|
||||
self.hasRing = bool(random.getrandbits(1))
|
||||
self.hasEmptyCore = bool(random.getrandbits(1))
|
||||
self.hasDots = bool(random.getrandbits(1))
|
||||
self.hasLamella = bool(random.getrandbits(1))
|
||||
self.isShiftingColor = bool(random.getrandbits(1))
|
||||
self.hasUnpleasantSmell = bool(random.getrandbits(1))
|
||||
self.hasSlime = bool(random.getrandbits(1))
|
||||
|
||||
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep + 'mushrooms' +
|
||||
os.path.sep + 'm' + str(img) + '.png')
|
||||
|
||||
poison = (int(self.isRed) + int(self.hasRing) + int(self.hasEmptyCore) + int(self.hasDots) + int(
|
||||
self.hasLamella) + int(self.isShiftingColor) + int(self.hasUnpleasantSmell) + int(self.hasSlime)) > 4
|
||||
if poison:
|
||||
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep + 'mushrooms' +
|
||||
os.path.sep + 'pm' + str(img) + '.png')
|
||||
|
||||
self.points = img + 1
|
||||
if poison:
|
||||
self.points *= -1
|
||||
|
||||
def draw(self, win, square_size):
|
||||
x = (self.col + 0.5 * (100 - self.grow) / 100) * square_size[0]
|
||||
y = (self.row + 0.5 * (100 - self.grow) / 100) * square_size[1]
|
||||
win.blit(pygame.transform.scale(self.img,
|
||||
(int(square_size[0] / 100 * self.grow), int(square_size[1] / 100 * self.grow))),
|
||||
(x, y))
|
@ -1,15 +1,15 @@
|
||||
|
||||
class Node:
|
||||
def __init__(self, x, y, angle = None, parent = None, action = 0, cost=0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.angle = angle
|
||||
self.parent = parent
|
||||
self.action = action
|
||||
self.cost = cost
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Node) and (self.x == other.x and self.y == other.y and self.angle == other.angle):
|
||||
return True
|
||||
else:
|
||||
|
||||
class Node:
|
||||
def __init__(self, x, y, angle = None, parent = None, action = 0, cost=0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.angle = angle
|
||||
self.parent = parent
|
||||
self.action = action
|
||||
self.cost = cost
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Node) and (self.x == other.x and self.y == other.y and self.angle == other.angle):
|
||||
return True
|
||||
else:
|
||||
return False
|
@ -1,15 +1,18 @@
|
||||
import pygame
|
||||
|
||||
class Piece:
|
||||
|
||||
def __init__(self,x_y):
|
||||
self.name = ""
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.img = ''
|
||||
|
||||
def draw(self, win, square_size):
|
||||
win.blit(pygame.transform.scale(self.img, (int(square_size[0]), int(square_size[1]))), (self.col*square_size[0], self.row*square_size[1]))
|
||||
|
||||
|
||||
|
||||
import pygame
|
||||
|
||||
class Piece:
|
||||
|
||||
def __init__(self,x_y,weight = 0 ,mushroom = 0):
|
||||
self.name = ""
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.img = ''
|
||||
self.weight = weight
|
||||
self.mushroom = mushroom
|
||||
|
||||
|
||||
def draw(self, win, square_size):
|
||||
win.blit(pygame.transform.scale(self.img, (int(square_size[0]), int(square_size[1]))), (self.col*square_size[0], self.row*square_size[1]))
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
257
core/resources/data/data_tree.csv
Normal file
@ -0,0 +1,257 @@
|
||||
Czy jest czerwony,Czy ma pierścień,Czy ma pusty trzpień,Czy ma kropki,Czy ma blaszki,Czy zmienia kolor po przełamaniu,Czy ma nieprzyjemny zapach,Czy ma śluz,Czy jest trujący
|
||||
0,0,0,0,0,0,0,0,0
|
||||
1,0,0,0,0,0,0,0,0
|
||||
0,1,0,0,0,0,0,0,0
|
||||
1,1,0,0,0,0,0,0,0
|
||||
0,0,1,0,0,0,0,0,0
|
||||
1,0,1,0,0,0,0,0,0
|
||||
0,1,1,0,0,0,0,0,0
|
||||
1,1,1,0,0,0,0,0,0
|
||||
0,0,0,1,0,0,0,0,0
|
||||
1,0,0,1,0,0,0,0,0
|
||||
0,1,0,1,0,0,0,0,0
|
||||
1,1,0,1,0,0,0,0,0
|
||||
0,0,1,1,0,0,0,0,0
|
||||
1,0,1,1,0,0,0,0,0
|
||||
0,1,1,1,0,0,0,0,0
|
||||
1,1,1,1,0,0,0,0,0
|
||||
0,0,0,0,1,0,0,0,0
|
||||
1,0,0,0,1,0,0,0,0
|
||||
0,1,0,0,1,0,0,0,0
|
||||
1,1,0,0,1,0,0,0,0
|
||||
0,0,1,0,1,0,0,0,0
|
||||
1,0,1,0,1,0,0,0,0
|
||||
0,1,1,0,1,0,0,0,0
|
||||
1,1,1,0,1,0,0,0,0
|
||||
0,0,0,1,1,0,0,0,0
|
||||
1,0,0,1,1,0,0,0,0
|
||||
0,1,0,1,1,0,0,0,0
|
||||
1,1,0,1,1,0,0,0,0
|
||||
0,0,1,1,1,0,0,0,0
|
||||
1,0,1,1,1,0,0,0,0
|
||||
0,1,1,1,1,0,0,0,0
|
||||
1,1,1,1,1,0,0,0,1
|
||||
0,0,0,0,0,1,0,0,0
|
||||
1,0,0,0,0,1,0,0,0
|
||||
0,1,0,0,0,1,0,0,0
|
||||
1,1,0,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1,0,1,1,1
|
||||
0,1,1,0,1,0,1,1,1
|
||||
1,1,1,0,1,0,1,1,1
|
||||
0,0,0,1,1,0,1,1,0
|
||||
1,0,0,1,1,0,1,1,1
|
||||
0,1,0,1,1,0,1,1,1
|
||||
1,1,0,1,1,0,1,1,1
|
||||
0,0,1,1,1,0,1,1,1
|
||||
1,0,1,1,1,0,1,1,1
|
||||
0,1,1,1,1,0,1,1,1
|
||||
1,1,1,1,1,0,1,1,1
|
||||
0,0,0,0,0,1,1,1,0
|
||||
1,0,0,0,0,1,1,1,0
|
||||
0,1,0,0,0,1,1,1,0
|
||||
1,1,0,0,0,1,1,1,1
|
||||
0,0,1,0,0,1,1,1,0
|
||||
1,0,1,0,0,1,1,1,1
|
||||
0,1,1,0,0,1,1,1,1
|
||||
1,1,1,0,0,1,1,1,1
|
||||
0,0,0,1,0,1,1,1,0
|
||||
1,0,0,1,0,1,1,1,1
|
||||
0,1,0,1,0,1,1,1,1
|
||||
1,1,0,1,0,1,1,1,1
|
||||
0,0,1,1,0,1,1,1,1
|
||||
1,0,1,1,0,1,1,1,1
|
||||
0,1,1,1,0,1,1,1,1
|
||||
1,1,1,1,0,1,1,1,1
|
||||
0,0,0,0,1,1,1,1,0
|
||||
1,0,0,0,1,1,1,1,1
|
||||
0,1,0,0,1,1,1,1,1
|
||||
1,1,0,0,1,1,1,1,1
|
||||
0,0,1,0,1,1,1,1,1
|
||||
1,0,1,0,1,1,1,1,1
|
||||
0,1,1,0,1,1,1,1,1
|
||||
1,1,1,0,1,1,1,1,1
|
||||
0,0,0,1,1,1,1,1,1
|
||||
1,0,0,1,1,1,1,1,1
|
||||
0,1,0,1,1,1,1,1,1
|
||||
1,1,0,1,1,1,1,1,1
|
||||
0,0,1,1,1,1,1,1,1
|
||||
1,0,1,1,1,1,1,1,1
|
||||
0,1,1,1,1,1,1,1,1
|
||||
1,1,1,1,1,1,1,1,1
|
|
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 9.9 KiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 9.1 KiB After Width: | Height: | Size: 9.1 KiB |
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
15
core/tree.py
Normal file
@ -0,0 +1,15 @@
|
||||
import os
|
||||
|
||||
import pygame
|
||||
|
||||
from core.piece import Piece
|
||||
|
||||
|
||||
class Tree(Piece):
|
||||
def __init__(self, x_y, img=0):
|
||||
super().__init__(x_y)
|
||||
self.name = "Tree"
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep + 'trees' +
|
||||
os.path.sep + 'tree' + str(img) + '.png')
|
129
main.py
@ -1,71 +1,58 @@
|
||||
import pygame
|
||||
from container.constans import WIDTH, HEIGHT, ROWS, COLUMNS, GREEN
|
||||
from container.board import Board
|
||||
|
||||
|
||||
FPS = 18
|
||||
|
||||
|
||||
#creating game window
|
||||
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
|
||||
#setting name
|
||||
pygame.display.set_caption('Forest')
|
||||
pygame.font.init()
|
||||
|
||||
#detective = pygame.image.load(r'container\detective.png')
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
run = True
|
||||
clock = pygame.time.Clock() #for fps
|
||||
board = Board(0,40,WIDTH,HEIGHT-40)
|
||||
|
||||
|
||||
while run:
|
||||
clock.tick(FPS)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
|
||||
|
||||
#managing arrow click
|
||||
|
||||
key_input = pygame.key.get_pressed()
|
||||
if key_input[pygame.K_LEFT]: board.agent.rotate(1)
|
||||
if key_input[pygame.K_UP]: board.agent.move()
|
||||
if key_input[pygame.K_RIGHT]: board.agent.rotate(-1)
|
||||
|
||||
|
||||
|
||||
#drawing map and detective
|
||||
WIN.fill(GREEN)
|
||||
board.update_agent() #update moves and collecting mushrooms
|
||||
board.grow_mushrooms()
|
||||
|
||||
board.draw_squares(WIN)
|
||||
board.draw_pieces(WIN)
|
||||
board.draw_agent(WIN)
|
||||
board.draw_info(WIN)
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
if key_input[pygame.K_SPACE]:
|
||||
board.a_starxd()
|
||||
|
||||
if(board.agent.rotating==0 and board.agent.moving==0):
|
||||
board.a_starxd()
|
||||
|
||||
|
||||
|
||||
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import pygame
|
||||
|
||||
from core.board import Board
|
||||
from core.constans import WIDTH, HEIGHT, GREEN
|
||||
|
||||
from core.decision_tree import DecisionTree
|
||||
from core.mushroom import Mushroom
|
||||
|
||||
FPS = 40
|
||||
|
||||
# creating game window
|
||||
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
# setting name
|
||||
pygame.display.set_caption('Forest')
|
||||
pygame.font.init()
|
||||
|
||||
# detective = pygame.image.load(r'core\detective.png')
|
||||
|
||||
|
||||
def main():
|
||||
run = True
|
||||
clock = pygame.time.Clock() # for fps
|
||||
board = Board(0, 40, WIDTH, HEIGHT - 40)
|
||||
|
||||
while run:
|
||||
clock.tick(FPS)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
|
||||
# managing arrow click
|
||||
key_input = pygame.key.get_pressed()
|
||||
if key_input[pygame.K_LEFT]: board.agent.rotate(1)
|
||||
if key_input[pygame.K_UP]: board.agent.move()
|
||||
if key_input[pygame.K_RIGHT]: board.agent.rotate(-1)
|
||||
|
||||
# drawing map and detective
|
||||
WIN.fill(GREEN)
|
||||
board.update_agent() # update moves and collecting mushrooms
|
||||
board.grow_mushrooms()
|
||||
|
||||
board.draw_squares(WIN)
|
||||
board.draw_pieces(WIN)
|
||||
board.draw_agent(WIN)
|
||||
board.draw_info(WIN)
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
if key_input[pygame.K_SPACE]:
|
||||
board.a_starxd()
|
||||
|
||||
if board.agent.rotating == 0 and board.agent.moving == 0:
|
||||
board.a_starxd()
|
||||
pygame.quit()
|
||||
|
||||
|
||||
main()
|
||||
|
10
requirements.txt
Normal file
@ -0,0 +1,10 @@
|
||||
joblib==1.0.1
|
||||
numpy==1.20.3
|
||||
pandas==1.2.4
|
||||
pygame==2.0.1
|
||||
python-dateutil==2.8.1
|
||||
pytz==2021.1
|
||||
scikit-learn==0.24.2
|
||||
scipy==1.6.3
|
||||
six==1.16.0
|
||||
threadpoolctl==2.1.0
|