简体   繁体   中英

How do I fill values inside square boxes in row column format using python for a tictaoe game

I want to build a tictactoe game, but first I need to build aa 3 X 3 grid and then insert values in it, like below在此处输入图像描述 I am unable to go beyond this:

print("+-------" * 3,"+",sep="")
for row in range(3):
    print("|       " * 3,"|",sep="")

Keep the drawing stuff and the actual game data separate. This also allows for different size grids:

rows = 3
columns = 3

# Create a 2d list and fill with numbers
# The inner list comprehension makes one row and
# the outer is all the rows
# row * columns + col + 1 is just the number to put in the cell
game = [[row * columns + col + 1 for col in range(columns)] for row in range(rows)]

def print_board():
    line_a = "+--------" * columns + "+"
    line_b = "|        " * columns + "|"
    line_c = "|   {:<2}   " * columns + "|"

    print(line_a)
    for row in game:
        print(line_b)
        print(line_c.format(*row))
        print(line_b)
        print(line_a)

game[1][1] = 'X'
print_board()

Output

+--------+--------+--------+
|        |        |        |
|   1    |   2    |   3    |
|        |        |        |
+--------+--------+--------+
|        |        |        |
|   4    |   X    |   6    |
|        |        |        |
+--------+--------+--------+
|        |        |        |
|   7    |   8    |   9    |
|        |        |        |
+--------+--------+--------+

Here is a generic method to create grids of any size:


def makegrid(l, w, h):
    n = len(l[0])

    line = '+%s+' % '+'.join(['-'*w]*n)
    sep = '|%s|' % '|'.join([' '*w]*n)
    sep2 = '\n'.join([sep]*((h-1)//2))

    sep3 =  '\n%s\n' % '\n'.join([sep2, line, sep2])

    space = ' '*((w-1)//2)
    grid = sep3.join('|%s|' % '|'.join(f'{space}{i}{space}' for i in x) for x in l)

    grid = f'{line}\n{sep2}\n{grid}\n{sep2}\n{line}'.replace('\n\n', '\n') 
    
    return grid

examples:

l = [[1,2,3],[4,5,6],[7,8,9]]

print(makegrid(l, 5, 3))
+-----+-----+-----+
|     |     |     |
|  1  |  2  |  3  |
|     |     |     |
+-----+-----+-----+
|     |     |     |
|  4  |  5  |  6  |
|     |     |     |
+-----+-----+-----+
|     |     |     |
|  7  |  8  |  9  |
|     |     |     |
+-----+-----+-----+

print(makegrid(l, 3, 1))
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+

print(makegrid(l, 7, 5))
+-------+-------+-------+
|       |       |       |
|       |       |       |
|   1   |   2   |   3   |
|       |       |       |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|       |       |       |
|   4   |   5   |   6   |
|       |       |       |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
|       |       |       |
+-------+-------+-------+
def crea_griglia():
values = [[1,2,3],[4,"X",6],[7,8,9]]
for i in range(len(values)):
    print(("+"+"-"*7)*3+"+")
    print(("|"+" "*7)*3+"|")
    for j in range(len(values[0])):
        print(("|"+" "*3+str(values[i][j])+" "*3),end="")
    print("|")
    print(("|"+" "*7)*3+"|")
print(("+"+"-"*7)*3+"+")

crea_griglia()

It's probably easier to just hardcode the board - especially since you can use f-strings to display actual values.

b = [['1', '2', '3'], ['4', 'X', '6'], ['7', '8', '9']]
print(f"""
+-------+-------+-------+
|       |       |       |
|   {b[0][0]}   |   {b[0][1]}   |   {b[0][2]}   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   {b[1][0]}   |   {b[1][1]}   |   {b[1][2]}   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   {b[2][0]}   |   {b[2][1]}   |   {b[2][2]}   |
|       |       |       |
+-------+-------+-------+
""")

prints:

+-------+-------+-------+
|       |       |       |
|   1   |   2   |   3   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   4   |   X   |   6   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
+-------+-------+-------+

how about something like this:

board = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

print("+-------" * 3 + '+')
for row in range(3):
    print("|       " * 3 + '|')

    for col in range(3):
        print(f"|   {board[row][col]}  ", end=' ')

    print('|\n' + "|       " * 3 + '|')
    print("+-------" * 3 + '+')

output:

+-------+-------+-------+
|       |       |       |
|   1   |   2   |   3   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   4   |   5   |   6   |
|       |       |       |
+-------+-------+-------+
|       |       |       |
|   7   |   8   |   9   |
|       |       |       |
+-------+-------+-------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM