Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Swing在Java中创建MasterMind_Java_Swing_User Interface - Fatal编程技术网

使用Swing在Java中创建MasterMind

使用Swing在Java中创建MasterMind,java,swing,user-interface,Java,Swing,User Interface,我是Java初学者,正在为一个项目创建一个游戏。我正在使用Swing创建一个版本的MasterMind 到目前为止,我得到的是: package GUI; import java.awt.GridLayout; import java.awt.event.*; import javax.swing.*; public class GUIMasterMind implements ActionListener { JFrame frame; JPanel contentPane; JLabe

我是Java初学者,正在为一个项目创建一个游戏。我正在使用Swing创建一个版本的MasterMind

到目前为止,我得到的是:

package GUI;

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;


public class GUIMasterMind implements ActionListener {
JFrame frame;
JPanel contentPane;
JLabel label, prompt, show, blackPegs, whitePegs;
JButton step, newGame;  
JTextField guessBox;
private String[] args;
int guess;

public GUIMasterMind() {

    //frame created
    frame = new JFrame("MasterMind:");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // content pane
    contentPane = new JPanel();      
    contentPane.setLayout(new GridLayout(0, 2, 10, 5));
    contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));


    prompt = new JLabel("Enter a guess: ");
    contentPane.add(prompt);

    guessBox = new JTextField();
    guessBox.addActionListener(this);
    contentPane.add(guessBox);

    newGame = new JButton("New Game");
    newGame.setActionCommand("New Game");
    newGame.addActionListener(this);
    contentPane.add(newGame);

    step = new JButton("Compare");
    step.setActionCommand("Compare");
    step.addActionListener(this);
    contentPane.add(step);

    show = new JLabel("Results");
    contentPane.add(show);

    blackPegs = new JLabel("Black Pegs: ");
    contentPane.add(blackPegs);

    whitePegs = new JLabel("White Pegs: ");
    contentPane.add(whitePegs);

    frame.setContentPane(contentPane);

    frame.pack();
    frame.setVisible(true);


}

public void actionPerformed(ActionEvent event){
    String eventName = event.getActionCommand();

    String g1 = guessBox.getText();

    if(eventName.equals("New Game")){
        GUIMasterMind.main(args);
    } 
    else if(eventName.equals("Compare")){


        String[] guess = new String[4];

        for (int i = 0; i < guess.length; i++) {
            guess[i] = g1.substring(i, i+1);
            System.out.println(guess[i]);
        }

        MMCode MM = new MMCode(guess);
        show.setText("This was your guess: "+ MM.toHTML());

        if(MM.compareTo() == 1){
            System.out.println("If you would like to start a new game press the button!");
        } 
        else if(MM.compareTo() == 0){
            prompt.setText("Guesses remaining: ");
        } 
        else {
            prompt.setText("Error");
        }
        guessBox.setText("");
        System.out.println("done");
        }
}


public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            GUIMasterMind game = new GUIMasterMind();
        }
    });
}
}
packagegui;
导入java.awt.GridLayout;
导入java.awt.event.*;
导入javax.swing.*;
公共类GUIMasterMind实现ActionListener{
JFrame框架;
JPanel内容窗格;
JLabel标签、提示、显示、黑钉、白钉;
JButton步,新游戏;
JTextField猜测框;
私有字符串[]args;
智力猜测;
公众策划人(){
//创建的框架
框架=新的JFrame(“策划人:”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//内容窗格
contentPane=newjpanel();
setLayout(新的GridLayout(0,2,10,5));
contentPane.setboorder(BorderFactory.createEmptyBorder(10,10,10,10));
prompt=newjlabel(“输入猜测:”);
contentPane.add(提示);
猜测框=新的JTextField();
addActionListener(this);
contentPane.add(猜测框);
newGame=newjbutton(“新游戏”);
newGame.setActionCommand(“新游戏”);
newGame.addActionListener(这个);
contentPane.add(新游戏);
步骤=新按钮(“比较”);
步骤.setActionCommand(“比较”);
步骤.addActionListener(此);
contentPane.add(步骤);
显示=新的JLabel(“结果”);
contentPane.add(show);
blackPegs=新的JLabel(“blackPegs:”);
contentPane.add(blackPegs);
whitePegs=新的JLabel(“whitePegs:”);
contentPane.add(whitePegs);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
已执行的公共无效操作(操作事件){
字符串eventName=event.getActionCommand();
字符串g1=guessBox.getText();
if(eventName.equals(“新游戏”)){
GUIMasterMind.main(args);
} 
else if(eventName.equals(“比较”)){
字符串[]猜测=新字符串[4];
for(int i=0;i
目前,当我的游戏运行时,我让用户输入一个代码,并将该代码与计算机自己生成的代码进行比较。然而,我的问题在于actionPerformed()方法。我希望这段代码能够继续运行,而不必创建一个全新的密码。我尝试过各种循环,但都没有成功。任何帮助都将不胜感激

另外,您可以忽略白色/黑色peg代码,稍后我将解决该问题。我只想让我的程序先正常运行。这是我的另一节课。这个类的目的是比较两个数组,猜测和秘密

package GUI;


public class MMCode {
String[] guess = new String[4];
String[] secret = new String[4];
int num;

public MMCode(String[] guessess){
    for (int i = 0; i < guess.length; i++) {
        this.guess[i] = guessess[i];
    }

    setSecret();

}

public void setSecret(){
    for (int i = 0; i < secret.length; i++) {
        num = ((int)(Math.random()*6+1));
        secret[i] = Integer.toString(num);
    }
}

public String getSecret(){
    return ("The secret code is: "+secret[0]+secret[1]+secret[2]+secret[3]);
}

public void setGuess(){

}

public String getGuess(){
    return ("Your guess is:      "+guess);
}

public int compareTo(){

    if(guess[0].equals(secret[0]) && guess[1].equals(secret[1]) && guess[2].equals(secret[2]) && guess[3].equals(secret[3])){
        System.out.println("Congratulations you've guessed the computer's code!");
        return 1;
    } else if (!(guess[0].equals(secret[0]) && guess[1].equals(secret[1]) && guess[2].equals(secret[2]) && guess[3].equals(secret[3]))){
        return 0;
    }
    return -1;
}

public String toHTML(){
    String html = "";


    html += guess[0].toString();
    html += guess[1].toString();
    html += guess[2].toString();
    html += guess[3].toString();
    return html;
}

 }
packagegui;
公共类MMCode{
字符串[]猜测=新字符串[4];
String[]secret=新字符串[4];
int-num;
公共MMCode(字符串[]猜测){
for(int i=0;i
首先更改
MMCode
的构造函数,这样就不需要猜测

public MMCode(){
    setSecret();
}
添加一个名为
setGuess
的新方法,例如

public void setGuess(String[] guessess){
    for (int i = 0; i < guess.length; i++) {
        this.guess[i] = guessess[i];
    }
}
然后在
actionPerformed
方法中,只需更新
MMCode
的实例即可

public void actionPerformed(ActionEvent event){
    //...
    else if(eventName.equals("Compare")){
        String[] guess = new String[4];

        for (int i = 0; i < guess.length; i++) {
            guess[i] = g1.substring(i, i+1);
            System.out.println(guess[i]);
        }

        mmCode.setGuess(guess);
        //...
public void actionPerformed(ActionEvent事件){
//...
else if(eventName.equals(“比较”)){
字符串[]猜测=新字符串[4];
for(int i=0;i
我在你的代码中没有看到任何“密码”——你是说
setSecret
?如果
MMCode
代表一组猜测,你不希望他们必须输入“密码”每次他们输入猜测,那么你就不希望秘密内容出现在
MMCode
中。将其移动到另一个类。使用一个处理两个完全无关的概念(“猜测”和“密码”)的类不管怎么说,它的设计很差,而你遇到的问题就是一个很好的例子,说明了为什么它很差。嘿,谢谢你的回答!我在你的想法的帮助下解决了我的问题!但是我的程序中缺少了一个组件…我希望有一个计数器,根据每次的猜测减少。这意味着玩家只需要有一定数量的猜测他/她c
public void actionPerformed(ActionEvent event){
    //...
    else if(eventName.equals("Compare")){
        String[] guess = new String[4];

        for (int i = 0; i < guess.length; i++) {
            guess[i] = g1.substring(i, i+1);
            System.out.println(guess[i]);
        }

        mmCode.setGuess(guess);
        //...