简体   繁体   English

如何使用 python 以行列格式填充方框中的值以进行 tictaoe 游戏

[英]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我想构建一个 tictactoe 游戏,但首先我需要构建一个 3 X 3 网格,然后在其中插入值,如下所示在此处输入图像描述 I am unable to go beyond this:除此之外,我无法 go :

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 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() crea_griglia()

It's probably easier to just hardcode the board - especially since you can use f-strings to display actual values.对板进行硬编码可能更容易 - 特别是因为您可以使用 f-strings 来显示实际值。

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: output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OCR - 如何使用 python 识别方框内的数字? - OCR - How to recognize numbers inside square boxes using python? 尝试在 Python 中使用 Turtle 绘制棋盘格 - 如何填充每隔一个方块? - Trying to draw a checkerboard using Turtle in Python - how do I fill in every other square? 如何根据 python 中的行值生成 id 列? - How do I generate id column based on row values in python? Python:如何检查每一行和每一列的值? - Python: How do I check for the values of each row and column? 如何删除列中所有值的方括号? - How do I remove square brackets for all the values in a column? 如何使用 Python 中同一列的值填充范围中的列? - How do I fill a column in ranges with values from the same column in Python? python:字典键作为行索引,值作为列标题。 如何使用字典引用并选择 df 中的特定值? - python: Dictionary key as row index, values as column headers. How do I refer back and select specific values in a df using a dictionary? 如何用开放的简历填充盒子内的盒子 - how to fill boxes inside boxes with open cv 如何用两个值的总和在特定列中填充 NA? - How do I fill NA in specific column with the sum of two values? 如何使用 python 创建幻方矩阵 - How do I create a magic square matrix using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM