-
Notifications
You must be signed in to change notification settings - Fork 0
/
behavior.py
170 lines (139 loc) · 5.25 KB
/
behavior.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import boardUtils, math, copy
from random import choice,randrange, shuffle
los = 5
moves = {
'left': (-1,0),
'right': (1,0),
'up': (0,1),
'down':(0,-1),
'left and up': (-1,1),
'right and up': (1,1),
'left and down': (-1,-1),
'right and down':(1,-1)
}
def dist(p1, p2):
return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
def mario1(mario,toads,board):
while True:
move = choice(moves.values())
if not( board[mario[0] + move[0]] [mario[1] + move[1]]==boardUtils.wall):
mario[0] += move[0]
mario[1] += move[1]
break
return mario
def mario2(mario,toads,board):
closestToad = 0
for toad in toads:
if dist(mario,[toad['x'],toad['y']]) > closestToad:
closestToad = toad
xdiff = mario[0]-closestToad['x']
ydiff = mario[1]-closestToad['y']
if abs(xdiff) < abs(ydiff):
if xdiff > 0:
dx = 1
dy = 0
else:
dx = -1
dy = 0
elif abs(ydiff) < abs(xdiff):
if ydiff > 0:
dy = 1
dx = 0
else:
dy = -1
dx = 0
else:
move = moves[choice(moves.keys())]
dx = move[0]
dy = move[1]
while True:
if not( board[mario[0] + dx][ mario[1] + dy]==boardUtils.wall):
mario[0] += dx
mario[1] += dy
break
move = moves[choice(moves.keys())]
dx = move[0]
dy = move[1]
return mario
def mario3(mario,toads,board):
randomizedMoves = moves.values()
#print randomizedMoves
for move in randomizedMoves:
#print move[0]
#print move[1]
if not( board[mario[0] + move[0]] [mario[1] + move[1]]==boardUtils.wall):
#print "Not Wall"
for toad in toads:
prevDist = dist([mario[0] + move[0], mario[1] + move[1]], [toad['x'],toad['y']])
newDist = dist(mario,[toad['x'],toad['y']])
if newDist < prevDist :
mario[0] += move[0]
mario[1] += move[1]
return mario
return mario
def toad1(mario,toads,board):
for toad in toads:
move = choice(moves.values())
toad['x'] = move[0]
toad['y'] = move[1]
def toad2(mario,toads,board):
for toad in toads:
toad['previousDistance'] = toad['distanceToMario']
toad['distanceToMario'] = dist(mario,[toad['x'],toad['y']])
if toad['previousDistance'] < toad['distanceToMario']:
toad['direction'] = choice(moves.keys())
toadMove = moves[toad['direction']]
board[toad['x']][toad['y']]=boardUtils.toadTrail
if not( board[toad['x'] + toadMove[0]][ toad['y'] + toadMove[1]]==boardUtils.wall):
toad['x'] += toadMove[0]
toad['y'] += toadMove[1]
return toads
def toad3(mario,toads,board):
#Toad AI
closestToad = toads[0]
for toad in toads:
if dist(mario,[toad['x'],toad['y']]) > closestToad:
closestToad = toad
for toad in toads:
approxclose = closestToad['x']+randrange(-3,3),closestToad['y']+randrange(-3,3)
possibleMoves = []
toad['previousDistance'] = toad['distanceToMario']
toad['distanceToMario'] = dist(mario,[toad['x'],toad['y']])
#Start new collaboration code
if toad['distanceToMario'] > los:
if toad['id'] != closestToad['id']:
for move in moves.keys():
x,y = moves[move][0],moves[move][1]
if dist([approxclose[0],approxclose[1]], [toad['x'],toad['y']]) >= dist([approxclose[0], approxclose[1]], [toad['x']+x,toad['y']+y]):
possibleMoves.append(move)
try:
toad['direction'] = choice(possibleMoves)
except:
toad['direction'] = choice(moves.keys())
else:
if toad['previousDistance'] < toad['distanceToMario']:
toad['direction'] = choice(moves.keys())
elif toad['distanceToMario'] <= los:
for move in moves.keys():
x,y = moves[move][0], moves[move][1]
if dist(mario, [toad['x']+x,toad['y']+y]) <= toad['previousDistance']:
possibleMoves.append(move)
try:
toad['direction'] = choice(possibleMoves)
except:
toad['direction'] = choice(moves.keys())
#End new Collaboration code
toadMove = moves[toad['direction']]
board[toad['x']][toad['y']]=boardUtils.toadTrail
if not( board[toad['x'] + toadMove[0]][ toad['y'] + toadMove[1]]==boardUtils.wall):
toad['x'] += toadMove[0]
toad['y'] += toadMove[1]
return toads
marioMap = {1:mario1,2:mario2,3:mario3}
toadMap = {1: toad1,2:toad2,3:toad3}
def mario(mario,toads,board,behavior):
f = marioMap[behavior](mario,toads,board)
return f
def toad(mario,toads,board,behavior):
f = toadMap[behavior](mario,toads,board)
return f