I am trying to recreate console game with JTextArea as console/output and JTextField as user input. Since the GUI is event driven I do not understand how could I stop the code from executing, to wait for user input before proceeding with opponents turn. The only solution I can think of is While(userTurn) and userTurn would be changed to false upon actionlistener is there a better way?
My console solution
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;
}
I just called this getInput method and I proceeded with opponents turn.
What I want to accomplish is:
I've written an example game so you can observe the difference. The computer and the user try to guess a random number between 0-2 inclusive. Who ever gets it right wins. If both get it right or both get it wrong its a draw.
EDIT: Updated GUI version
Here's the console program:
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();
}
}
Here's the GUI program:
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;
}
}