2014-12-19 86 views
-3

我一直在尝试编写“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; 
      } 
     } 
    } 
} 
+1

使用一个调试器或分析器来确定它被卡住的位置(例如,只需要运行一段时间调试器,然后暂停它,看看堆栈在哪里)。我猜想它会在某个地方陷入无限循环。 – lmm

+0

您可以在其中放置跟踪代码,以更改窗口上的标题,以便知道停止的位置。 – idstam

+1

请优化您的问题以指出您的代码中最相关的部分。 –

回答

0

的问题确实是在你的合法性检查,特别是在你的底线是永不有效。

第一次运行时,你生成一个5个字符的字符串,它是无效的,然后你再次启动,但你永远不会重置该字符串,而是继续追加它,使其不断增长。我修改你的程序输出的每个底排它检查,这是我所得到的...

Checking bottom row: 00001 
Checking bottom row: 0000100000 
Checking bottom row: 000010000000000 
Checking bottom row: 00001000000000000000 
Checking bottom row: 0000100000000000000000000 
Checking bottom row: 000010000000000000000000000000 
Checking bottom row: 00001000000000000000000000000000001 

您需要重置数据结构,当你重新董事会是空的。

作为一种风格,过度使用类级别的变量会伤害您的调试能力,因此您应该尽量保持数据在本地的可用性。例如,放弃使用静态变量并改变实例变量,让你的方法返回数据而不是仅仅设置它

+0

我试图在循环之前和调用方法之前添加bottomRow的重置,但没有更改。谢谢你的风格提示,但由于某种原因,我似乎总是有退货问题。 –

+0

没有重置,从来没有一个点产生进一步的序列。 bottomRow变量永远不会在方法外部读取,所以摆脱公共静态字符串bottomRow =“”;而是在第一次使用它的checkValidity方法中包含String bottomRow =“”。这不会完全解决您的问题,因为您在该方法内的实际逻辑中也存在问题 – tddmonkey

+0

特别是您的checkValidity方法正在修改它应该检查的数据,因此无论您的颜色数组中有什么值,它会切换到[false,false,false,false,false]或[false,false,false,false,true] - 它永远不会产生正确的序列。不知道你究竟在做什么,我将无法提供更多的帮助 – tddmonkey