我有一个使用 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");
}
我现在真的很迷茫,所以即使朝正确的方向点头也会很有帮助:)