# Modify function given below to add a second # player-ant to the game. The seco
ID: 3600191 • Letter: #
Question
# Modify function given below to add a second
# player-ant to the game. The second player will be controlled by
# another player. The game must be turn-based (alternates between the
# two players) and the first ant to get to the "food" wins the game.
#
# 1: Make the other ant controlled by the computer. The game
# should still be turn-based but the computer ant will navigate by
# itself to the goal.
# 2: Create a list of ants. The user can decide how many
# ants to add to the stage. Some or all of the ants can be controlled
# by the computer.
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
def antFoodGame(widthCells,heightCells):
gridSize=10
width=gridSize*widthCells
height=gridSize*heightCells
stage=makeEmptyPicture(width,height)
repaint(stage)
antX=random.randint(1,widthCells-1)*gridSize
antY=random.randint(1,heightCells-1)*gridSize
foodLeft = random.randint(1,heightCells - 1)*gridSize
for i in range(0,foodLeft):
goalX=random.randint(1,widthCells-1)*gridSize
goalY=random.randint(1,heightCells-1)*gridSize
addRectFilled(stage,goalX,goalY,gridSize,gridSize,red)
addRectFilled(stage,antX,antY,gridSize,gridSize,black)
stepsTaken=0
dir=requestString("A:Left, S:Down, D:Right, W:Up, X:Exit")
dir=dir.upper()
while(dir!="X" and foodLeft>0):
repaint(stage)
if dir=="A":
addRectFilled(stage,antX,antY,gridSize,gridSize,white)
antX=antX-gridSize
stepsTaken=stepsTaken+1
elif dir=="D":
addRectFilled(stage,antX,antY,gridSize,gridSize,white)
antX=antX+gridSize
stepsTaken=stepsTaken+1
elif dir=="W":
addRectFilled(stage,antX,antY,gridSize,gridSize,white)
antY=antY-gridSize
stepsTaken=stepsTaken+1
elif dir=="S":
addRectFilled(stage,antX,antY,gridSize,gridSize,white)
antY=antY+gridSize
stepsTaken=stepsTaken+1
addRectFilled(stage,antX,antY,gridSize,gridSize,black)
if antX==goalX and antY==goalY:
addText(stage,antX,antY,"Yum!",red)
foodLeft=foodLeft-1
if foodLeft>):
dir=requestString("A:Left, S:Down, D:Right, W:Up, X:Exit")
dir=dir.upper()
addText(stage,50,50,"Steps taken: "+str(stepsTaken))
repaint(stage)
Explanation / Answer
addRectFilled(stage,antX,antY,gridSize,gridSize,white)
antY=antY+gridSize
stepsTaken=stepsTaken+1
addRectFilled(stage,antX,antY,gridSize,gridSize,black)
if antX==goalX and antY==goalY:
addText(stage,antX,antY,"Yum!",red)
foodLeft=foodLeft-1
if foodLeft>):
dir=requestString("A:Left, S:Down, D:Right, W:Up, X:Exit")
dir=dir.upper()
addText(stage,50,50,"Steps taken: "+str(stepsTaken))
repaint(stage)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.