Skip to content

Commit

Permalink
looks good 🐷
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-atul committed Nov 6, 2020
1 parent f030fb7 commit 38ab256
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# SnakeAI
AI for Snake game using A star algorithm
AI for Snake game using A star algorithm.

Works right in the terminal. Uses Astar to find the route

```
Astar:
fn = gn+ hn
here,
gn is the total moves taken for a food
hn is the manhattan distance
```

Customize the game values with the ```constant.py``` file

## Execution
1. Clone the repo
```
$ https://github.com/AP-Atul/SnakeAI.git
```
2. Install the curses package
```
$ pip install curses
```
3. Run the main file in the terminal
```
$ python main.py
```
33 changes: 18 additions & 15 deletions astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ def __init__(self):
KEY_RIGHT: KEY_LEFT
}

def getMoves(self, direction):
paths = self.paths.copy()
paths.remove(direction)
return paths
self.moves = 0

def getDistances(self, paths, goal, current):
def sortDictionary(self, x):
return {k: v for k, v in sorted(x.items(), key=lambda item: item[1])}

def collides(self, headPosition, snake):
return any([body.position == headPosition for body in snake.body[: -1]])

def getDistances(self, goal, current, snake):
distances = dict()
self.moves += 1

for path in paths:
for path in self.paths:
x = None
y = None
goal_x = goal.x
Expand All @@ -46,21 +50,20 @@ def getDistances(self, paths, goal, current):
x = current.x - 1
y = current.y

distances[path] = abs(x - goal_x) + abs(y - goal_y)
if self.collides((x, y), snake):
continue

return distances
distances[path] = self.moves + abs(x - goal_x) + abs(y - goal_y)

def sortDictionary(self, x):
return {k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
return distances

def getKey(self, food, snake):
if snake.head.x == food.x and snake.head.y:
self.moves = 0
return snake.direction

currentDirection = snake.direction
availablePaths = self.getMoves(currentDirection)

distances = self.getDistances(availablePaths, food, snake.head)
distances = self.getDistances(food, snake.head, snake)
distances = self.sortDictionary(distances)
distances = list(distances.keys())

return list(distances.keys())[0]
return distances[0]
11 changes: 6 additions & 5 deletions components.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def position(self):

class Food(object):
def __init__(self, window, char=FOOD_CHAR):
self.x = randint(10, MAX_X)
self.y = randint(10, MAX_Y)
self.x = randint(10, MAX_X - 10)
self.y = randint(10, MAX_Y - 10)
self.char = char
self.window = window

def render(self):
self.window.addstr(self.y, self.x, self.char)

def reset(self):
self.x = randint(10, MAX_X)
self.y = randint(10, MAX_Y)
self.x = randint(10, MAX_X - 10)
self.y = randint(10, MAX_Y - 10)


class Snake(object):
Expand Down Expand Up @@ -70,7 +70,8 @@ def eatFood(self, food: Food):
self.score += 1

if self.score % 3 == 0:
self.timeout -= 5
if self.timeout > 20:
self.timeout -= 5
self.window.timeout(self.timeout)

@property
Expand Down
Binary file added img/snake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 38ab256

Please sign in to comment.