如何将我的游戏从系统实现到GUI? Java代码

时间:2018-04-11 14:43:33

标签: java user-interface console

嗨,StackOverflow的人,我对Java很新,需要一些帮助, 我制作了一个程序来编译我想要的程序,一个猜谜游戏,计算机随机生成一个数字,并要求用户猜测它,但这完全在控制台中。我希望为游戏创建一个GUI,我已经在代码中制作了GUI的大纲,但我很难将逻辑放入其中,我最好从控制台程序中调用方法(并进行一些更改)或启动从GUI代码中的顶部开始并将逻辑放在那里?如果是后者,我把逻辑放在哪里?任何帮助将不胜感激!

到目前为止,这是我的两个程序,两个程序都已编译,但GUI现在没有任何意义!

提前谢谢

此代码是控制台代码,扫描用户的猜测并与随机数进行比较。

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class GuessingGameSystem {
    public static int randomizer() {
        Random rand = new Random();
        int num = rand.nextInt(10)+1;
        System.out.println(num);
        return num;
    }
    public static int userInput() {

        System.out.println("I've thought of a number between 1 and 10");
        System.out.println("Enter your guess...");
        Scanner scan = new Scanner(System.in);
        int guess = scan.nextInt();
        return guess;
    }
    public static String compare(int a, int b) {

        String result = null;
        if (a < b) {
            result = "Higher!";
        }
        else if (a > b)  {
            result = "Lower!";
        }
        else {
            result = "You guessed it - I was thinking of " + b;
        }
        return result;
    }
    public static void main(String[] args) {
        Scanner scanLine = new Scanner(System.in);

        String playAgain = "";
        int count = 0;
        int a;
        int b;
        do  {
            b= randomizer();
            count = 0;
            ArrayList<Integer> guessesSoFar = new ArrayList<>();

            do {
                a = userInput();
                count++;
                guessesSoFar.add(a);
                System.out.println("Guesses so far: " + Arrays.toString(guessesSoFar.toArray()));
                System.out.println("Number of guesses so far: " + guessesSoFar.size());
                System.out.println(compare(a,b));       
            } while (a != b);
            System.out.println("It took you " + count + " guesses.");
            System.out.println("Play again? Yes/No");
            playAgain = scanLine.nextLine();
        } while (playAgain.equalsIgnoreCase("yes") || playAgain.equalsIgnoreCase("y"));
    }
}

这是我到目前为止的GUI代码:

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

public class GuessingGameGUI {
    private JFrame frame;
    private JPanel panel;
    private JLabel lblInstructions, lblResult, lblGuesses, lblNoOfGuesses;
    private JButton btnCheck, btnNewGame, btnExit;
    private JTextField txtUserGuess, txtListOfGuesses, txtGuessesCount;

    public static void main(String[] args) {
        new GuessingGameGUI();

    }
    public GuessingGameGUI() {
        createForm();
        createFields();
        createButtons();
        createTextField();

        frame.add(panel);
        frame.setVisible(true);
    }
    public void createForm() {
        frame = new JFrame();
        frame.setTitle("Guessing Game");
        frame.setSize(800,350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(null);
    }
    public void createFields() {
        lblInstructions = new JLabel("Guess the number between 1 and 10:");
        lblInstructions.setBounds(285, 50, 350, 20);
        panel.add(lblInstructions);

        lblResult = new JLabel("Result");
        lblResult.setBounds(380, 200, 100, 20);
        panel.add(lblResult);

        lblGuesses = new JLabel("Your Guesses:");
        lblGuesses.setBounds(70, 50, 100, 20);
        panel.add(lblGuesses);

        lblNoOfGuesses = new JLabel("Number of Guesses:");
        lblNoOfGuesses.setBounds(600, 50, 200, 20);
        panel.add(lblNoOfGuesses);
    }

    public void createButtons() {
        btnCheck = new JButton("Check");
        btnCheck.setBounds(325, 150, 150, 20);
        panel.add(btnCheck);

        btnNewGame = new JButton("New Game");
        btnNewGame.setBounds(160, 300, 150, 20);
        panel.add(btnNewGame);

        btnExit = new JButton("Exit");
        btnExit.setBounds(495, 300, 150, 20);
        panel.add(btnExit);

        btnCheck.addActionListener(new CheckHandler());
        btnNewGame.addActionListener(new NewGameHandler());
        btnExit.addActionListener(new ExitHandler());
    }
    public void createTextField() {
        txtUserGuess = new JTextField();
        txtUserGuess.setBounds(300, 100, 200, 20);
        panel.add(txtUserGuess);

        txtListOfGuesses = new JTextField("List of Guesses");
        txtListOfGuesses.setBounds(65, 75, 115, 200);
        panel.add(txtListOfGuesses);

        txtGuessesCount = new JTextField("Guesses Count");
        txtGuessesCount.setBounds(610,75, 110, 20);
        panel.add(txtGuessesCount);
    }
    class CheckHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            //to check random number against user guess in txtUserGuess
        }
    }
    class NewGameHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            //to start a new game
         }
    }
    class ExitHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你应该看看MVP(模型视图演示者)。

GUI代码不应该具有游戏的逻辑 - 你不应该调用定义游戏行为的方法,只能向前传递用户命令。

以下是使用MVP实现它的可能方式的概要(A> B表示A调用B&#39方法; GUI是视图):

  • 用户写一个数字
  • 用户按下&#34;检查&#34;
    • 查看&gt;演示者:onCheckCommand(int number)
      • 演示者&gt;型号:receiveGuess(int number)
        • 模型将收到的号码添加到猜测列表
        • 模型返回HIGHERLOWERRIGHT
      • 演示者&gt;型号:getListOfGuesses()getGuessesCount()
      • 演示者&gt;查看:showListOfGuesses(List<Integer> list)showGuessesCount(int count)showResult(Result result)HIGHERLOWERRIGHT
        • 查看:如果结果为RIGHT,请停用&#34;检查&#34;按钮
  • 用户按下&#34;新游戏&#34;
    • View启用&#34;检查&#34;按钮并清除结果,猜测列表和猜测计数
    • 查看&gt;演示者:onNewGameCommand()
      • 演示者&gt;型号:startNewGame()
        • 模型清除猜测列表并生成新号码

基本上,演示者是谁知道应该调用哪些游戏方法(以及何时)以及应该显示什么(以及何时)。 模型应该有一个公共方法,用于响应用户输入或其他外部事件(在任何情况下)发生的事情(在游戏中) - 例如,每秒触发一次更新剩余时间的计时器。

我假设视图强制用户输入整数。您应该小心调用更新GUI on the event dispatching thread的方法。在这个例子中已经发生了这种情况,因为在按下&#34;按钮时会调用所有方法。事件。从处理程序调用所有内容并不总是有效。