-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_only_w_wrap.py
44 lines (40 loc) · 1.9 KB
/
agent_only_w_wrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from snake import Snake
from constants import *
import math
class AgentWwrap(Snake):
def __init__(self,body=[(0,0)] , direction=(1,0), name="Agent w wrap"):
super().__init__(body,direction,name=name)
def pathlen(self,a,b):
distX = abs(a[0]-b[0])
distY = abs(a[1]-b[1])
if distX > 60/2:
distX = 60- distX;
if distY > 40/2:
distY = 40 - distY;
return distX + distY
def add(self,a,b):
return (a[0]+b[0])%60,(a[1]+b[1])%40
def update(self,points=None, mapsize=None, count=None,agent_time=None):
pass
def updateDirection(self,maze):
#this is the brain of the snake player
olddir=self.direction
position=self.body[0]
#new direction can't be up if current direction is down...and so on
complement=[(up,down),(down,up),(right,left),(left,right)]
invaliddir=[x for (x,y) in complement if y==olddir]
validdir=[dir for dir in directions if not ( dir in invaliddir )]
#get the list of valid directions for us
validdir=[dir for dir in validdir if not (self.add(position,dir) in maze.obstacles or self.add(position,dir) in maze.playerpos)]
#if we collide then set olddir to first move of validdir (if validdir is empty then leave it to olddir)
olddir= olddir if olddir in validdir or len(validdir)==0 else validdir[0]
#shortest path.....we assume that the direction we are currently going now gives the shortest path
shortest=self.pathlen(self.add(position,olddir) , maze.foodpos)#length in shortest path
for dir in validdir:
newpos=self.add(position,dir)
newlen=self.pathlen(newpos , maze.foodpos)#length in shortest path
if newlen < shortest:
olddir=dir
shortest=newlen
#print("DIRECTION: " + str(olddir))
self.direction=olddir