2016-05-23 8 views
0

2人のプレイヤーがプレイできるハングマンゲームを作りたいと思います。プレーヤー1は、他のプレイヤーが推測する必要のある単語を与えます。プレイヤー2は無制限のアテンプを取得しました。私のハングマンゲーム(Java)のuserInputについて

私の問題は、文字の代わりに単語全体を与える必要があるということです。私は、プレイヤー2が手紙の入力で単語を終わらせることができる私のゲームで機能を持っていたいと思います。私はこの動作をさせるためにStringBufferを使用する必要があることを知っていますが、私はこれをどうやって行う必要があるのか​​分かりません。

私はJavaを初めて使い、いつも学びたい!

import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Hangman { 
    public static void main(String[] args) { 

     JFrame frame = new JFrame("Hangman"); 
     frame.setLayout(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setBounds(50,50,800,600); 


     //OBJECTS 
     JButton start = new JButton("START"); 
     start.setBounds(140,80,110,30); 
     frame.getContentPane().add(start); 
     frame.repaint(); 

     JButton guess = new JButton("GUESS"); 
     guess.setBounds(280,80,110,30); 
     frame.getContentPane().add(guess); 
     frame.repaint(); 


     start.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       String tip = JOptionPane.showInputDialog("Give a tip for the user."); 

       JLabel givenTip = new JLabel(tip); 
       givenTip.setBounds(50, 300, 300, 40); 
       frame.getContentPane().add(givenTip); 
       frame.repaint(); 

       JButton buttons = new JButton(); 

       String word = JOptionPane.showInputDialog("What is the word?"); 

       for (int i = 0; i < word.length(); i++) { 

        buttons = new JButton("___"); 
        buttons.setBounds(50 + (i * 80), 350, 60, 40); 

        frame.getContentPane().add(buttons); 
        frame.repaint(); 
       } 

       guess.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       //I think that I need to add this function here. 
       String inputUser = JOptionPane.showInputDialog("Give a letter."); 

       if (word.equals(inputUser)) { 


         JOptionPane.showMessageDialog(null,"This is right!"); 

         JLabel won = new JLabel("You won the game!"); 
         won.setBounds(50, 400, 110, 40); 
         frame.getContentPane().add(won); 


         JLabel restart = new JLabel("Restart the game to play again."); 
         restart.setBounds(50, 500, 250, 40); 
         frame.getContentPane().add(restart); 


         JButton correctInput = new JButton(inputUser); 
         correctInput.setBounds(210, 400, 300, 40); 
         frame.getContentPane().add(correctInput); 
         frame.repaint(); 
        } 
        else 
        { 
         JOptionPane.showMessageDialog(null,"This is not right."); 


        } 



      } 

     }); 





      } 
     }); 

     frame.setVisible(true); 

    } 

} 
+0

ここには多くのハングマン質問があります。それらを検索するだけで、必要なものを見つけることができます。 – Tom

答えて

0

if (word.equals(inputUser)) {入力が完全な文字かどうかを確認するだけです。

char[] charArray = inputUser.toCharArray(); 
if (charArray.length == 1) { 
    //checks if the input is only one character 
    //now you can check if the word contains this letter 
    // and find the buttons with '__' and replace them with the letter 
} 
0

あなたは、まずplayer1の言葉のバージョンを保つが、アンダースコア(_)で構成され、それがplayer2で可視化すること推測しても、より良い時に右の位置に文字を追加する方が簡単ですができます。

あなたはその後、推測を作るためにplayer2のためのボタンを追加し、そのボタンのリスナーメソッドにだけ(メソッドの呼び出しを介して)そのような通常のチェックを行うことができます:

1)は、文字の部分推測されますplayer1の言葉の?そうであれば、player2の単語の正しい位置に追加してください(player1の単語内の位置を見つけてください)

2)はplayer1の単語ですか?次に、彼が勝ったことをプレーヤー2に通知します。

3)敗北を実施することを決定した場合(たとえば、いくつかの推測が行われた後)にも、ここでこれをチェックします。他のゲームロジック機能が必要な場合は、ここに追加することもできます。

関連する問題