Hello All!
I am making a simple four-in-a-row style game where 2 players can play against eachother. This is part of an AI course I'm taking where we should demonstrate the machine (the AI) playing against the player.
The game part of the game and the gui are completely separated. I want to keep things this way prefarably. The game can be player vs player, player vs AI, AI vs AI (to test different AI models) and so on.
When the game is started, I create a jFrame window asking for some player info (name, AI type, player vs player or player vs AI etc). When the user clicks on 'Play' the jFrame creates a "GameType" object containing all the relevant info nescesary to initialize the rest of the game.
How do I make the jFrame wait for the 'play' button and then send the info to the rest of the game?
See the code for a better idea
The Main class:
public class Main {
public static void main(String[] args) {
Game game = new Game();
game.run();
}
}
The Game class:
public class Game {
GameType GT;
public void run(){
if (init()){
gameLoop();
}
}
private void gameLoop(){
while (true){
//the main game loop
}
}
private boolean init(){
PlayerInfo playerInfo = new PlayerInfo();
playerInfo.display();
GT = playerInfo.getGameType();
System.out.println(GT.getPlayer1());
//and eventually if everything's fine,
return true;
}
}
And finally the GUI PlayerInfo class:
public class PlayerInfo extends javax.swing.JFrame {
private GameType GT;
private boolean OKPressed = false;
public PlayerInfo() {
initComponents();
}
/*the event listener function for the 'play' button*/
private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {
GT = new GameType(/*all relevant info will be inserted here*/);
OKPressed = true;
System.out.println("OK Pressed");
//this.dispose();
}
public void display() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PlayerInfo().setVisible(true);
}
});
}
void waitForOKButton(){
System.out.println("waiting...");
while (OKPressed == false){}
System.out.println("done waiting");
}
public GameType getGameType(){
waitForOKButton();
System.out.println("senting GT!");
return GT;
}
}
I am using netbeans and a lot of the code for the PlayerInfo class was left out. The actionPerformed() method is the one that's called when the button is pressed.
As you can see I tried spinning on a boolean variable that turns true when the button is pressed. For some reason it keeps on spinning and the waitForOK() never returns?
What am I doing wrong? Or is my train of thought completely wrong?