如何解决扫雷游戏中踩踏我的第一步的问题

时间:2019-05-26 02:23:50

标签: java minesweeper

扫雷器中有一条规则,即您不能单击鼠标踩踏地雷。我已经完成了游戏,但是问题在于,在第一次单击之前,地雷将是随机生成的,因此可以通过单击第一次单击地雷。有什么建议么?谢谢!

    //This generates the mines
    public void makeMSMine() {
    int i = 0, coordX, coordY;
    for (; i < mscountMine;) {
        coordX = (int) (Math.random() * msFieldNumber);
        coordY = (int) (Math.random() * msFieldNumber);
        if (map[coordX][coordY] == msEmptyField) {
            map[coordX][coordY] = msMINE;
            i++;
        }
    }
}


@Override
public void actionPerformed(ActionEvent e) {

    Object localObject = e.getSource();
    int xCoord, yCoord;
    String[] tmp_str = ((JButton) localObject).getName().split("_");
    xCoord = Integer.parseInt(tmp_str[0]);
    yCoord = Integer.parseInt(tmp_str[1]);

    if (map[xCoord][yCoord] == msMINE) { //game over when you click on where the mine is randomly generated
        showMessage("Game Over", userNameInput + ", you stepped on a mine~~~");
        flag = true;
        showMSMine();
        return;

    }
    dfs(xCoord, yCoord, 0);
    checkSuccess();
}

0 个答案:

没有答案