2015-07-22 21 views
0

我的Boggle項目存在一些邏輯錯誤,無法查明它們。我唯一能做的就是退出遊戲。誰能幫我嗎?在這個程序中,每當點擊JMenu Item新遊戲時,我需要開始一個新遊戲,每當單擊Shake Dice按鈕時隨機選擇的字母,包含提交單詞Jbutton以及分數和當前單詞jlabels的Jpanel,以及當點擊提交文字按鈕時更新當前文字標籤並更新分數。我該如何確定Boggle遊戲中的邏輯錯誤,以及如何修復它們?

驚奇UI代碼:

package userInterface; 

/** 
* 
* @author Zac 
*/ 

import core.Board; 
import core.Die; 
import javax.swing.*; 
import javax.swing.Timer; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 


public class BoggleUi extends JFrame{ 

    Board boggleBoard;//creating a board object 

    //components 

    private static JMenuBar menuBar;//declare menubar 

    private static JMenu Boggle;// declare Jmenu 

    private static JMenuItem game;// set menu item for game 

    private static JMenuItem exit;// set menu item for exit 

    //buttons 

    private static JButton Shakedice;//button for shaking the dice 

    private static JButton submitWord;// button to submit word 

    //layout of the UI 

    private JPanel diceButtons;//jpanel for the dice buttons 

    private JPanel componentLayout;//jpanel for other components 

    private JPanel wordPanel;// Jpanel for word related ui 

    private static JScrollPane scrollPane;//scrollpane 

    private static JTextArea textArea;//textarea 

    private static JLabel timeLeft;// Jlabel for countdown 

    private static JLabel timer;//label for above time left 

    private static JLabel wordsLeft;//label for words entered in text area 

    private static JLabel currentWord;//label for current word 

    private static JLabel playerScore;// label for score 

    private static JLabel dieButton;// label for dice button 

    //handlers 

    private GameListener gameListener;// listener for the Boggle game 

    private PanelListener panelListener;// listener for dice 

    //timer variables 

    int timeMili; //miliseconds 

    int timeMin;// minutes 

    int timeSec;//seconds 

    Timer countdownTimer;//timer 

public BoggleUi(Board boggleBoard,ArrayList Readdictionary){ 

    this.boggleBoard = boggleBoard;//calls the boggle board 

    initComponents(); 

} 


public void initComponents(){ //initialzes components 

    this.setSize(800,800);//set size of JFrame 

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);//remove window if closed 

    menuBar = new JMenuBar();//creates a menu bar 

    Boggle = new JMenu("Boggle"); 

    game = new JMenuItem("New Game");// menu item for new game 

     exit = new JMenuItem("Exit");// menu item for exiting the game 

     exit.addActionListener(new ExitListener());//adding an action listener to the exit menu item 

     Boggle.add(game); 

     Boggle.add(exit); 

     menuBar.add(Boggle); 

     this.setJMenuBar(menuBar); 

     //actionlistener 


    gameListener = new GameListener();//make a new game listener 

    game.addActionListener(gameListener);//add game listener 

    panelListener = new PanelListener();//make new panel listener 

    Boggle.addActionListener(panelListener);// add panel listener 

    Shakedice = new JButton("Shake Dice"); 

    Shakedice.addActionListener(new ShakeListener());// make shake listener 

    submitWord = new JButton("Submit Word"); 

    submitWord.addActionListener(new SubmitWordListener());// make submit listener 

    //inititialize timer and listener 

    countdownTimer = new Timer(1000, new TimerListener());//setting timer and listener 

    countdownTimer.start();// start timer 

     //set up textarea and jscroll pane and other components 

     Dicepanels();//set up for panels and components 

     textArea = new JTextArea();//creates new text area 

     textArea.setLineWrap(true);//wraps text 

     scrollPane = new JScrollPane(textArea);//creates new scroll pane 

     scrollPane.setMinimumSize(new Dimension(200, 300));// set minimum size 

     scrollPane.setPreferredSize(new Dimension(200, 300));// preferred size of scroll pane 

     scrollPane.setMaximumSize(new Dimension(400, 600));//set max size 

     scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);//never allows horizontal bar 

     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//calls for vertical bar 

     componentLayout.setLayout(new BoxLayout(componentLayout,BoxLayout.Y_AXIS));//sets to y axis 

     componentLayout.add(wordsLeft); 

     componentLayout.add(scrollPane);//appears after a certain amount of lines are entered 

     componentLayout.add(timer);// adds time left label 

     componentLayout.add(timeLeft);//adds time label 

     componentLayout.add(Shakedice);//adds shake dice button 

     this.add(diceButtons);//adds dice buttons 

     this.add(componentLayout,BorderLayout.EAST);//adds the components 

     this.add(wordPanel,BorderLayout.SOUTH);//adds word panel 

     this.setVisible(true);// makes the entire jframe visible 
} 



private JFrame getThisParent(){// method to retrieve the JFrame 

    return this; 
} 

//code for text area 
public void updateText(String boggleData){//method to update the text 


     textArea.append(boggleData+ "\n"); 

    textArea.setCaretPosition(textArea.getDocument().getLength()); 



} 

//area to place dice buttons and other components 

    private void Dicepanels() { 

     //setup dice buttons 

     diceButtons = new JPanel(new GridLayout(4, 4));// set buttons in a 4x4 grid 

     diceButtons.setMinimumSize(new Dimension(400, 400));//set minimum size 

     diceButtons.setPreferredSize(new Dimension(400, 400));//set preferred size 

     //setup components 

     componentLayout = new JPanel(); 

     wordsLeft = new JLabel("Enter Words Found"); 

     timer = new JLabel("Time Left"); 

     timeLeft = new JLabel("3:00");//JLabel for timer 

     timeLeft.setMinimumSize(new Dimension(500,500));//sets size for label 

     Shakedice = new JButton("Shake Dice"); 

     //dimensions for the shake dice button 

     Shakedice.setMinimumSize(new Dimension(120, 60)); 

     Shakedice.setPreferredSize(new Dimension(120, 60)); 

     Shakedice.setMaximumSize(new Dimension(120, 60)); 

     Shakedice.setFont(null);//font size 

     //setup Word panel 

     wordPanel = new JPanel(); 

     currentWord = new JLabel();// label to show current word 

     playerScore = new JLabel("Score:" + "0");// label that shows the player's score 

     submitWord = new JButton("Submit Word"); 

     //dimensions for the submit word button 

     submitWord.setMinimumSize(new Dimension(100, 50)); 

     submitWord.setPreferredSize(new Dimension(100, 50)); 

     submitWord.setMaximumSize(new Dimension(100, 50)); 

     submitWord.setFont(null);//font size 

     //JButton ArrayList 



    ArrayList <Die> dice = boggleBoard.shakeDice(); 

     int counter = 0;  

       for(int row = 0; row < 4; row++){ 

        for(int col = 0; col < 4; col++){ 

     Die die = dice.get(counter); 

     JButton button = new JButton(); 

     button.setText(die.getLetter()); 

     button.putClientProperty("letter",die.getLetter()); 

     button.putClientProperty("row",row); 

     button.putClientProperty("col",col); 

     diceButtons.add(button); 

     diceButtons.setFont(null); 

     counter++; 

     } 
    }  


     } 


    private class ExitListener implements ActionListener { //action for exiting 

     public void actionPerformed(ActionEvent e) 

     { 

      //dialogue to confirm quiting the game 

      int response = JOptionPane.showConfirmDialog(null, "Confirm to exit Boggle?", 
        "Exit?", JOptionPane.YES_NO_OPTION); 

      if (response == JOptionPane.YES_OPTION) 

      System.exit(0);//exit game 

     } 


    } 




private class GameListener implements ActionListener{//implement actionlistener for game 

    public void actionPerformed(ActionEvent e){ 


     textArea.revalidate();//revailidate text area 

     timeLeft.repaint(); //repaint time left Jpanel 

     Shakedice.setEnabled(true);//reenable shake dice button 

} 




     } 


    private class ShakeListener implements ActionListener{ 




     public void actionPerformed(ActionEvent e){ 

     boggleBoard.shakeDice();//call shake dice method into shake listener 

     //Random randomDie = new Random();//new radomization 

     //String placement = randomDie.(diceButtons); 

     diceButtons.revalidate();// revalidates dice buttons 

     playerScore.repaint();//reset score 

     playerScore.setText("Score:" + "0");// set to 0 

     currentWord.repaint();// reset current word 

     timeLeft.repaint();// reset timer 

     Shakedice.setEnabled(false);//disable shake dice button 




     } 


    } 



    private class SubmitWordListener implements ActionListener{ 

     boolean wordValidate = false; 

    public void actionPerformed(ActionEvent e){ 

      if(wordValidate = true){ 

     System.out.println("TemporaryDictionary.txt"); 

     textArea.append("" + currentWord); 

     currentWord.revalidate();//update current word j label 

      } 

     } 


     }   

    private class PanelListener implements ActionListener{ 

     public void actionPerformed(ActionEvent e){ 

      ArrayList <JButton> buttons = new ArrayList<JButton>(); 

      if(e.getSource()instanceof JButton){ 

       //create Jbutton with get properties 

       JButton button = (JButton)e.getSource(); 

       String letter = button.getClientProperty("letter").toString(); 

       int rowClick = (int) button.getClientProperty("row"); 

       int colClick = (int) button.getClientProperty("col"); 

       //get text in label 

       String word = currentWord.getText(); 

       currentWord.setText(word + letter); 

       int MIN_INDEX = 0;// minimum index 

       int MAX_INDEX = 3;// maximum index 

       // enable/disable the buttons 

       int count = 0; 

       for(int row = 0; row < 4;) 

        for(int col = 0; col < 4;){ 

        if(row == rowClick && col == colClick) 

         button.setEnabled(false); 

        if(colClick > MIN_INDEX && colClick < MAX_INDEX && rowClick > MIN_INDEX && rowClick < MAX_INDEX){ 

         buttons.get(count).setEnabled(true); 

         count++; 

        } 

       } 
      } 

     } 

    } 

    private class TimerListener implements ActionListener{ 

     public void actionPerformed(ActionEvent e){ 

      timeMili -= 1000; 

      //conversion into minutes and seconds 

      if(timeSec == 0){ 

       timeSec = 59; 

       timeMin --; 

      } 

      else{ 

       timeSec --; 

      } 

      timeLeft.setText("3:00");//enable time left to start countdown 

     } 

    } 


    } 
+0

請[edit]添加一個特定的問題陳述 - 「它不起作用」可以假設,但*如何*它不起作用?什麼錯誤信息或不正確的行爲是特徵? –

+0

我以爲我說過在這個問題中什麼不起作用。如果我必須重複我自己的一些具體問題,我知道它是如何不能正常工作的。什麼不能正常工作:每當點擊JMenu Item新遊戲時開始一個新遊戲,每當Shake Dice按鈕時隨機選擇字母單擊包含提交單詞Jbutton以及分數和當前單詞jlabels的Jpanel,並且當單擊提交單詞按鈕時更新當前單詞標籤並更新分數。 wordpanel也不出現在南方的位置。 – user3537249

+0

是的,這些具體問題是可能需要的。 –

回答

1

如果你在一個IDE,比如Eclipse運行此,你可以放置一個breakpoint,然後運行你的應用程序和步驟,通過它,就看你的代碼正是真的在做。

如果您無法訪問調試器,則可以簡單地使用System.out.println()調用來報告程序的當前狀態 - 雖然不夠高雅,但它可以工作。