0

This is a follow up to a previous question I had. I have a Battleships game with two boards. When the user clicks on the computer board an action occurs, along these lines:

public void mouseClicked(MouseEvent e)
// Get coordinates of mouse click

if (//Set contains cell) {
    /add Cell to set of attacked cells

//Determine if set contains attacked cell.
// If yes, hit, if no, miss.
checkForWinner();

The checkForWinner method determines if the game has been won yet. If it hasn't it calls a nextTurn method which changes the current turn. If the currentTurn is set to Computer, a ComputerMove() method is automatically called.
When that method finishes, it again checksforWinner, changes turn and waits for the user to click on the grid to start the cycle again.

Ideally, I'd like to have sound effects, or at the very least a pause between moves. However, no matter how I use Thread.sleep, or TimerTask, or anything else, I can't get it to function correctly.

If I use a simple Thread.sleep(500) in the CheckforWinner method, or in the ComputerMove method, all that happens is the human's go is delayed for the set amount of time. As soon as his move is executed the computer's move is completed immediately.

I know very little about threads but I assume this is because all the initiation of the bouncing back and forth between methods begins with a method in the mouse listener.

Given the set up of my system, is there a way to implement a delay without radically changing things?

Edit: May as well include the classes:

public void checkForWinner() {
    if (human.isDefeated())
        JOptionPane.showMessageDialog(null, computer.getName() + " wins!");
    else if (computer.isDefeated())
        JOptionPane.showMessageDialog(null, human.getName() + " wins!");
    else
        nextTurn();
}

public void nextTurn() {
    if (currentTurn == computer) {
        currentTurn = human;
    } else {
        currentTurn = computer;
        computerMove();
    }
}

public void computerMove() {

    if (UI.currentDifficulty == battleships.UI.difficulty.EASY)
        computerEasyMove();
    else
        computerHardMove();
}

public void computerEasyMove() {

    // Bunch of code to pick a square and determine if its a hit or not.
    checkForWinner();
}
5
  • Have you tried waiting only at the start of ComputerMove? May 6, 2013 at 13:33
  • Yeah - but it just pauses the human move. It doesn't remotely delay the computer move. May 6, 2013 at 13:34
  • Then it seems like you need to debug your program to figure out why ComputerMove is called before the "human move" has ended, right? May 6, 2013 at 13:36
  • I thought it was happening because the human move technically "ends" with the end of the mouseClick method. But before the mouseClick method has ended the computerMove method is called, which means they are cyclically tied. I could be totally wrong - that's just what I thought. I've added a little more code. May 6, 2013 at 13:37
  • 1
    For better help sooner, post an SSCCE. May 6, 2013 at 13:43

1 Answer 1

1

Ideally, I'd like to have sound effects, or at the very least a pause between moves. However, no matter how I use Thread.sleep, or TimerTask, or anything else, I can't get it to function correctly.

You should be using a Swing Timer. Something like:

Timer timer = new Timer(1000, new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        currentTurn = computer;
        computerMove();
    }
});
timer.setRepeats(false);
timer.start();
1
  • Upvoted; @Andrew, you should set the timer when you handle the mouse click; play the sound at this same time. When the handler executes, the machine will move. This will give you a 1s delay between human move+sound, and machine move.
    – tucuxi
    May 6, 2013 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.