-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGreedyAgent2.py
113 lines (100 loc) · 4.47 KB
/
GreedyAgent2.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'''
import types
class AgentStrategy:
def __init__(self, func = None):
self.name = 'Strategy Example 0'
if func is not None:
self.execute = types.MethodType(func, self)
def execute(self):
print(self.name)
def reduceX(self):
print(self.name + 'from execute 1')
def reduceY(self):
print(self.name + 'from execute 2')
'''
class Agent:
def __init__(self, game):
self.game = game
self._isTuringingAround = False
def getDistanceToSnack(self):
deltaX = self.game.snake.body[0].position.x - self.game.snack.position.x
deltaY = self.game.snake.body[0].position.y - self.game.snack.position.y
return (deltaX,deltaY)
def update(self):
if self._isTuringingAround == True:
self.__turnAround()
self._isTuringingAround = False
velocity = self.game.snake.velocity
(x,y)=self.getDistanceToSnack()
if x == 0: # the snake is either above or below the snack
if y > 0: # the snake is below the snack
if velocity.dir == 'Left':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Right':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Down':
self.__turnAround()
self._isTuringingAround=True
else: # the snake is above the snack
if velocity.dir == 'Left':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Right':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Up':
self.__turnAround()
self._isTuringingAround=True
elif y == 0: # the snake is either to the right or to the left to the snack
if x > 0: # the snake is to the left to the snack
if velocity.dir == 'Up':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Down':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Right':
self.__turnAround()
self._isTuringingAround=True
else: # the snake is to the right to the snack
if velocity.dir == 'Down':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Up':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Left':
self.__turnAround()
self._isTuringingAround=True
else: #there is no straight way to the snack
#reduce X first
if x > 0:
if velocity.dir == 'Up':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Down':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Right':
self.__turnAround()
self._isTuringingAround=True
elif x < 0:
if velocity.dir == 'Down':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Up':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Left':
self.__turnAround()
self._isTuringingAround=True
elif y > 0:
if velocity.dir == 'Left':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Right':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Down':
self.__turnAround()
self._isTuringingAround=True
elif y < 0:
if velocity.dir == 'Left':
self.game.snake.velocity = velocity.turnLeft()
elif velocity.dir == 'Right':
self.game.snake.velocity = velocity.turnRight()
elif velocity.dir == 'Up':
self.__turnAround()
self._isTuringingAround=True
print(str(x)+" "+str(y)+" "+self.game.snake.velocity.dir)
input()
def __turnAround(self):
self.game.snake.velocity = self.game.snake.velocity.turnLeft()