[英]Creating MasterMind in Java using Swing
我是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();
}
});
}
}
目前,当我运行游戏时,我让用户输入一个代码,并将该代码与计算机自己生成的代码进行比较。 但是我的问题是在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;
}
}
首先更改MMCode
的构造函数,这样就无需猜测...
public MMCode(){
setSecret();
}
例如,添加一个名为setGuess
的新方法。
public void setGuess(String[] guessess){
for (int i = 0; i < guess.length; i++) {
this.guess[i] = guessess[i];
}
}
以MMCode MM = new MMCode(guess);
从actionPeformed
方法并使其成为实例变量
public class GUIMasterMind implements ActionListener {
//...
private MMCode mmCode;
//...
public GUIMasterMind() {
//...
mmCode = new MMCode();
//...
然后在actionPerformed
方法中,您只需要更新MMCode
的实例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);
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.