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

  • Context: Comp Sci 
  • Thread starter Thread starter simphys
  • Start date Start date
  • Tags Tags
    2d Game Matrix
Click For Summary

Discussion Overview

The discussion revolves around a coding task related to a 2D game implemented as a matrix. Participants are examining a function that checks player movement and interactions with enemies on a 5x5 tileset. The focus includes debugging and clarifying the logic of the code, particularly regarding player movement and health management.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a code snippet and describes the functionality related to player movement and enemy interactions.
  • Another participant questions the validity of indices used to access the game world, specifically regarding the handling of row and column values.
  • Concerns are raised about the significance of certain lines in the code, particularly the handling of old and new positions in the game world.
  • A participant acknowledges forgetting to remove unused variables related to position checks and clarifies their intended logic for returning to original states.
  • Another participant reflects on their code being "completely fine" despite initial concerns, noting a misunderstanding about how the game world was updated.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the code and the logic behind certain operations. There is no consensus on the resolution of the issues raised, and some participants continue to question aspects of the implementation.

Contextual Notes

Participants mention specific lines of code and their implications, indicating that there may be unresolved assumptions about the game's mechanics and the handling of player states.

simphys
Messages
327
Reaction score
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:
[CODE lang="python" highlight="36-65 is the function"]# 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()[/CODE]
 

Attachments

Last edited:
Physics news on Phys.org
simphys said:
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.
 
  • Like
Likes   Reactions: simphys
pbuk said:
No code.
my apologies, didn't send through perhaps! Uploading Right now, thanks for notifying me :)
 
pbuk said:
No code.
I uploaded the whole file, which is apparently not possible
 
simphys said:
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.
 
pbuk said:
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
 
simphys said:
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.
 
  • Like
Likes   Reactions: 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?
 
  • Like
Likes   Reactions: simphys
pbuk said:
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
 
  • #10
pbuk said:
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'
 
  • #11
@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!
 
  • Like
Likes   Reactions: berkeman

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
796
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
10K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
1
Views
4K