Home > OS >  How can I add letter in front of grid/map without ruining loop and replace values in the map afterwa
How can I add letter in front of grid/map without ruining loop and replace values in the map afterwa

Time:07-30

I have an assignment to program a simple tower defense game in python through printing. I managed to print the map/grid. But I'm not sure if I'm using the correct method because I'll need to replace the spaces in the map later. I was given a nested list with None variables. I'm not sure if I need to use it, and also there should be a letter in front of every row with A,B,C,D,E. How am I supposed to add this into my code, 'cause it'll probably mess up the loop?

What I'm supposed to get:

figure

This is my current code without using the None variable

field = [ [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None] ]

num_rows=len(field)
num_columns=len(field[0])
    
for column in range(num_columns): 
    print(' ----',end='')
print(' ')

for row in range(num_rows):
    for column in field[row]:
        print("|    ",end='')
    print('|')
    for column in field[row]:
        print("|    ",end='')
    print('|')
    for column in range(num_columns):
        print(' ----', end = '')
    print(' ')

which prints this:

figure

CodePudding user response:

You can try the following:

field = [ [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, ("ZOMBI", "15/15")],
        [None, ("ZOMBI", "15/15"), None, None, None, None, None] ]

num_rows=len(field)
num_columns=len(field[0])

row_indexes="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
padding = " " * 5 # 5 spaces

### print column index
print(" " * 2, end="") # padding
for column in range(1, num_columns 1):
    print(f"  {column}   ", end="")
print()

print(" ", end="")
for column in range(num_columns): 
    print(' -----',end='')
print(' ')

for row in range(num_rows):
    print(f"{row_indexes[row]}", end="") # print row index
    for column in range(num_columns):
        element = field[row][column]
        if element is None:
            print("|"   padding, end='')
        else:
            print("|"   element[0], end='')
    print('|')

    print(' ', end="")
    for column in range(num_columns):
        element = field[row][column]
        if element is None:
            print("|"   padding, end='')
        else:
            print("|"   element[1], end='')
    print('|')

    print(" ", end="") # padding
    for column in range(num_columns):
        print(' -----', end = '')
    print(' ')

Output:

    1     2     3     4     5     6     7   
  ----- ----- ----- ----- ----- ----- ----- 
A|     |     |     |     |     |     |     |
 |     |     |     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 
B|     |     |     |     |     |     |     |
 |     |     |     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 
C|     |     |     |     |     |     |     |
 |     |     |     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 
D|     |     |     |     |     |     |ZOMBI|
 |     |     |     |     |     |     |15/15|
  ----- ----- ----- ----- ----- ----- ----- 
E|     |ZOMBI|     |     |     |     |     |
 |     |15/15|     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 
  • Related