用Java递归调用游戏类

时间:2017-02-23 01:41:58

标签: java swing user-interface

我正在使用骰子滚动创建游戏GUI以获得乐趣。代码看起来像这样:

public class run {
    //set up frame
    Game game = new Game();
    game.run();
}

public class Game {

    public void run() {
         Round round = new Round();
         int playerWhoLostADie = round.run();

         //handle if the game ends
         //otherwise, call recursively
         run(playerWhoLostADie);
    }
}

public class Round {

    public int run() {
         Turn turn = new Turn();
         Bet bet = turn.run();

         //if the round is over
         return(currentPlayer);
         //otherwise, call recursively
         run(bet);
    }
}

public class Turn {

    public Bet run() {
         //handle betting
         return bet;
    }
}

以递归的方式轮流调用是一种智能的方法吗?我应该使用单独的线程以避免冻结GUI,如果是,如何?

1 个答案:

答案 0 :(得分:-1)

为什么使用递归来实现游戏中的转弯?我会选择这样的解决方案:

public class Main {

    public static void main(String[] args) {
        Game game = new Game();
        game.run();
    }

}


public class Game {

    public void run() {
        boolean gameOver = false;

        while (!gameOver) {
            Round round = new Round();
            int playerWhoLost = round.run();

            /* if the game ends, then gameOver = true */
        }
    }

}


/* other classes related to the Game */