2013-05-27 3 views
0

Java 할당 - Minesweeper 게임 클론에서 작업 중입니다. 게임 끝 부분에 "Game Wow!"/ "Game Lost"창을 표시하는 역할을하는 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

당신은를 호출, 하나의 방법이 있어야합니다, 언뜻보기에는 두 개의 인수, 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

: 나 같은 부울 게임이 원 문자열 설정 방법에 부울 검사를 한 경우이다 매개 변수, 뭔가를 복용하여 매개 변수로 제목과 메시지 코드에서 좀 더 자세히 살펴보면 부울 또는 문자열 매개 변수를 다른 답변에서 제안 된 대로만 전달하는 것으로 충분하지 않습니다. 해야 할 일은 코드 공통 코드와 다른 코드 (모두 2 개)을 식별하는 것입니다. 귀하의 경우에는, 나는이 함께 올 것 :

  • 제목
  • 메시지
  • 버튼 1 메시지
  • 버튼 1 수신기
  • 버튼 2 메시지
  • 버튼이 리스너

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

따라서 깔끔한 작은 방법을 사용할 수 있습니다 을 다시 표시하려면 두 개의 버튼 창을 표시하십시오.