开发者

java timer for game

开发者 https://www.devze.com 2022-12-22 20:47 出处:网络
I have created a game in java and now I just need to add a time开发者_如何学JAVAr that allow the user to play under 60s. I have searched on internet and found the timer for swing and util packages. co

I have created a game in java and now I just need to add a time开发者_如何学JAVAr that allow the user to play under 60s. I have searched on internet and found the timer for swing and util packages. could you please just give me a method to be able to use it in my game???


if you want something interactive you can use TimerTask and Timer classes:

class ExpireTask extends TimerTask
{
  YourClass callbackClass;

  ExpireTask(YourClass callbackClass)
  {
    this.callbackClass = callbackClass;
  }

  public void run()
  {
    callbackClass.timeExpired()
  }
}

So now you've got a timer that fires calling timeExpired of another class. Now with a Timer you can schedule it:

...
Timer timer = new Timer();
timer.schedule(new ExpireTask(callbackClass), 60000 /* 60 secs */);
...


System.currentMiliSeconds(); Save it at the beggining of the game. And then compare it: if(cm<(System.currentMiliSeconds()/1000-60)){System.exit(0);}


The thing is that when you schedule a task,that task runs on a different thread, how could you possibly use that to change the player turn which is running on another thread, without making variable checking along the whole execution to know whenever the callback function is called.

I add some sample code as well.

public class CustomTimerTask extends TimerTask {

private ServerGameManager gameManager;

public CustomTimerTask(ServerGameManager gameManager){
    this.gameManager= gameManager;

}
@Override
public void run() {
    gameManager.changeTurn();
}}

This is my custom Timer class that will call the gameManager method, but this method does not stop the loop of the game to give the turn to another player.

private void playGame() {
        while (true) {

            try {
                int turnDelay = 1000;
                int turnSeconds = 30;
                java.util.Timer timer = new java.util.Timer();
                timer.scheduleAtFixedRate(new CustomTimerTask(this), 0, turnDelay);

                try {
                    // Send player turn color to clients
                    //ToDo change method to return to start whenever the update method is called
                    toPlayerOne.writeObject(turn);
                    toPlayerTwo.writeObject(turn);

                    // Get turn from client.
                    if (playerOne.getColor() == turn) {
                        move = (Move) fromPlayerOne.readObject();
                        move.setStart(9 - move.getStart().x, 9 - move.getStart().y);
                        move.setEnd(9 - move.getEnd().x, 9 - move.getEnd().y);
                    } else {
                        move = (Move) fromPlayerTwo.readObject();
                    }

                    Move moveToPlayerOne = new Move(), moveToPlayerTwo = new Move();

                    // Register move on the board.
                    // If there is no piece at the end (normal move, no attack)
                    if (board.getSquare(move.getEnd().x, move.getEnd().y).getPiece() == null) {
                        Piece piece = board.getSquare(move.getStart().x, move.getStart().y).getPiece();

                        board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);
                        board.getSquare(move.getEnd().x, move.getEnd().y).setPiece(piece);

                        // Rotate the move 180 degrees before sending.
                        moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                        moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                        moveToPlayerOne.setMoveColor(move.getMoveColor());
                        moveToPlayerOne.setStartPiece(null);
                        moveToPlayerOne.setEndPiece(piece);

                        moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                        moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                        moveToPlayerTwo.setMoveColor(move.getMoveColor());
                        moveToPlayerTwo.setStartPiece(null);
                        moveToPlayerTwo.setEndPiece(piece);
                    } else {
                        Piece attackingPiece = board.getSquare(move.getStart().x, move.getStart().y).getPiece();
                        Piece defendingPiece = board.getSquare(move.getEnd().x, move.getEnd().y).getPiece();

                        BattleOutcome outcome = attackingPiece.getPieceType().attack(defendingPiece.getPieceType());

                        moveToPlayerOne.setAttackMove(true);
                        moveToPlayerTwo.setAttackMove(true);

                        if (outcome == BattleOutcome.WIN) {
                            board.getSquare(move.getEnd().x, move.getEnd().y).setPiece(board.getSquare(move.getStart().x, move.getStart().y).getPiece());
                            board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);

                            // Rotate the move 180 degrees before sending.
                            moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                            moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                            moveToPlayerOne.setMoveColor(move.getMoveColor());
                            moveToPlayerOne.setStartPiece(null);
                            moveToPlayerOne.setEndPiece(attackingPiece);
                            moveToPlayerOne.setAttackWin(true);
                            moveToPlayerOne.setDefendWin(false);

                            moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                            moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                            moveToPlayerTwo.setMoveColor(move.getMoveColor());
                            moveToPlayerTwo.setStartPiece(null);
                            moveToPlayerTwo.setEndPiece(attackingPiece);
                            moveToPlayerTwo.setAttackWin(true);
                            moveToPlayerTwo.setDefendWin(false);
                        } else if (outcome == BattleOutcome.LOSE) {
                            board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);

                            // Rotate the move 180 degrees before sending.
                            moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                            moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                            moveToPlayerOne.setMoveColor(move.getMoveColor());
                            moveToPlayerOne.setStartPiece(null);
                            moveToPlayerOne.setEndPiece(defendingPiece);
                            moveToPlayerOne.setAttackWin(false);
                            moveToPlayerOne.setDefendWin(true);

                            moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                            moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                            moveToPlayerTwo.setMoveColor(move.getMoveColor());
                            moveToPlayerTwo.setStartPiece(null);
                            moveToPlayerTwo.setEndPiece(defendingPiece);
                            moveToPlayerTwo.setAttackWin(false);
                            moveToPlayerTwo.setDefendWin(true);
                        } else if (outcome == BattleOutcome.DRAW) {
                            board.getSquare(move.getStart().x, move.getStart().y).setPiece(null);
                            board.getSquare(move.getEnd().x, move.getEnd().y).setPiece(null);

                            // Rotate the move 180 degrees before sending.
                            moveToPlayerOne.setStart(new Point(9 - move.getStart().x, 9 - move.getStart().y));
                            moveToPlayerOne.setEnd(new Point(9 - move.getEnd().x, 9 - move.getEnd().y));
                            moveToPlayerOne.setMoveColor(move.getMoveColor());
                            moveToPlayerOne.setStartPiece(null);
                            moveToPlayerOne.setEndPiece(null);
                            moveToPlayerOne.setAttackWin(false);
                            moveToPlayerOne.setDefendWin(false);

                            moveToPlayerTwo.setStart(new Point(move.getStart().x, move.getStart().y));
                            moveToPlayerTwo.setEnd(new Point(move.getEnd().x, move.getEnd().y));
                            moveToPlayerTwo.setMoveColor(move.getMoveColor());
                            moveToPlayerTwo.setStartPiece(null);
                            moveToPlayerTwo.setEndPiece(null);
                            moveToPlayerTwo.setAttackWin(false);
                            moveToPlayerTwo.setDefendWin(false);
                        }
                    }

                    GameStatus winCondition = checkWinCondition();

                    toPlayerOne.writeObject(moveToPlayerOne);
                    toPlayerTwo.writeObject(moveToPlayerTwo);

                    toPlayerOne.writeObject(winCondition);
                    toPlayerTwo.writeObject(winCondition);


                    // Change turn color.
                    //ToDo use this change colors in a daemon that has a timer, this daemon should be located in the client and notify the server whenever the turn has changed
                    if (turn == PieceColor.RED)
                        turn = PieceColor.BLUE;
                    else
                        turn = PieceColor.RED;

                    // Check win conditions.
                } catch (IOException | ClassNotFoundException e) {
                    System.out.println(session + "Error occured during network I/O");
                    return;
                }
            } catch (Exception e) {
                System.out.println("CATCH");
                if (turn == PieceColor.RED)
                    turn = PieceColor.BLUE;
                else
                    turn = PieceColor.RED;

            }
        }
    }

This is the game loop that executes always (I know I should add a condition to stop if the game is paused)

The problem here is what to do a the changeTurn() method in order to stop the game loop while it is executing.

The first way that comes to my mind is using exceptions, but this wont work because they are two separate threads therefore the exception stops at the CustomTimerTask.

0

精彩评论

暂无评论...
验证码 换一张
取 消