Yeah, I am not always very good at asking for help. ;D
I was just toying with this some more and here is some sample code that does not work, that mimics other examples I have seen, but does not allow the GUI to hear the mouselistener (all the time).
I also have a Thread.yield() request at the beginning of each call to recMove() in the AI, so theoretically, a yield request gets put in regularly, but it may be futile here since this is supposed to be happening in the worker thread. I’m not sure.
//This method plays the comp moves.
private void playCompMove(int comp){
final AI tempAI;
if (comp == RED) tempAI = redComp;
else if (comp == YELLOW) tempAI = yellowComp;
else if (comp == GREEN) tempAI = greenComp;
else tempAI = blueComp;
final SwingWorker worker = new SwingWorker() {
public Object construct() {
Move temp = tempAI.getMove(B);
return temp;
}
};
worker.start();
Move compMove = (Move) worker.get();
if (!reset) {
boolean compMoveIsSuccessful = false;
//If a null Move is returned we check to make sure the computer
//really can't make any moves. If a Move can be made, we end
//the game with a game error to let the player know the AI screwed up.
if (compMove == null && (B.getFillMoves(comp) != null
|| B.getJumpMoves(comp) != null)) {
playError(comp);
}
if (compMove != null && B.isLegalFillMove(compMove, comp)) {
clickCompMove(compMove);
B.makeFillMove(compMove, comp);
compMoveIsSuccessful = true;
}
else if (compMove != null && B.isLegalJumpMove(compMove, comp)) {
clickCompMove(compMove);
B.makeJumpMove(compMove, comp);
compMoveIsSuccessful = true;
}
//If something screwed up.
if (compMove != null && compMoveIsSuccessful == false) {
playError(comp + 4);
}
else if (B.isGameOver()) {
gameOver = true;
endOfGame();
}
else {
resetClicks();
paintWorld();
}
}
}
Now supposedly the GUI is working in the event dispatch thread, but the worker thread seems to be getting all of the resources.
Above, I made a reference to “all of the time”. There is something strange going on here because in games where I have 3 AI and a human player (blue), if I let the human player get wiped out by the 3rd move the AI players continue on to finish the game (this is normal). If I click on the reset region, most of the time, the reset click is heard and the game resets after the computer plays its move.
However, with the exact same code, if I play a game with all 4 spots as AI players, the reset will always go unheard. This is the best I have been able to do so far in terms of interupting things. Since this works for 3AI + human games and not 4 AI games, I probably should also present this code:
//This method plays out the comp moves in the proper order.
private void nextCompMove(int comp) {
if (!reset) {
if (comp == RED) playCompMove(RED);
else if (comp == YELLOW && yellowComp != null) playCompMove(YELLOW);
else if (comp == GREEN && greenComp != null) playCompMove(GREEN);
else if (comp == BLUE && blueComp != null) playCompMove(BLUE);
comp++;
if (!gameOver && !reset) {
if (comp > GREEN) {
if (blueComp != null) nextCompMove(BLUE);
//If human player has no legal moves...
else if (B.getFillMoves(BLUE) == null && B.getJumpMoves(BLUE) == null)
nextCompMove(YELLOW);
else {
playersMove = true;
paintWorld();
}
}
else nextCompMove(comp);
}
}
}
This method plays the computer moves in their proper order. As you can see, there is a little bit more work done around the BLUE player, since this player may be human or AI. As things are now, this little bit of extra work seems to be enough to allow the mouselistening to occur when the human player is wiped out and can not make a legal move, as opposed to an AI player.
If you want to see the recMove code I can post it, but it seems to be working fine, it is just processor intensive for 3-4 seconds per move, so if someone gets bored watching an all AI game (like me, after seeing 100’s of them) it would be nicer if the reset button could interupt the game.
Not sure if any of this helps, Malohkan. Thanks again though for responding at all. 