如何将我的球员目前的位置放在董事会上


想把我的球员和墙的位置放在我的板上,但我不知道怎么做。我认为这不是建造空板并在之后添加元素的正确方法。我举了一个应该如何的例子。我只是想理解,所以如果有人能把我放在正确的位置,那就太好了。

在这种情况下,最好定义一个矩阵(表示数组的数组)来保存网格的当前状态。例如,您可以使用int数组的数组,其中0:表示空,1:表示第一个玩家,2:表示第二个玩家。

当您需要显示网格时,可以使用一个函数将int值转换为语义内容。

这是一个非常简单的例子:


SIZE = 9
players = [
{"nom": "1", "pos": [5, 5]},
{"nom": "2", "pos": [8, 6]}
]
def build_grid(size,value_default=0): 
grid = []
for _ in range(size): 
line = []
for _ in range(size):
line.append(value_default) 
grid.append(line)
return grid
def display_grid(grid):
for i in range(len(grid)): 
line = grid[i]
print(str(i+1)+" ",end="")
for j in range(len(line)):
print(str(line[j])+" ",end="")
print()
def update_grid(grid,position_ij,value): 
i = position_ij[0]
j = position_ij[1]
grid[i][j] = value
return grid
# basic use
grid = build_grid(SIZE)
display_grid(grid)
grid = update_grid(grid,[5,6],1)
display_grid(grid)
# use with your dict 
grid = build_grid(SIZE)
display_grid(grid)
grid = update_grid(grid,players[0]["pos"],str(players[0]["nom"]))
display_grid(grid)

PouceHeure。

您应该创建一个算法,该算法将逐行生成网格,并在生成网格时将块插入其正确位置。

例如:

def add_joueurs(joueurs):
"""creates the inner grid and places joueurs in positions"""
grid = [['.' for _ in range(9)] for _ in range(9)]
for joueur in joueurs:
c,r =  joueur["pos"]
grid[r - 1][c-1] = joueur["nom"]
return grid
def build_grid(joueurs):
"""creates the boundaries of the grid"""
blank = "  |" + (" " * 29) + "| "
grid = add_joueurs(joueurs)
lines = ["  " + ('-'* 31)]
for i in range(9,0,-1):
line = f"{i} |  " + "  ".join(grid[i-1]) + "  | "
lines.append(line)
if i > 1:
lines.append(blank)
lines.append("- | " + ("-"*27) + " | -")
lines.append("  |  " + "  ".join([str(i) for i in range(1,10)]) + "  | ")
return "n".join(lines)

然后,您可以使用字典列表作为函数的输入参数,并为您生成板。

joueurs = [
{"nom": "1", "pos": [5, 5]},
{"nom": "2", "pos": [8, 6]}
]
print(build_grid(joueurs))

输出:

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

的另一个例子

joueurs = [
{"nom": "1", "pos": [5, 5]},
{"nom": "2", "pos": [8, 6]},
{"nom": "3", "pos": [3, 2]},
{"nom": "4", "pos": [4, 1]},
{"nom": "5", "pos": [9, 0]},
]
print(build_grid(joueurs))

输出:

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

最新更新