# Extend function given below to add a second # player-ant to the game. The seco
ID: 3600729 • Letter: #
Question
# Extend 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.
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
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
An intArrayBag is a collection of integer numbers,some numbers may appear multiple times in a bag for that it can use capacity one of these bag can change after its created but maximum capacity is limited by amount of free memory on machine.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.