Mochamethod
Mochamethod

Reputation: 276

How to replace a specific index inside of an array with a new string, and then revert it back to its original value?

gameMap = [['.'] * 5 for _ in xrange(5)]   # '.' represents grass.

This should print something along the lines of:

[['.', '.', '.', '.', '.'], 
['.', '.', '.', '.', '.'], 
['.', '.', '.', '.', '.'], 
['.', '.', '.', '.', '.'], 
['.', '.', '.', '.', '.']]

I'm looking to replace a specific index of this array using the tracked user position. If the user position is at (0, 0), i.e. userpos = gamemap[0][0], and I replace that index with a 'P' so the first row looked like [['P', '.', '.', '.', '.'],, how would I go about tracking that index's original value of '.' so that when the player moves from 0,0 I could replace the that tile with its first value, which in this case would be '.'?

Upvotes: 0

Views: 49

Answers (1)

Tom Karzes
Tom Karzes

Reputation: 24052

Just save the old value in a variable, e.g. player_room_char:

player_room_char = gameMap[i][j]
gameMap[i][j] = 'P'

...

gameMap[i][j] = player_room_char

Presumably there is only one player, so you should only need a single variable.

Upvotes: 3

Related Questions