Reputation: 276
worldArray = [["." for i in range(5)] for i in range(5)]
This produces a map that I can use for my game. It should look something like:
[['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.']]
Let's say the '.'
represents a grass tile. If I wanted to replace a specific number of indexes to '~'
instead to represent a water tile, what would be the easiest way of doing so? I want the map to look a bit like:
[['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['~', '~', '.', '.', '.'],
['~', '~', '~', '.', '.']]
I know I can manually go through and change each specific index to show the '~'
tile, but the real in-game map I use is 40 x 40 instead -- which would make the job of individually replacing each index a bit tedious and redundant. I would like to be able to define which tiles I want replaced, i.e. row 4, columns 1 - 2; row 5, columns 1 - 3. how could I go about doing that?
Upvotes: 1
Views: 416
Reputation: 47
Based on your question I threw together a simple function you can run. Feel free to copy and paste it into the IDLE console:
>>> def a():
global worldArray
b = 1
while b < 2:
c = (int(input('Row #: ')),int(input('Column-Leftbound: ')),int(input('Column-Rightbound: ')))
worldArray[c[0]][c[1]] = '~'
worldArray[c[0]][c[2]] = '~'
d = c[2] - c[1]
for i in range(d):
worldArray[c[0]][c[1]+i] = '~'
print(worldArray)
b = int(input('b now equals: '))
>>> a() #This line is for you to call the function at the console
Keep these things in mind:
Legitimate Row and Column numbers are from: 0-4.
Also, in order to exit the while loop I ask you to reset the value of b. You stay in the loop if you enter a value less than 2. Let me know if this helps. I tried to keep it very simple.
.
.
As a side note,
worldArray = [["." for i in range(5)] for i in range(5)]
does not produce a nice and neat matrix for me.
Upvotes: 0
Reputation: 36043
Slice notation is perfect for this:
from functools import partial
def tile(icon, row, col_start, col_end):
worldArray[row][col_start:col_end] = icon * (col_end - col_start)
water = partial(tile, '~')
mountain = partial(tile, '^')
water(3, 0, 2)
water(4, 0, 3)
Upvotes: 3
Reputation: 7110
I would define a function that returns whether or not to make it ~
instead of .
.
"""
Determines if the game position is regular or not
"""
def isRegular(x,y):
# Only replace Row 4 column 1 and 2 with ~
return not (x==4 and y in [1,2])
worldArray = [["." if isRegular(x,y) else "~" for x in range(5) ] for y in range(5)]
You can change the regular()
function to your liking.
Upvotes: 0
Reputation: 4321
You could write a helper function
def replace_at_position(world_array, row_col_dict, repl_char):
"""
Use row_col_dict in format of {row : (startOfRange, endOfRange)} to replace the characters
inside the specific range at the specific row with repl_char
"""
for row in row_col_dict.keys():
startPos, endPos = row_col_dict[row]
for i in range(startPos, endPos):
worldArray[row][i] = repl_char
return worldArray
You can use it like this:
worldArray = [["." for i in range(10)] for j in range(5)]
# replace row 2 (the third row) colums 0-4 (inclusive, exclusive, like range) with character '~'
worldArray = replace_at_position(worldArray, {2 : (0,10)}, '~')
#replace row 1 (the second row) colums 0-5 (inc, exc, like range) with character '~'
worldArray = replace_at_position(worldArray, {1 : (0, 5)}, '~')
pprint.pprint(worldArray)
This will result in:
[['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
['~', '~', '~', '~', '~', '.', '.', '.', '.', '.'],
['~', '~', '~', '~', '~', '~', '~', '~', '~', '~'],
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.']]
Upvotes: 1