Question

I’ve been trying to create a minesweeper game in android, and so far, all has gone accordingly. However, I’m currently stuck on the part where I have to randomly place the mines within the game board.

I’ve tried a few things that I could think of, but none of which worked, except one. However, it doesn’t give me the results that I want. Here is how I am drawing the game board (using a 2D array of buttons).

final Button currentButton = new Button(this);
final int bombState = R.drawable.bomb_state;
final Button[][] buttonArray = new Button[6][6];
final int mine = R.drawable.bomb;
final Random rand = new Random();
final int number = 36;
int button;
int row;

//create new Linear Layout
RelativeLayout linearLayout = new RelativeLayout(this);

//creating the layout Params
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

//set layout background
linearLayout.setBackgroundDrawable(getWallpaper());

//set LinearLayout as a root element of the screen 
setContentView(linearLayout, linLayoutParam);

//create a new Table Layout for the game grid
TableLayout mineGrid = new TableLayout(this);

/*
 * creates TableRows and Buttons using the for loop
 * then add the buttons into the rows and the rows
 * into the TableLayout 
 */

 for(row = 0; row < 6; row++){
    //create new Table Row
    TableRow currentRow = new TableRow(this);
    for(button = 0; button < 6; button++){
        //create new Button

        for(int id = 0; id < number; id++){
            currentButton.setId(id);
        }
        currentButton.setText("      ");

        //storing the buttons into the array of Buttons
        buttonArray[row][button] = currentButton;

        if(currentButton.isClickable()){
        currentButton.setOnClickListener(new OnClickListener() {

            /*
             * (non-Javadoc)
             * @see android.view.View.OnClickListener#onClick(android.view.View)
             */
        public void onClick(View v) {
            try
            {
                for(int i = 0; i < 10; i++){

                    if(rand.nextInt(10) == i){
                         currentButton.setBackgroundResource(mine);
                         restart.setBackgroundResource(bombState);
                    } 
                }
             } catch(Exception e)
               {
                    Toast.makeText(Game.this,e.getMessage() + "Error : ",
                             Toast.LENGTH_SHORT).show();
               }
        }
        });
        }

        //store the button into the Table Row
        currentRow.addView(currentButton);

        }

        //add the newly created row into the table
        mineGrid.addView(currentRow);
    }

    linearLayout.addView(score, params3);

    linearLayout.addView(mineGrid, params);
}

What the above code gives me, is a 6x6 grid made up of buttons. And the following is where I’m trying to randomly place n amount of mines within the board.

try
{
    for(int i = 0; i < 10; i++){

        if(rand.nextInt(10) == i){
             currentButton.setBackgroundResource(mine);
             restart.setBackgroundResource(bombState);
        }
    }
}

Unfortunately, this fills the whole board with mines, instead of only placing n amount of mine on the board. I know am missing something when I try to randomly set the mines! Can anyone advise me as to where I’m going wrong and help point me in the right direction?

Please ask me anything for clarification.

Thanks in advance.

Was it helpful?

Solution

You basically, on every click of a button try to place a mine instead placing them when you create buttons. Maybe You could add to a list, id of a buttons which are mines and only check if user has clicked on one of those buttons.

ArrayList<Integer> mines = new ArrayList<Integer>();
.
.
.
currentButton.setText("      ");
if(rand.nextInt(2)==1)
    mines.add(currentButton.id);

and in onClick() You check if currentButton.id is in mines list and if it is, display appropriate image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top