2013-05-27 123 views
0

我正在開發我的Java任務 - 掃雷遊戲克隆。我有兩個幾乎完全相同的(只有文本標籤和文本框架不同的)方法gameWon()和gameLost(),它們負責在遊戲結束時顯示「遊戲贏了!」/「遊戲迷失」窗口。我知道代碼重複是不好的做法,所以我想優化它。問題是,我對OOP有點新鮮,我不確定如何去做。也許我可以將這些方法合併成某種方式,在不同的情況下采取不同的行爲,或者繼承可能會有用。我真的不知道,希望你們中的一些人能夠幫助我一點。感謝您的回答。Java OOP優化代碼

下面是這些方法的代碼:

GAMEOVER

public static void gameOver() { 

     F1 = new JFrame("Game Over"); 
     F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     F1.setSize(360, 120); 
     Container content = F1.getContentPane(); 
     content.setBackground(Color.white); 
     content.setLayout(new FlowLayout()); 

     JLabel textLabel = new JLabel("Sorry, you have lost this game! Better luck next time.",SwingConstants.CENTER); 
     textLabel.setPreferredSize(new Dimension(360, 40)); 
     content.add(textLabel, BorderLayout.CENTER); 

     JButton button = new JButton("Exit"); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       System.exit(0); 
      } 
     }); 
     content.add(button); 

     button = new JButton("Restart This Game"); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       F1.dispose(); 
       Board.doRepaint(); 
      } 
     });   
     content.add(button); 

     button = new JButton("Play Again"); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       F1.dispose(); 
       restartGame(); 
      } 
     });   
     content.add(button); 

     F1.setLocationRelativeTo(null); 
     F1.setVisible(true); 
    } 

gameWon

public static void gameWon() { 
    F1 = new JFrame("Game Won"); 
    F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    F1.setSize(360, 120); 
    Container content = F1.getContentPane(); 
    content.setBackground(Color.white); 
    content.setLayout(new FlowLayout()); 

    JLabel textLabel = new JLabel("Congratulations, you have won the game!",SwingConstants.CENTER); 
    textLabel.setPreferredSize(new Dimension(360, 40)); 
    content.add(textLabel, BorderLayout.CENTER); 

    JButton button = new JButton("Exit"); 
    button.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) 
    { 
     System.exit(0); 
    } 
    }); 
    content.add(button); 

    button = new JButton("Restart This Game");  
    button.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) 
    { 
     F1.dispose(); 
     Board.doRepaint(); 
    } 
    });  
    content.add(button); 

    button = new JButton("Play Again"); 
    button.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) 
    { 
     F1.dispose(); 
     restartGame(); 
    } 
    });  
    content.add(button); 

    F1.setLocationRelativeTo(null); 
    F1.setVisible(true); 
} 
+0

你可以創建一個通用的'GameComplete'版本,它需要幾個字符串。這將允許您重複使用相同的代碼並顯示不同的文本 – Craig

回答

0
public static void gameEnd(boolean hasWon) { 

    String title = hasWon ? "Game Won" : "Game Over"; 
    F1 = new JFrame(title); 
    F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    F1.setSize(360, 120); 
    Container content = F1.getContentPane(); 
    content.setBackground(Color.white); 
    content.setLayout(new FlowLayout()); 

    String message = hasWon ? "Congratulations, you have won the game!" : 
      "Sorry, you have lost this game! Better luck next time."; 
    JLabel textLabel = new JLabel(message,SwingConstants.CENTER); 
    textLabel.setPreferredSize(new Dimension(360, 40)); 
    content.add(textLabel, BorderLayout.CENTER); 

    JButton button = new JButton("Exit"); 
    button.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) 
     { 
      System.exit(0); 
     } 
    }); 
    content.add(button); 

    button = new JButton("Restart This Game"); 
    button.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) 
     { 
      F1.dispose(); 
      Board.doRepaint(); 
     } 
    });   
    content.add(button); 

    button = new JButton("Play Again"); 
    button.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) 
     { 
      F1.dispose(); 
      restartGame(); 
     } 
    });   
    content.add(button); 

    F1.setLocationRelativeTo(null); 
    F1.setVisible(true); 
} 
1

你應該只有一個方法,調用它gameOver(....),乍一看,你只需要兩個參數,titlemessage。然後,更改只有兩行代碼:

public static void gameOver(final String title, final String message) { 
    ..... 
    F1 = new JFrame(title); 
    ..... 
    JLabel textLabel = new JLabel(message ,SwingConstants.CENTER); 
} 

然後,而不是調用兩種方法,調用相同的方法使用不同的參數:

gameOver("Game Won", "Congratulations, you have won the game!"); 
1

你可以做最簡單的事情就是把字符串標題和消息作爲參數,或採取任何表示,如果比賽已經贏了,在設定的字符串的方法的布爾測試一個布爾參數,喜歡的東西:

public static void gameOver(boolean won) { 
    .... 
    F1 = new JFrame(won?"Game Won":"Game Over"); 
    .... 
} 
0

看起來有點在代碼上更接近,僅僅按照其他答案中的建議來傳遞布爾或String參數是不夠的。你需要的工作是確定所有(這裏:兩個)方法的通用代碼和不同的。在你的情況,我會想出這樣的:

  • 標題
  • 消息
  • 按鈕1消息
  • 按鈕1監聽
  • 按鈕2消息
  • 按鈕2監聽

    public static void showTwoButtonMessage(String title, String message, 
    String button1Message, ActionListener listener1, 
    String button2Message, ActionListener listener2){ 
    //... 
    } 
    

因此,你有一個整潔的小方法,你可以重新使用來顯示任何兩個按鈕窗口。