Search code examples
pythonlist2dminesweeper

Insert value at random place in 2D list


I'm trying to make a minesweeper game using lists in python. I have have this code so far:

import random as r
import sys

#dimension of board and number of bombs
width = int(sys.argv[1])
height = int(sys.argv[2])
b = int(sys.argv[3])

#creates the board
board = [[0.0] * width] * height

#places bombs
for i in range(b):
    x = r.randint(0, width - 1)
    y = r.randint(0, height - 1)
    board.insert(x, y, 0.1)

#prints board
for i in range(len(board)):
    for j in range(len(board[i]))
        print(board[i][j], end=" ")

I'm trying to get the bombs to be placed at random places on the board, but insert() only accepts 2 args. Is there any other way in which I can do this?

I have an idea to place a random bomb in row 1, then a random bomb in row 2, and so on and once it hits row n it loops back to row 1 until enough bombs have been placed, but I'm not sure if it will work (and I have no way of testing it because I have do idea how to do that either). I feel like this solution is pretty inefficient, so I'm also wondering if there's a more efficient way to do it.


Solution

  • You can just use board[x][y] = 0.1 to access index y in row x of your board. Also, you don't want to build a board like that. The way you're doing it will only actually create 1 array with numbers. Here's your code with some modifications.

    import random as r
    
    # dimension of board and number of bombs
    # (I'm using hard coded values as an example)
    width = 5
    height = 7
    b = 10
    
    #creates the board
    board = []
    for i in range(height): # create a new array for every row
        board.append([0.0] * width)
    
    #places bombs
    for i in range(b):
        x = r.randint(0, height - 1)
        y = r.randint(0, width - 1)
        board[x][y] = 0.1 # this is how you place a bomb at a random coordinate
    
    #prints board
    for row in board:
        print(row)
    

    The resulting board for me looks like:

    [0.0, 0.0, 0.1, 0.0, 0.0]                                                                                                                        
    [0.0, 0.0, 0.0, 0.0, 0.0]                                                                                                                        
    [0.0, 0.0, 0.0, 0.0, 0.0]                                                                                                                        
    [0.1, 0.0, 0.1, 0.0, 0.1]                                                                                                                        
    [0.1, 0.0, 0.0, 0.0, 0.1]                                                                                                                        
    [0.0, 0.1, 0.0, 0.0, 0.1]                                                                                                                        
    [0.0, 0.1, 0.0, 0.0, 0.0]
    

    Note that you can end up with less than b bombs in case the random x and y values repeat. That's a good problem to solve next.