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:
public void 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