You are on page 1of 3

# Shahpur Khan, Keshav Subramanyam

# Summer 440 - Intro to Artificial Intelligence


# Project 3: The Meanest Sheep in the World Who Is Also Near Sighted
import random

def inbound(position):
return position >= 0 and position < 51

def sheep(sheep_position1, sheep_position2, bot_position1, bot_position2,


graze_field):
# The sheep occupies a random cell in the grid
sheep_position1 = random.randint(0, 51)
sheep_position2 = random.randint(0, 51)

# The robot occupies a random cell in the grid


bot_position1 = random.randint(0, 51)
bot_position2 = random.randint(0, 51)

# Suppose sheep is already at the bot, the program ends


if (sheep_position1, sheep_position2) == (bot_position1, bot_position2):
return False

# Center
center = sheep_position1 == 25 and sheep_position2 == 25

# While the sheep isn't at center,


while not center:
if not (inbound(sheep_position1) and inbound(sheep_position2)):
return False
print(sheep_position1)
print(sheep_position2)
# Creates a field of view at every sheep position (x1, x2).
# Field of View Represents:
# (x1 - 2, x2 + 2), (x1 - 1, x2 + 2), (x1, x2 + 2), (x1 + 1, x2 + 2), (x1 +
2, x2 + 2)
# (x1 - 2, x2 + 1), (x1 - 1, x2 + 1), (x1, x2 + 1), (x1 + 1, x2 + 1), (x1 +
2, x2 + 1)
# (x1 - 2, x2), (x1 - 1, x2), <<<<SHEEP'S>>>>(x1, x2)<<<<POSITION>>>>, (x1
+ 1, x2), (x1 + 2, x2)
# (x1 - 2, x2 - 1), (x1 - 1, x2 - 1), (x1, x2 - 1), (x1 + 1, x2 - 1), (x1 +
2, x2 - 1)
# (x1 - 2, x2 - 2), (x1 - 1, x2 - 2), (x1, x2 - 2), (x1 + 1, x2 - 2), (x1 +
2, x2 - 2)
fieldofview = []
end = sheep_position2 + 2
while end > sheep_position2 - 3:
for i in range(sheep_position1 - 2, sheep_position1 + 3):
fieldofview.append((i, end))
end -= 1

# Seeing if robot is in field of view, this will determine the sheep's


action
botfield = False
for i in range(0, len(fieldofview)):
if (bot_position1, bot_position2) == fieldofview[i]:
botfield = True

# If robot isn't in the field of view,


if not botfield:
sheep_directions = [(0, 0), (0, 1), (1, 0), (-1, 0), (0, -1)]
validmoves = []

# 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))

# If the y axis difference is negative, append (0, -1)


if bot_position2 - sheep_position2 < 0:
validmoves.append((0, -1))
# Else if the y axis difference is positive, append (0, 1)
elif bot_position2 - sheep_position2 > 0:
validmoves.append((0, 1))

# Pick a random element out of validmoves


rand2 = random.randint(0, len(validmoves) - 1)

# Increment sheep position with those numbers


sheep_position1 += validmoves[rand2][0]
sheep_position2 += validmoves[rand2][1]

# One more check, if sheep is at robot position, it returns false


if sheep_position1 == bot_position1 and sheep_position2 ==
bot_position2:
return False

# 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]

# Last and final check whether sheep is at bot


if sheep_position1 == bot_position1 and sheep_position2 == bot_position2:
return False
print("LEFT")
# Given that the sheep never reaches the bot and eventually hits the center,
return true
return True

# Creates the grazing field


graze_field = [[0] * 51] * 51

# U is the pen, the pen is blocked. 1 means blocked.


graze_field[25][24] = 1
graze_field[25][26] = 1
graze_field[24][26] = 1
graze_field[26][24] = 1
graze_field[26][25] = 1
graze_field[26][26] = 1

# The sheep occupies a random cell in the grid


sheep_position1 = random.randint(0, 51)
sheep_position2 = random.randint(0, 51)

# The robot occupies a random cell in the grid


bot_position1 = random.randint(0, 51)
bot_position2 = random.randint(0, 51)

print(sheep(sheep_position1, sheep_position2, bot_position1, bot_position2,


graze_field))
# print(graze_field)

You might also like