Professional Documents
Culture Documents
def inbound(position):
return position >= 0 and position < 51
# Center
center = sheep_position1 == 25 and sheep_position2 == 25
# Lists all the valid moves that the sheep can make (a valid move is a
move that will not permit the sheep
# to go out of bounds or to go into a blocked cell
for d in sheep_directions:
indexinbound = inbound(sheep_position1 + d[0]) and
inbound(sheep_position2 + d[1])
if indexinbound:
if graze_field[sheep_position1 + d[0]][sheep_position2 + d[1]]
== 0:
validmoves.append((d[0], d[1]))
# print(validmoves)
# Picks a valid move at random and changes
rand1 = random.randint(0, len(validmoves) - 1)
sheep_position1 += validmoves[rand1][0]
sheep_position2 += validmoves[rand1][1]
# If robot is in the field of view,
else:
# Finding moves that bring us closer to the target
validmoves = []
# If the x axis difference is negative, append (-1, 0)
if bot_position1 - sheep_position1 < 0:
validmoves.append((-1, 0))
# Else if x axis difference is positive, append (1, 0)
elif bot_position1 - sheep_position1 > 0:
validmoves.append((1, 0))
# Bot has 8 neighbors. Finds all valid neighbors and picks one valid at
random
botdirections = [(0, 0), (0, 1), (-1, 0), (1, 0), (0, -1), (-1, -1), (1,
1), (-1, 1), (1, -1)]
botmoves = []
for d in botdirections:
indexinbound = inbound(bot_position1 + d[0]) and inbound(bot_position2
+ d[1])
if indexinbound and graze_field[bot_position1 + d[0]][bot_position2 +
d[1]]:
validmoves.append((d[0], d[1]))
rand3 = random.randint(0, len(validmoves) - 1)
bot_position1 += validmoves[rand3][0]
bot_position2 += validmoves[rand3][1]