JOptionPane を使用してユーザーの推測を取得するハングマン エンジンがありますが、テキスト フィールドを使用して GUI を実装しているため、エンジンを停止してテキスト フィールドからユーザーの推測を取得する方法が必要です。 MVCを使用して、テキストフィールドからのデータがメイン/コントローラークラスに送信される瞬間に、データがメインクラスの変数にロードされた後-そのデータをエンジンに統合する際に問題が発生し始めます
エンジンに待機を追加することが最善かどうかはわかりませんが、それが私が本当に考えることができるすべてです。
これが私のエンジンクラスです:
public class engine {
char [] charda = new char[20];
char [] charwo = new char[20];
Highscore words = new Highscore();
main mn = new main();
int guesses =7;
char guess;
public engine() {
}
public void enginer(){
int count = 0;
String word = words.getWord();
for (int i = 0; i<word.length(); i++)
{
//instantiates two arrays one of dahses and one with the word
charwo[count] = word.charAt(i);
charda[count]= '_';
count++;
}
for (int l=0; l<count; l++)
{
System.out.print(" "+charda[l]);
}
while (guesses != 0 && !Arrays.equals(charwo, charda))
{
System.out.println("");
guess = JOptionPane.showInputDialog("enter a Guess").charAt(0);
if (word.toUpperCase().contains(String.valueOf(guess).toUpperCase()))
{
for (int k = 0; k<word.length(); k++)
{
if (String.valueOf(guess)
.toUpperCase()
.equals( String.valueOf(charwo[k]).toUpperCase() ))
{
charda[k]=charwo[k];
for(int l=0; l<count; l++)
{ // prints dashes here to avoid a letter being
// chopped off by the program stopping in the middle
System.out.print(" "+charda[l]);
}
}
}
}
else
{
guesses = guesses-1;
System.out.println("guesses left "+guesses);
//
//Re-displays dashes
for (int l=0; l<count; l++)
{
System.out.print(" "+charda[l]);
}
}
if (Arrays.equals(charwo, charda))
{
System.out.println("");
System.out.println("You are Winner");
}
}
}
}
これは、ボタンのクリックを処理するメイン クラスの一部です。
public void buttonClicked ()
{
gameplay gp = new gameplay();
engine en = new engine();
en.guess = gp.getInput; // this is where I try send the input from the text field to the engine
en.enginer();
System.out.println("button clicked");
}
私は現時点で本当に迷っているので、正しい方向へのうなずきさえも本当に役に立ちます:)