gpt4 book ai didi

java - Lights Out board 随机发生器无法解决

转载 作者:行者123 更新时间:2023-11-30 10:08:17 27 4
gpt4 key购买 nike

我正在尝试重新创建 Lights Out 和一个棋盘随机发生器来开始游戏。当我使用我的 boardRandomizer 时,我仍然发现该板无法解决,即使我的代码从完全关闭的位置启动该板,然后随机切换灯 x 次,使用 Math.Random 随机选择灯。

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();

}

最佳答案

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);
}

否则随机化器执行非法移动。

您应该重构您的代码,使 toggleLight 成为 Board 中的私有(private)方法,以防止将来出现此类错误。

考虑以下几点:

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();
}
}

关于java - Lights Out board 随机发生器无法解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806792/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com