2017-02-23 48 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 */