how to change items in a loop as if the letter is hidden behind the question mark

Question:

Is it possible to select a specific question marks from a loop and replace it with random.choice(letters) when chosen?
For example:

  0 1 2
0 ? ? ?
1 ? ? ?
2 ? ? ?

User inputs 11 for example:

  0 1 2
0 ? ? ?
1 ? M ?
2 ? ? ?

This is what i have done to show the board, but I have no clue how to select each question marks when the user input e.g (01 (0row 1column))


def create_game_board(rows, cols):
    board = dict()
    # save dimensions inside the dict itself
    board['cols'] = cols
    board['rows'] = rows
    for y in range(rows):
        for x in range(cols):
            # add random letter to board at (x,y)
            # x,y makes a tuple which can be a key in a dict
            # changed to use string.ascii_uppercase so that you don't forget any letter
            board[x, y] = random.choice(string.ascii_uppercase)

    # change last element to @ when both dimensions are odd
    if (rows * cols) % 2 == 1:
        board[rows-1, cols-1] = "?"
    return board

def display_board(board):
    # get dimensions
    cols, rows = board['cols'], board['rows']
    # print header
    print(' '*30+" ".join([' '] + [str(x) for x in range(cols)]))
    for y in range(rows):
        # print rows
       # print(' '.join([str(y)] + [board[x, y] for x in range(cols)]))  # to display the actual letter at this location
        print(' '*30+" ".join([str(y)] + ['@' if board[x, y] == '?' else '?' for x in range(cols)])) # using your display function
    print()  # separator empty line


board = create_game_board(rows[0], columns[0])
display_board(board)

def choice():
    print('Hello ' + player[0]+"!")
    cellnumber = input("Which cell do you want to open? (rowcolumn)")
    print(cellnumber)
choice()
Asked By: bob

||

Answers:

You can try to update the choice function to:

def choice(board):
    print('Hello ' + player[0]+"!")
    cellnumber = input("Which cell do you want to open? (rowcolumn)")
    row = int(cellnumber[0])
    col = int(cellnumber[1])
    board[row, col] = random.choice(string.ascii_uppercase)
    display_board(board)
Answered By: Nova

First, I suggest simplifying the majority of your code with the itertools module. To change get the user input (assuming it is valid), use a list comprehension to convert each character to an integer, destructure the list into col and row, then change board[col, row] to random.choice(string.ascii_uppercase). Like this:

import itertools
import random
import string


def create_board(cols, rows):
    board = {k: '?' for k in itertools.product(range(cols), range(rows))}
    board['dims'] = (cols, rows)
    return board


def display_board(board):
    cols, rows = [int(x) for x in board['dims']]
    print(' ', *(str(x) for x in range(cols)))
    for row in range(rows):
        print(row, *(board[col, row] for col in range(cols)))


board = create_board(3, 3)
while True:
    display_board(board)
    row, col = [int(ch) for ch in input('Which cell do you want to open? (rowcolumn) ')]
    board[col, row] = random.choice(string.ascii_uppercase)

I also added an infinite loop to keep the program going, but you can change this to fit the constraints of your program.

Answered By: Michael M.
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.