Skip to content

Commit

Permalink
add daddy2 snake
Browse files Browse the repository at this point in the history
  • Loading branch information
lehoangphu committed Jul 8, 2023
1 parent d984851 commit f1b738b
Show file tree
Hide file tree
Showing 10 changed files with 703 additions and 150 deletions.
Empty file added __init__.py
Empty file.
Empty file added pathfinding/__init__.py
Empty file.
31 changes: 11 additions & 20 deletions pathfinding/astarpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,9 @@
import logging
from enum import Enum

class Location:
def __init__(self, x, y) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
retString = "(" + str(self.x) + ", " + str(self.y) + ")"
return retString
def __eq__(self, object) -> bool:
if self.x == object.x and self.y == object.y:
return True
else:
return False
def __hash__(self):
return self.x*1000 + self.y
def get_distance(self, location):
return abs(self.x - location.x) + abs(self.y - location.y)

def find_astar_path(startSnake, endLoc, obstacles):
finder = AStarFinder(startSnake, endLoc, obstacles, 11, 11, 2)
return finder.findthepath()

class PathEntry:
def __init__(self, snake) -> None:
Expand Down Expand Up @@ -143,9 +129,8 @@ def basicTest1():
destination = Location(9,9)
obstacles = [
]
finder = AStarFinder(snake, destination, obstacles, 11, 11, 1)

path = finder.findthepath()
path = find_astar_path(snake, destination, obstacles)
logging.info(path)
logging.info("elapsed time: %d", (time.time() - start_time)*1000)

Expand Down Expand Up @@ -181,6 +166,12 @@ def circleSnake1():
logging.info("elapsed time: %d", (time.time() - start_time)*1000)

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
# peterpath.py is being run directly
from location import Location

# run tests
logging.basicConfig(level=logging.INFO)
circleSnake1()
else:
# peterpath.py is being imported into another script
from .location import Location
129 changes: 129 additions & 0 deletions pathfinding/findapath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
def findapath(startLoc, endLoc, obstacles):
finder = PathFinder(startLoc, endLoc, obstacles, 11, 11)
return finder.findthepath()

class PathEntry:
def __init__(self, location) -> None:
self.location = location
self.history = []
self.cost = 0

class PathFinder:
def __init__(self, startLoc, destination, obstacles, width, height) -> None:
self.data = []
self.startLoc = startLoc
self.destination = destination
self.obstacles = obstacles
self.width = width
self.height = height
self.visited = []

def addPathData(self, oldEntry, newlocation):
if (self.isValidLocation(newlocation)):
newEntry = PathEntry(newlocation)
newEntry.history = oldEntry.history.copy()
newEntry.history.append(oldEntry.location)
self.data.append(newEntry)

def findthepath(self):
startEntry = PathEntry(self.startLoc)
self.data.append(startEntry)

while len(self.data) > 0:
current = self.data.pop(0)
if current.location == self.destination:
current.history.append(self.destination)
# print("Found a path")
return current.history
break
if not self.isVisited(current.location):
self.visited.append(current.location)
uplocation = Location(current.location.x, current.location.y+1)
downlocation = Location(current.location.x, current.location.y-1)
leftlocation = Location(current.location.x-1, current.location.y)
rightlocation = Location(current.location.x+1, current.location.y)

self.addPathData(current, uplocation)
self.addPathData(current, downlocation)
self.addPathData(current, leftlocation)
self.addPathData(current, rightlocation)

def isValidLocation(self, location):
if location.x < 0 or location.x > self.width-1:
return False
if location.y < 0 or location.y > self.height-1:
return False
for obstacles in self.obstacles:
if obstacles == location:
return False
return True
def isVisited(self, newlocation):
for location in self.visited:
if location == newlocation:
return True
return False

def basicTest1():
start = {"x": 0, "y": 0}
end = {"x": 8, "y": 8}
snakes = [
{
"id": "snake-508e96ac-94ad-11ea-bb37",
"name": "My Snake",
"health": 54,
"body": [
{"x": 0, "y": 0},
{"x": 1, "y": 0},
{"x": 2, "y": 0}
],
"latency": "111",
"head": {"x": 0, "y": 0},
"length": 3,
"shout": "why are we shouting??",
"customizations":{
"color":"#FF0000",
"head":"pixel",
"tail":"pixel"
}
},
{
"id": "snake-b67f4906-94ae-11ea-bb37",
"name": "Another Snake",
"health": 16,
"body": [
{"x": 5, "y": 4},
{"x": 5, "y": 3},
{"x": 6, "y": 3},
{"x": 6, "y": 2}
],
"latency": "222",
"head": {"x": 5, "y": 4},
"length": 4,
"shout": "I'm not really sure...",
"customizations":{
"color":"#26CF04",
"head":"silly",
"tail":"curled"
}
}
]

obstacles = []
for snake in snakes:
for position in snake["body"]:
obstacles.append(Location(position["x"],position["y"]))
startLoc = Location(start["x"], start["y"])
endLoc = Location(end["x"], end["y"])

print(findapath(startLoc, endLoc, obstacles))


if __name__ == "__main__":
# peterpath.py is being run directly
from location import Location

# run tests
basicTest1()
else:
# peterpath.py is being imported into another script
from .location import Location
16 changes: 16 additions & 0 deletions pathfinding/location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Location:
def __init__(self, x, y) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
retString = "(" + str(self.x) + ", " + str(self.y) + ")"
return retString
def __eq__(self, object) -> bool:
if self.x == object.x and self.y == object.y:
return True
else:
return False
def __hash__(self):
return self.x*1000 + self.y
def get_distance(self, location):
return abs(self.x - location.x) + abs(self.y - location.y)
113 changes: 0 additions & 113 deletions pathfinding/patrickpath.py

This file was deleted.

24 changes: 8 additions & 16 deletions pathfinding/peterpath.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
class Location:
def __init__(self, x, y) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
retString = "(" + str(self.x) + ", " + str(self.y) + ")"
return retString
def __eq__(self, object) -> bool:
if self.x == object.x and self.y == object.y:
return True
else:
return False
def get_distance(self, location):
return abs(self.x - location.x) + abs(self.y - location.y)



class PathEntry:
def __init__(self, location) -> None:
self.location = location
Expand Down Expand Up @@ -95,4 +80,11 @@ def basicTest1():
finder.findthepath()

if __name__ == "__main__":
# peterpath.py is being run directly
from location import Location

# run tests
basicTest1()
else:
# peterpath.py is being imported into another script
from .location import Location
Loading

0 comments on commit f1b738b

Please sign in to comment.