Having trouble completing these few steps to finish my program. Programming in P
ID: 3734239 • Letter: H
Question
Having trouble completing these few steps to finish my program.
Programming in Python 3
My code:
http://www.mediafire.com/file/w1aw58zsmcx03gn/mini_game.py
Step 1 1) Keep player in the world. Right now our player can "fall off the edges" causing the program to abort. Modify your program so the player cannot move past the top, bottom, left and right edges of the world. For example, if the player is standing at the top edge (row 0) and the user enters UP the program should ignore this request. Step 2 2) Picking up coins. Modify your program so it keeps track of the coins collected by the player, and ends the game when all coins have been collected. If the player moves into a cell containing a coin, the program should increase the variable used to count the coins, and turn that cell into a grass cell (which gives the illusion of the player picking up the coin) Step 3 3) TNT. This is a feature turns rock cells surrounding the player into grass cells when the player enters the TNT" command. This command should not destroy coins Example 1: Assume the state of the small world is as follows (In red the cells surrounding the player) .. P RCR . . After entering the TNT command, the state of the world should look as follows Example 2: Assume the state of the small world is as follows RPR .. RCR .. After entering the TNT command the result should be Given a cell located at row r and cell c, the "surrounding" cells are the eight cells that are one step away from (rc) c-1 c c+1 Once all steps completed you are finished.Explanation / Answer
I have modified your code. Kindly check for comments.
miniGame.py
world = [ ['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', 'R', 'R', 'R', '.', '.'],
['.', '.', '.', 'R', 'C', 'R', '.', '.'],
['.', '.', '.', 'R', 'R', 'R', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'] ]
def showWorld(w, player_row, player_col):
for row in range(len(w)):
for col in range(len(w[0])):
if row == player_row and col == player_col:
print('P', end = ' ')
# show player
else:
print(w[row][col], end = ' ')
# show world item
print()
print()
# Checks if the player is not outside the board
def checkLimits(row, col, w):
# Check if row is within the board
if row < 0 or row >= len(w):
return False;
#check if column is within board
if col <0 or col >= len(w[0]):
return False;
#if both above are true return true, that is row and col are within bounds
return True;
def blast(row, col, w):
# here all the possible combinations are taken for all 8 positions.
# if you keep (0,0) at center and take all combinations of +1 and -1
# you will result into following array.
pos = [(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]
# TNT command should blast rock leaving the coins and grass
for i in pos:
# checking if it is rock if yes make it grass
if checkLimits(row+int(i[0]),col+int(i[1]),w) and w[row+int(i[0])][col+int(i[1])] == 'R':
w[row+i[0]][col+i[1]]='.'
def readAction():
print("Enter UP, DOWN, LEFT, RIGHT to move around")
print("Enter QUIT to end program")
a = input("Enter action: ")
return a
# Tracking location of player
player_row = 0
player_col = 0
#Tracking the number of coins picked by player
numberCoins = 0
# Game loop
done = False
while not done:
showWorld(world, player_row, player_col)
action = readAction()
if action == 'UP':
#Check if the next position is outside graph if yes then do nothing and just warn
if not checkLimits(player_row -1, player_col, world):
print("Cannot move outside of board")
elif world[player_row - 1][player_col] == 'R': # is there a Rock there?
print("Ouch! Can't move there...there is a rock!")
#Check if the next position is coin, if it is then increment the coin count
# and change it to grass as the player as picked the coin
elif world[player_row - 1][player_col] == 'C': # is there a Rock there?
print("Yayyy!! found Coin")
numberCoins+=1
world[player_row-1][player_col] = '.'
player_row -= 1
else: # No rock to the right of the player
player_row -= 1
elif action == 'DOWN':
#Check if the next position is outside graph if yes then do nothing and just warn
if not checkLimits(player_row +1, player_col, world):
print("Cannot move outside of board")
elif world[player_row + 1][player_col ] == 'R': # is there a Rock there?
print("Ouch! Can't move there...there is a rock!")
#Check if the next position is coin, if it is then increment the coin count
# and change it to grass as the player as picked the coin
elif world[player_row + 1][player_col] == 'C': # is there a Rock there?
print("Yayyy!! found Coin")
numberCoins+=1
world[player_row+1][player_col] = '.'
player_row += 1
else: # No rock to the right of the player
player_row += 1
elif action == 'LEFT':
#Check if the next position is outside graph if yes then do nothing and just warn
if not checkLimits(player_row , player_col - 1, world):
print("Cannot move outside of board")
elif world[player_row][player_col - 1] == 'R': # is there a Rock there?
print("Ouch! Can't move there...there is a rock!")
#Check if the next position is coin, if it is then increment the coin count
# and change it to grass as the player as picked the coin
elif world[player_row][player_col - 1] == 'C': # is there a Rock there?
print("Yayyy!! found Coin")
numberCoins+=1
world[player_row][player_col - 1] = '.'
player_col -= 1
else: # No rock to the right of the player
player_col -= 1
elif action == 'RIGHT':
#Check if the next position is outside graph if yes then do nothing and just warn
if not checkLimits(player_row, player_col + 1, world):
print("Cannot move outside of board")
elif world[player_row][player_col + 1] == 'R': # is there a Rock there?
print("Ouch! Can't move there...there is a rock!")
#Check if the next position is coin, if it is then increment the coin count
# and change it to grass as the player as picked the coin
elif world[player_row][player_col + 1] == 'C': # is there a Rock there?
print("Yayyy!! found Coin")
numberCoins+=1
world[player_row][player_col + 1] = '.'
player_col += 1
else: # No rock to the right of the player
player_col += 1
elif action == 'QUIT':
print ("Number of coins collected ",numberCoins)
done = True
elif action == 'TNT':
blast(player_row, player_col, world)
else:
print(" Huh? Try again. ")
Sample Output:
P . . . . . . .
. . . R R R . .
. . . R C R . .
. . . R R R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: DOWN
. . . . . . . .
P . . R R R . .
. . . R C R . .
. . . R R R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: DOWN
. . . . . . . .
. . . R R R . .
P . . R C R . .
. . . R R R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: RIGHT
. . . . . . . .
. . . R R R . .
. P . R C R . .
. . . R R R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: RIGHT
. . . . . . . .
. . . R R R . .
. . P R C R . .
. . . R R R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: TNT
. . . . . . . .
. . . . R R . .
. . P . C R . .
. . . . R R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: RIGHT
. . . . . . . .
. . . . R R . .
. . . P C R . .
. . . . R R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: TNT
. . . . . . . .
. . . . . R . .
. . . P C R . .
. . . . . R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: RIGHT
Yayyy!! found Coin
. . . . . . . .
. . . . . R . .
. . . . P R . .
. . . . . R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: LEFT
. . . . . . . .
. . . . . R . .
. . . P . R . .
. . . . . R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: UP
. . . . . . . .
. . . P . R . .
. . . . . R . .
. . . . . R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: TNT
. . . . . . . .
. . . P . R . .
. . . . . R . .
. . . . . R . .
. . . . . . . .
Enter UP, DOWN, LEFT, RIGHT to move around
Enter QUIT to end program
Enter action: QUIT
Number of coins collected 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.