I can't get this task right regarding a simple ' 2D game' as a matrix

• Comp Sci
simphys
Homework Statement:
Hello guys, if you don't mind, I will attach the whole exercise in the pdf(it is the 1st difficult task RPG_game).

This is the description of the exact task(task 3 of difficult task 1) WITH CODE HIGHLIGHTED(function starts from line 36, the other stuff is for context):

[3 Points] Write a function player_move that takes the same three parameters as the previous function, plus a fourth parameter direction, which represents the direction of the move (“UP”, “DOWN”, “LEFT”, “RIGHT”). The function determines whether the intended move of the player is possible, and what consequences it has for the player’s health. If the move is possible, the function returns a list that includes (1) the player’s health after the move, (2) the player’s position, and (3) the updated state of the world. If the move is impossible, the player does not move and nothing changes. Should the player die, they are no longer present in the matrix, since the position where they died will be occupied by the enemy that defeated them. In that case the returned position is the position where the player died.
Relevant Equations:
So It seems to me that i have done everything correctly, comparing it with the test underneath the file as well, yet it doesn't seem to work out.
Can someone help/steer me in the right direction please?
And perhaps, if possible, give some advice on how I have written the code. It's the first time I use 2D Lists in matrix-form, just something to keep in mind :'D
I have also put some notes on what is to be done in the problematic function
Note also that this is not homework, but am just preparing for an exam.

forgot to provide the code here, so here it is:
Python:
# RPG subsystem: check whether the next player move on a 5x5 tileset is possible, and if player
# is attacked by an enemy. Player symbol: 5, weak enemies (-50 health): 1, strong enemies
# (-100 health): 2. Possible player movement: LEFT, RIGHT, UP, DOWN (move one field on map).
#player cannot displace beyond the boundaries of the playing field.

def translate_move(direction):
coordinate = [0,0] #starting point.
#+ve y-axis is DONW!
if direction == 'UP':
coordinate[0] -= 1
elif direction == 'DOWN':
coordinate[0] += 1
elif direction == 'LEFT':
coordinate[1] -= 1
elif direction == 'RIGHT':
coordinate[1] += 1
return coordinate

def fight_enemy(health, position, world):
#players fight with enemy that is at a certain position. player receives remaining health back.
# position 1 loses 50 hjealth at position 2 loses -100 health.
#position = [row, col]
row = position[0]
col = position [-1]
position_in_world = world[row][col]
if position_in_world == 2:
health -= 100
elif position_in_world == 1:
health -= 50
if health <= 0:
health = 0
return health

def player_move(health, position, world, direction):
'''
Function displays whether a desirable move of the player is possible and what the consequences are.
'''
#Updated positions.
updated_position = [position[0] + translate_move(direction)[0], position[1] + translate_move(direction)[1]]
row = updated_position[0]
col = updated_position[1]
old_row = position[0]
old_col = position[1]
###if move not possible, return original
if row > 4 or row < 0 or col > 4 or col < 0:
return [health, position, world]
#Updating world from old to new position (changing 5)
if old_row == 0 or old_row == 2 or old_row == 3:
world[old_row][old_col] = 0
elif old_row == 1:
world[old_row][old_col] = 1
elif old_row == 4:
world[old_row][old_col] = 2
###if player gets killed --> remove player, change with enemy + return updated position
health = fight_enemy(health, updated_position, world)
if health == 0:
return [health, updated_position, world]
#if move possible + not killed --> return [updated health, updated position, updated world]
else:
world[row][col] = 5
return [health, updated_position, world]

### DO NOT TOUCH THE CODE BELOW THIS LINE ###

def print_world(world):
for row in world:
print(row)
print()

def test():
score = 0

game_world = [
#0 1 2 3 4
[0,0,0,0,0],#0
[1,1,1,1,1],#1
[0,0,0,0,0],#2
[0,0,0,0,0],#3
[2,2,2,2,2]]#4
player_health = 100
player_location = [0,0]

# print_world(game_world)
game_world[player_location[0]][player_location[1]] = 5
print_world(game_world)
print("Initial player health:", player_health)
print()
t3_1 = player_move(100, [0,0], [[5,0,0,0,0], [1,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]], "DOWN")
print( "World state after move down:" )
print_world( t3_1[2] )

t3_2 = player_move(50, [1,0], [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]], "LEFT")
print( "World state after move left:" )
print_world( t3_2[2] )

t3_3 = player_move(50, [1,0], [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]], "RIGHT" )
print( "World state after move right:" )
print_world( t3_3[2] )

if t3_1[0] == 50 and t3_1[1] == [1,0] and t3_1[2] == [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]] and t3_2[0] == 50 and t3_2[1] == [1,0] and t3_2[2] == [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]] and t3_3[0] == 0 and t3_3[1] == [1,1] and t3_3[2] == [[0,0,0,0,0], [0,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]]:
print("# TEST 3 SUCCESSFUL #")
score += 3
else:
print("# TEST 3 FAILED #")
print()

print()
print("Tentative overall score: ", score, "/ 6")
print()
print("This is an indicative value only. You also need to follow the instructions of the exam,")
print("and code in a flexible way (i.e., your code still needs to work when tested with different values).")

# run the program.
if __name__ == "__main__":
test()

Attachments

• lab10_assignment_en.pdf
844.6 KB · Views: 50
Last edited:

Homework Helper
Gold Member
Homework Statement:: Hello guys, if you don't mind, I will attach the whole exercise in the pdf(it is the 1st difficult task RPG_game) and my code as well.
No code.

simphys
simphys
No code.
my apologies, didn't send through perhaps! Uploading Right now, thanks for notifying me :)

simphys
No code.
I uploaded the whole file, which is apparently not possible

Homework Helper
Gold Member
I uploaded the whole file, which is apparently not possible
No, you can't upload source files here. Post your source using the </> icon if it is a sensible length, if it is not a sensible length then edit it down to just the bit you have difficulty with.

simphys
No, you can't upload source files here. Post your source using the </> icon if it is a sensible length, if it is not a sensible length then edit it down to just the bit you have difficulty with.
hey @pbuk is it possible to leave the code like this for context and simply highlight the problematic code? Thanks in advance

Homework Helper
Gold Member
hey @pbuk is it possible to leave the code like this for context and simply highlight the problematic code? Thanks in advance
It's now fine, although yes it would be even better if you highlighted the problem lines.

simphys
Homework Helper
Gold Member
On line 46 you set new_position_in_world = world[row][col].
• How do you know that row and col are valid indices?
• Do you use new_position_in_world anyway (or old_position_in_world)?
I don't understand what lines 51-57 are trying to do: what is the significance of old_row here?

simphys
simphys
On line 46 you set new_position_in_world = world[row][col].
• How do you know that row and col are valid indices?
• Do you use new_position_in_world anyway (or old_position_in_world)?
I don't understand what lines 51-57 are trying to do: what is the significance of old_row here?
Thanks, so yes
1. for new_position_in_world and old_position_in_world, I forgot to remove these.
2. so that is what I am checking initially, if these are: col<0(or >4) and row<0(or >4) then I just return everything how it was given as input otherwise I continue further with the other code

simphys
On line 46 you set new_position_in_world = world[row][col].
• How do you know that row and col are valid indices?
• Do you use new_position_in_world anyway (or old_position_in_world)?
I don't understand what lines 51-57 are trying to do: what is the significance of old_row here?
as for row_old change the position in the matrix from 5(position where the player is located back to its' original number 0, 1 or 2 and then at the end only for when the player is still living I update his position in the 'world'

simphys
@pbuk It turns out that my code is COMPLETELY fine...! altough messy.
Someone told me that was a solution of the mock exam. I went through the code and then I realized that all that struggle was for nothing... When he updates the world(/matrix) he does not update the position back to 0,1 or 2 but just changes the old position with a zero.

Whilst I on the other hand was changing it back to its' original state..
Anyway Thanks for having a look at it!

berkeman