[英]How do I fill values inside square boxes in row column format using python for a tictaoe game
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.