2017-02-23 49 views
-2

我使用骰子卷创建了一个有趣的游戏GUI。代码看起来像这样:在Java中递归地调用游戏类

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,如果是这样,怎么办?

+0

不,不管你做什么,**不** **使用任何这种递归。而且可能不需要直接使用线程。只需使用一个摆动计时器。 –

+0

我熟悉使用计时器进行延迟任务。你会如何推荐在这种情况下使用它们? –

+0

我看不出有什么方法可以给你提供的具体建议,只有一般性的建议,因为我已经给了。如果你想要一个更具体的答案,你需要提高发布的信息的质量,包括给我们一个更好的想法你的程序结构和更有用的代码 - 准确地说[mcve]。 –

回答

-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 */