我有一个hangman引擎,它使用JOptionPane来获取用户的猜测,但现在我正在实现一个带有文本字段的GUI,我需要一种方法来停止引擎并从文本字段中获取用户猜测 - 我试图使用MVC,因此目前来自文本字段的数据被发送到主/控制器类,但是在数据加载到主类中的变量之后 - 我开始无法将这些数据整合到引擎中可以一个while循环等待事件继续java
我不知道给引擎添加一个等待是否是最好的事情,但这是我真正想到的。
这里是我的引擎类:
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");
}
我真的失去了此刻所以即使是在点头正确的方向将是真正有帮助:)
请参阅观察者和听众设计模式。这是你需要的。 –