我一直在尝试编写“Lights Out”游戏并对游戏及其机制进行了编码。我唯一的问题是每个难题都无法解决的问题。我试图编写一种方法来检查它为解决问题而创建的难题,但是当我运行游戏时,现在什么都没有显示出来。游戏永远不会运行。我相信它卡在checkValidity()方法的某个地方,但我不知道在哪里。任何帮助?“熄灯”有效性检查
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.util.Random;
public class buildBoard {
public static JButton[][] board = new JButton[5][5];
public static boolean[][] color = new boolean[5][5];;
JFrame frame = new JFrame();
public static UIManager UIManager = new UIManager();
public static int boardWidth, boardHeight, numBlack, moves = 0;
public static String bottomRow = "";
public static boolean isSolvable = false;
public buildBoard(final int width, final int height){
boardWidth = width;
boardHeight = height;
frame.setLayout(new GridLayout(5, 5));
boolean[][] color = new boolean[boardWidth][boardHeight];
for(int a = 0; a < width; a++){
for(int b = 0; b < height; b++){
board[a][b] = new JButton();
}
}
generateLights();
while(isSolvable == false){
generateLights();
checkValidity();
if(isSolvable == false){
for(int c = 0; c < boardWidth; c++){
for(int d = 0; d < boardHeight; d++){
color[c][d] = false;
}
}
}
}
for(int a = 0; a < width; a++){
for(int b = 0; b < height; b++){
board[a][b].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moves++;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++){
if(e.getSource()==board[i][j]){ //gameButtons[i][j] was clicked
if(board[i][j].getBackground() == Color.BLUE){
board[i][j].setBackground(Color.BLACK);
}
else{
board[i][j].setBackground(Color.BLUE);
}
if(j > 0 && (board[i][j-1].getBackground() == Color.BLUE)){
board[i][j-1].setBackground(Color.BLACK);
}
else if(j > 0){
board[i][j-1].setBackground(Color.BLUE);
}
if(i > 0 && board[i-1][j].getBackground() == Color.BLUE){
board[i-1][j].setBackground(Color.BLACK);
}
else if (i > 0){
board[i-1][j].setBackground(Color.BLUE);
}
if(i < width-1 && board[i+1][j].getBackground() == Color.BLUE){
board[i+1][j].setBackground(Color.BLACK);
}
else if(i < width-1){
board[i+1][j].setBackground(Color.BLUE);
}
if(j < height-1 && board[i][j+1].getBackground() == Color.BLUE){
board[i][j+1].setBackground(Color.BLACK);
}
else if(j < height-1){
board[i][j+1].setBackground(Color.BLUE);
}
checkIfWon();
}
}
}
}
});
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { }
frame.add(board[a][b]);
}
}
frame.setTitle("Lights Out!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(250, 250));
frame.pack();
frame.setVisible(true);
}
public void checkIfWon(){
int numBlack = 0;
for(int i = 0; i < boardWidth; i++){
for(int j = 0; j < boardHeight; j++){
if(board[i][j].getBackground() == Color.BLACK){
numBlack++;
}
}
}
if(numBlack == 25){
JOptionPane.showMessageDialog(null, "Hooray! You Won!\nYou won in " + moves + " moves.");
System.exit(0);
}
}
public static void checkValidity(){
for(int j = 0; j < boardHeight - 1; j++){
for(int i = 0; i < boardWidth; i++){
if(color[i][j] == true){
if(color[i][j] == true){
color[i][j] = false;
}
else{
color[i][j] = true;
}
if(j > 0 && color[i][j+1] == true){
color[i][j+1] = false;
}
else if(j > 0){
color[i][j+1] = true;
}
if(i > 0 && color[i-1][j+1] == true){
color[i-1][j+1] = false;
}
else if (i > 0){
color[i-1][j+1] = true;
}
if(i < boardWidth-1 && color[i+1][j+1] == true){
color[i+1][j+1] = false;
}
else if(i < boardWidth-1){
color[i+1][j+1] = true;
}
if(j < boardHeight-2 && color[i][j+2] == true){
color[i][j+2] = false;
}
else if(j < boardHeight-2){
color[i][j+2] = true;
}
}
}
}
for(int c = 0; c < boardWidth; c++){
if(color[4][c] == true){
bottomRow += "1";
}
else{
bottomRow += "0";
}
}
if(bottomRow.equals("10001") || bottomRow.equals("01010") || bottomRow.equals("11100") || bottomRow.equals("00111") || bottomRow.equals("10110") || bottomRow.equals("01101") || bottomRow.equals("11011")){
isSolvable = true;
}
else{
isSolvable = false;
}
}
public static void generateLights(){
Random random = new Random();
for(int a = 0; a < boardWidth; a++){
for(int b = 0; b < boardHeight; b++){
if(random.nextInt(99)+1 > 75){
board[a][b].setBackground(Color.BLUE);
color[a][b] = true;
}else{
board[a][b].setBackground(Color.BLACK);
color[a][b] = false;
}
}
}
}
public static void main(String []args){
new buildBoard(5, 5);
}
}
的checkValidity()方法的问题,并张贴在这里(该方法的代码剩下的就是上面,以防万一):
public static void checkValidity(){
for(int j = 0; j < boardHeight - 1; j++){
for(int i = 0; i < boardWidth; i++){
if(color[i][j] == true){
if(color[i][j] == true){
color[i][j] = false;
}
else{
color[i][j] = true;
}
if(j > 0 && color[i][j+1] == true){
color[i][j+1] = false;
}
else if(j > 0){
color[i][j+1] = true;
}
if(i > 0 && color[i-1][j+1] == true){
color[i-1][j+1] = false;
}
else if (i > 0){
color[i-1][j+1] = true;
}
if(i < boardWidth-1 && color[i+1][j+1] == true){
color[i+1][j+1] = false;
}
else if(i < boardWidth-1){
color[i+1][j+1] = true;
}
if(j < boardHeight-2 && color[i][j+2] == true){
color[i][j+2] = false;
}
else if(j < boardHeight-2){
color[i][j+2] = true;
}
}
}
}
for(int c = 0; c < boardWidth; c++){
if(color[4][c] == true){
bottomRow += "1";
}
else{
bottomRow += "0";
}
}
if(bottomRow.equals("10001") || bottomRow.equals("01010") || bottomRow.equals("11100") || bottomRow.equals("00111") || bottomRow.equals("10110") || bottomRow.equals("01101") || bottomRow.equals("11011")){
isSolvable = true;
}
else{
isSolvable = false;
}
}
public static void generateLights(){
Random random = new Random();
for(int a = 0; a < boardWidth; a++){
for(int b = 0; b < boardHeight; b++){
if(random.nextInt(99)+1 > 75){
board[a][b].setBackground(Color.BLUE);
color[a][b] = true;
}else{
board[a][b].setBackground(Color.BLACK);
color[a][b] = false;
}
}
}
}
使用一个调试器或分析器来确定它被卡住的位置(例如,只需要运行一段时间调试器,然后暂停它,看看堆栈在哪里)。我猜想它会在某个地方陷入无限循环。 – lmm
您可以在其中放置跟踪代码,以更改窗口上的标题,以便知道停止的位置。 – idstam
请优化您的问题以指出您的代码中最相关的部分。 –