User Input Q

For fun I am writing an unbeatable TicTacToe game. So far here are the classes and their functions:

model

Ai.java - obviously the AI, builds a state tree and makes decisions on moves

Board.java - The board. Holds a number of useful methods needed to play

Game.java - The game loop lives here. Holds two players and an AI object

Player.java - Holds stuff about the player

ui

BoardRenderer - Given a board, it draws the correct images in the correct places. Extends Jpanel so I can place it on the MainUi

MainUi - where all the graphical components live. Extends JFrame

So here's my question. As explained above my game loop lives in Game.java. After some preliminary setup (like allowing the player to choose X's or O's ) I will create an instance of a game object and call the play() method. That will initiate the game loop and start everything running.

The problem I am having is that I cant figure out how to get user input from the BoardRenderer to the game. I can't figure out how to tell the game loop to wait until we get a click from the BoardRenderer, and then handling the click by figuring out where on the board the player wishes to move and marking the spot.

I haven't written it yet, but I plan to have an EventListener class that will handle all the click events from the BoardRenderer, and then it's a matter of figuring out how to make the game loop wait until my event handler gets the click, and then have the loop call Player.move(x, y, symbol) based on the info from the handler.

Here is the game loop code:

publicvoid play(){

boolean gameover =false;

//make sure we have a fresh board.

if(board.getMovesLeft() != Board.MAX_MOVES)

resetBoard();

while(!gameover){

if(turns%2 != 0){//its player ones turn

if(player1.getPlayerType() == Player.PlayerType.HUMAN){

//Human turn, wait for input from the UI

}else{

//Let the AI do it's thing

}

}else{//its player twos turn

if(player2.getPlayerType() == Player.PlayerType.HUMAN){

//Human turn, wait for input from the UI

}else{

//Let the AI do it's thing

}

}

//after turn cleanup

if(board.getBoardState() != Board.BoardState.UNKNOWN){

gameover =true;//game is over!

//Who won?

if(board.getBoardState() == Board.BoardState.WIN){

if(turns%2 != 0){

player1.incrementWins();

player2.incrementLosses();

}else{

player1.incrementLosses();

player2.incrementWins();

}

}

//Was it a tie?

if(board.getBoardState() == Board.BoardState.TIE){

player1.incrementTies();

player2.incrementTies();

}

}else{//No one has won yet, keep going

turns++;//increment turns.

}

}

}

Ideas?

Message was edited by:

jduv

[4690 byte] By [jduva] at [2007-10-2 21:15:11]
# 1
Game loop? Bad idea. You don't need to write a loop, the Swing GUI already runs a loop listening for events like button presses and mouse clicks. You should write your program to respond to those events.
DrClapa at 2007-7-14 0:22:47 > top of Java-index,Java Essentials,Java Programming...
# 2

What if the game is AI vs AI? There are no clicks or events from the GUI. Writing a loop like this is very common when writing games. However, what you suggest may be my only option.

One of the points of this project was to test my design skills. I could have easily already hacked the thing together without any consideration towards good OO design.

I would like to make it work with my current design, but if I cant figure it out then I will try and figure out how to restructure it around the EventHandler as you suggest. Although, that isnt exactly a good design either.

jduva at 2007-7-14 0:22:47 > top of Java-index,Java Essentials,Java Programming...