Home > Software engineering >  how to put the current position of my players on the board?
how to put the current position of my players on the board?

Time:11-17

def formater_damier(joueurs):
    joueurs = [
        {"nom": "1", "pos": [5, 5]},
        {"nom": "2", "pos": [8, 6]}
    ]
     grille = (
    (   '    ----------------------------------- \n'
        '9 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '8 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '7 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '6 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '5 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '4 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '3 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '2 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '1 |  .   .   .   .   .   .   .   .   .  | \n'
'--| ----------------------------------- \n'
       f'  |  1   2   3   4   5   6   7   8   9 \n'))
    return grille

    example  grille = (
    (   '    ----------------------------------- \n'
        '9 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '8 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '7 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '6 |  .   .   .   .   .   .   .   2   .  | \n'
        '  |                                     | \n'
        '5 |  .   .   .   .   1   .   .   .   .  | \n'
        '  |                                     | \n'
        '4 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '3 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '2 |  .   .   .   .   .   .   .   .   .  | \n'
        '  |                                     | \n'
        '1 |  .   .   .   .   .   .   .   .   .  | \n'
'--| ----------------------------------- \n'
       f'  |  1   2   3   4   5   6   7   8   9 \n'))

i would like to put the position of my players and my walls on my board, but I don't know how to do it. I don't think that's the right way to build the empty board and add the elements afterward. I made an example of how it is supposed to be. I just want to understand, so if someone can put me in the right way, that's will be great.

CodePudding user response:

In this case it's probably better to define a matrix (means array of array) where you save the current state of the grid. For example you can use an array of array of int, where 0: means empty, 1: means first player et 2: means the second one.

And when you need to show the grid, you can use a function translating int value to a semantic thing.

This a very simple example:


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.

CodePudding user response:

You should create an algorithm that will generate the grid row by row, inserting the pieces in their correct position as grid is generated.

For example:

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)

then you can use your list of dictionaries as input argument to the function and the board is generated for you.

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

output:

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

Another example

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

output:

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