事件驱动输入与回合制

时间:2016-08-27 13:42:43

标签: java swing jtextfield

我正在尝试使用JTextArea作为控制台/输出和JTextField作为用户输入重新创建控制台游戏。由于GUI是事件驱动的,我不明白如何阻止代码执行,等待用户输入然后继续对手转向。我能想到的唯一解决方案是,当actionlistener有更好的方法时,While(userTurn)和userTurn会被更改为false吗?

我的控制台解决方案

 String getInput(String prompt){
        String inputLine = null;
      console.setTextConsole(prompt + " ");
        try{
            BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
            inputLine = is.readLine();
            if(inputLine.length() == 0) return null;

        }catch(IOException e){
            console.setTextConsole("IOException "+e);
        }
        return inputLine;
    }

我刚刚调用了这个getInput方法,然后我开始跟随对手。

我想要完成的是:

  1. 反对者转
  2. 游戏等待用户
  3. 用户在JtextField中键入文本,然后按Enter
  4. 游戏执行玩家命令
  5. 反对者再次转身..

2 个答案:

答案 0 :(得分:1)

嗯,我想这个:

游戏的统治者是谁拥有转弯。只要统治者行动,另一个必须等​​待。如何实现这个?

  1. 如果用户拥有转弯,他/她可以在JTextField中输入文字。

  2. 当他/她按下ENTER时,必须验证该命令。如果没问题,必须将转弯转移到程序,同时,用户甚至不能在JTextField中输入文本。例如,禁用它:

  3.     private void switchTurnToTheProgram()
        {
            jTextField.setEnabled(false);
        }
    
    1. 当程序完成移动时,必须再次将转弯转移给用户,因此必须启用jTextField:
    2.     private void switchTurnToTheUser()
          {
              jTextField.setEnabled(true);
          }
      
      1. 最后,您必须在每种情况下确定其第一次转弯(使jTextField显示为启用或禁用)。
      2. 完整的算法:

        public void startGame(boolean userOwnsTheFirstTurn)
        {
            if (userOwnsTheFirstTurn)
            {
                switchTurnToTheUser();
            }
            else
            {
                 switchTurnToTheProgram();
                 calculateNextMove();
                 switchTurnToTheUser();
            }
        }
        
        public void userHasEnteredSomeCommand(String command)
        {
            // This must be called from the correspondant actionListener.
            if (validateCommand())
            {
                 switchTurnToTheProgram();
                 calculateNextMove();
                 switchTurnToTheUser();
            }
            else
            {
               ... log an error to the user ...
            }
        }
        

        为了增强用户的体验,启用/禁用按钮以及textField可能会很有用。在这种情况下,您只需修改两种方法switchTurnToTheProgramswitchTurnToTheUser

答案 1 :(得分:1)

我已经编写了一个示例游戏,因此您可以观察到差异。计算机和用户尝试猜测0-2之间的随机数。谁得到了正确的胜利。如果两者都做对了,或者两者都弄错了它。

编辑:更新的GUI版本

这是控制台程序:

import java.util.Random;
import java.util.Scanner;

public class ConsoleGame {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Random rand = new Random();
        boolean playAgain = false;
        int wins = 0, losses = 0, draw = 0;
        do {
            int num = rand.nextInt(3); // 0-2 inclusive
            System.out.println("Guess the number [0-2]: ");
            int guess = Integer.parseInt(console.nextLine());
            int computerGuess = rand.nextInt(3);
            System.out.println("You: " + guess + "\tComputer: " + computerGuess + "\tNumber: " + num);
            if (guess == num && computerGuess == num || guess != num && computerGuess != num) {
                draw++;
                System.out.println("Draw!");
            } else if (guess == num) {
                wins++;
                System.out.println("You win!");
            } else if (computerGuess == num) {
                losses++;
                System.out.println("Computer wins :(");
            }
            System.out.println("Play again [y/n]? ");
            playAgain = console.nextLine().startsWith("y");
        } while (playAgain);
        System.out.println("Wins: " + wins + "\nLosses: " + losses + "\nDraw: " + draw);
        console.close();
    }
}

这是GUI程序:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GUIGame extends JFrame {
    private JPanel contentPane;
    private JTextField textField;
    private JTextArea textArea;
    private boolean textReceived;

     /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    GUIGame frame = new GUIGame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GUIGame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        setContentPane(contentPane);

        textField = new JTextField();
        textField.addActionListener(new ActionListener() {
            @Override
            // user pressed 'enter' key,
            public void actionPerformed(ActionEvent e) {
                textReceived = true;
                synchronized (textField) {
                    // notify game loop thread which is waiting on this event
                    textField.notifyAll();
                }
            }
        });
        contentPane.add(textField, BorderLayout.SOUTH);

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        textArea = new JTextArea();
        textArea.setFont(new Font("Consolas", Font.PLAIN, 12));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setForeground(Color.LIGHT_GRAY);
        textArea.setBackground(Color.BLACK);
        textArea.setEditable(false);
        scrollPane.setViewportView(textArea);

        // Start game loop in new thread since we block the thread when
        // waiting for input and we don't want to block the UI thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                playGame();
            }
        }).start();
    }

    private void playGame() {
        Random rand = new Random();
        boolean playAgain = false;
        int wins = 0, losses = 0, draw = 0;
        do {
            int num = rand.nextInt(3); // 0-2 inclusive
            textArea.append("Guess the number [0-2]: \n");
            int guess = Integer.parseInt(requestInput());
            int computerGuess = rand.nextInt(3);
            textArea.append("You: " + guess + "\tComputer: " + computerGuess + "\tNumber: " + num + "\n");
            if (guess == num && computerGuess == num || guess != num && computerGuess != num) {
                draw++;
                textArea.append("Draw!\n");
            } else if (guess == num) {
                wins++;
                textArea.append("You win!\n");
            } else if (computerGuess == num) {
                losses++;
                textArea.append("Computer wins :(\n");
            }
            textArea.append("Play again [y/n]? \n");
            playAgain = requestInput().startsWith("y");
        } while (playAgain);
        textArea.append("Wins: " + wins + "\nLosses: " + losses + "\nDraw: " + draw + "\n");
    }

    private String requestInput() {
        textField.setEnabled(true);
        textField.requestFocus();
        // wait on text field till UI thread signals a user input event
        synchronized (textField) {
            while (!textReceived) {
                try {
                    textField.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        String input = textField.getText();
        textField.setText("");
        textField.setEnabled(false);
        textReceived = false;
        return input;
    }
}