- #1
simphys
- 324
- 46
- 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.
Thanks in advance!
forgot to provide the code here, so here it is:
Note also that this is not homework, but am just preparing for an exam.
Thanks in advance!
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()
# TEST TASK 3 #
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
Last edited: