sparklecoding
sparklecoding

Reputation: 99

How can I get input letters displayed on a game board?

So I have a python function that displays a game board with any number of rows and columns:

def displayy(rows:int, cols:int)-> None:
    for row in range(rows):
        print('|' + ' ' * (3*cols) + '|')
    print(' ' + '-' * 3* cols)

Now using this, if I have this as user input:

4 #number of rows
4 #number of cols
CONTENTS
            # there are four spaces on this line of input
B HF
CGJB
DBFC

How can I get these letters printed into the board? As so:

|            |
| B     H  F |
| C  G  J  B |
| D  B  F  C |
 ------------ 

Upvotes: 2

Views: 58

Answers (1)

purarue
purarue

Reputation: 2164

Unless the rows and columns won't be the same, this should work.

# use length of strings as columns
# calculate number of rows based on length of columns
# dispay empty rows at top

def display(board):
    empty_rows = len(board[0]) - len(board) 
    board = [" " * len(board[0]) for _ in range(empty_rows)] + board #add empty rows to the top
    for row in board:
        print("| " + "  ".join(list(row)) + " |")
    print(" " + ('-' * 3*len(board)))

board = ["B HF", "CGJB" , "DBFC"]
display(board)

Output:

|            |
| B     H  F |
| C  G  J  B |
| D  B  F  C |
 ------------ 

Upvotes: 1

Related Questions