2014-06-17 49 views
-1

我是Java的初学者,正在为项目创建游戏。我正在使用Swing创建一个MasterMind版本。使用Swing在Java中创建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(); 
     } 
    }); 
} 
} 

目前,当我的游戏运行我有用户输入一个代码和代码是相对于计算机本身生成的代码。然而,我的问题是在actionPerformed()方法中。我希望这段代码能够保持重新运行而不必创建全新的密码。我尝试过各种循环,但没有成功。任何帮助,将不胜感激。

此外,您可能会忽略白色/黑色挂钩代码,稍后我会解决。我只想让我的程序首先正确运行。这是我的另一堂课。这个类的目的是比较两个数组,猜测和秘密。

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; 
} 

} 
+1

我没有看到你的代码中的任何“密码” - 你在说什么'setSecret'?如果'MMCode'代表一组猜测,并且你不希望他们每次输入猜测都必须输入一个“秘密”,那么你不希望秘密的东西进入MMCode。将其移至不同的课程。有一个类处理两个完全不相关的概念(“猜测”和“密码”),无论如何都是糟糕的设计,而你遇到的问题就是它为什么很差的一个很好的例子。 – ajb

回答

2

开始通过改变MMCode的构造函数,因此它并不需要猜测...

public MMCode(){ 
    setSecret(); 
} 

添加一个名为setGuess新的方法,例如...

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

actionPeformed方法取MMCode MM = new MMCode(guess);并使其成为实例变量

public class GUIMasterMind implements ActionListener { 
    //... 
    private MMCode mmCode; 
    //... 
    public GUIMasterMind() { 
     //... 
     mmCode = new MMCode(); 
     //... 

然后在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); 
     //... 
+0

嘿感谢您的回复!我已经解决了我的问题,并从您的想法中获得了一些帮助!我错过了我的程序中的一个组件...我希望有一个计数器,每次猜测都会减少。这意味着玩家只能猜测他/她可以做出的一定数量。我目前的新手尝试如下所示: – user3750380

+0

因此,在您的'actionPerformed'中,您需要增加某种“计数器”值(然后您可以使用它来更新屏幕上的JLabel)。如果“计数器”超过了允许的猜测次数,游戏结束... – MadProgrammer