Java LightsOut板随机化器不可解
我正在尝试重新创建熄灯和一个棋盘随机发生器来开始游戏。当我使用boardRandomizer时,我仍然发现电路板无法求解,即使我的代码从全关闭位置启动电路板,然后使用Math.Random随机切换灯光x次Java LightsOut板随机化器不可解,java,Java,我正在尝试重新创建熄灯和一个棋盘随机发生器来开始游戏。当我使用boardRandomizer时,我仍然发现电路板无法求解,即使我的代码从全关闭位置启动电路板,然后使用Math.Random随机切换灯光x次 final int intBoardSize = 5; boolean[][] boolLightState = new boolean[intBoardSize][intBoardSize]; public void boardRandomize() { for (int ro
final int intBoardSize = 5;
boolean[][] boolLightState = new boolean[intBoardSize][intBoardSize];
public void boardRandomize() {
for (int row = 0; row < intBoardSize; row++)
for (int column = 0; column < intBoardSize; column++)
boolLightState[row][column] = false;
int randomRow, randomColumn;
for (int randomCount = 0; randomCount <= 50; randomCount++) {
randomRow = (int) (Math.random() * intBoardSize);
randomColumn = (int) (Math.random() * intBoardSize);
toggleLight(randomRow, randomColumn);
}
}
public void mouseToggleLight (int x, int y) {
for (int row = 0; row < intBoardSize; row++)
for (int column = 0; column < intBoardSize; column++)
if ((Math.sqrt (Math.pow ((y - intLightPosition [1][row][column] - intLightRadius), 2) + Math.pow((x - intLightPosition [0][row][column] - intLightRadius), 2))) < intLightRadius)
toggleAdjacentLights(row, column);
}
public void toggleAdjacentLights(int row, int column) {
toggleLight(row, column);
if (row + 1 >= 0 && row + 1 < intBoardSize)
toggleLight(row + 1, column);
if (row - 1 >= 0 && row - 1 < intBoardSize)
toggleLight(row - 1, column);
if (column + 1 >= 0 && column + 1 < intBoardSize)
toggleLight(row, column + 1);
if (column - 1 >= 0 && column - 1 < intBoardSize)
toggleLight(row, column - 1);
}
public void toggleLight(int row, int column) {
if (boolLightState[row][column] == false)
boolLightState[row][column] = true;
else
boolLightState[row][column] = false;
repaint();
}
final int intBoardSize=5;
boolean[][]boolLightState=新的boolean[intBoardSize][intBoardSize];
公共空白板随机化(){
对于(int row=0;row=0和行-1=0&&column+1=0&&column-1
您如何确定电路板无法解决?我建议记录随机发生器执行的所有步骤,然后向后执行。因为有一套解决方案已经被许多人计算出来。通过将所有灯光移动到板的底部一行,只有大约5种不同的组合可能会出现,而我不断得到不可能出现的组合。
for (int randomCount = 0; randomCount <= 50; randomCount++) {
randomRow = (int) (Math.random() * intBoardSize);
randomColumn = (int) (Math.random() * intBoardSize);
toggleLight(randomRow, randomColumn);
}
for (int randomCount = 0; randomCount <= 50; randomCount++) {
randomRow = (int) (Math.random() * intBoardSize);
randomColumn = (int) (Math.random() * intBoardSize);
toggleAdjacentLights(randomRow, randomColumn);
}
import java.util.Arrays;
class Board {
public static void main(String[] args) {
Board board = new Board();
boardRandomize(board);
System.out.println(Arrays.deepToString(board.boolLightState));
}
public static void boardRandomize(Board board) {
board.resetBoard();
int randomRow, randomColumn;
for (int randomCount = 0; randomCount <= 50; randomCount++) {
randomRow = (int) (Math.random() * board.intBoardSize);
randomColumn = (int) (Math.random() * board.intBoardSize);
board.toggleAdjacentLights(randomRow, randomColumn);
}
}
private final int intBoardSize = 5;
private boolean[][] boolLightState = new boolean[intBoardSize][intBoardSize];
public void resetBoard() {
for (int row = 0; row < intBoardSize; row++)
for (int column = 0; column < intBoardSize; column++)
boolLightState[row][column] = false;
}
public void toggleAdjacentLights(int row, int column) {
toggleLight(row, column);
if (row + 1 >= 0 && row + 1 < intBoardSize)
toggleLight(row + 1, column);
if (row - 1 >= 0 && row - 1 < intBoardSize)
toggleLight(row - 1, column);
if (column + 1 >= 0 && column + 1 < intBoardSize)
toggleLight(row, column + 1);
if (column - 1 >= 0 && column - 1 < intBoardSize)
toggleLight(row, column - 1);
}
//make private to prevent access
private void toggleLight(int row, int column) {
if (boolLightState[row][column] == false)
boolLightState[row][column] = true;
else
boolLightState[row][column] = false;
// all of the above can be simplified to
// boolLightState[row][column] = !boolLightState[row][column];
repaint();
}
}