2016-12-03 60 views
0

我有用Java編寫的Sudoku遊戲。遊戲的主要過程是主要方法。我有一個while循環,其中我有myClassObject.getCommandFromScanner()函數來讀取輸入與掃描儀,所以用戶可以填補數獨板上的空位或從菜單中選擇其他選項(例如保存,顯示解決方案和退出到主菜單)。此循環僅在用戶選擇退出到菜單時結束,因爲函數myClassObject.getCommandFromScanner()僅在該情況下返回false。 ((((System.currentTimeMillis() - nGame.timeAtStart < 10000)& & myClassObject.getCommandFromScanner()))< - 第一次第一次,第一次,第一次,第一次,我想設置遊戲的指定時間,但不能在while循環條件爲真,並且循環可以等待用戶輸入。只有當用戶輸入了某些內容時,循環條件纔會再次chceck,然後退出該循環。我怎麼解決這個問題?我也嘗試使用ExecutorService,它幾乎工作,循環執行指定的時間,但它然後退出菜單,用戶無法啓動新遊戲。掃描儀停止正常工作,我的主窗口與遊戲崩潰...我在這裏是新的,所以很抱歉,如果我的問題不夠清楚。我希望你能幫助我如何設置方法的最大執行時間?

public class Sudoku 
{ 
public static void main(String[] args) throws Exception 
{   
while(course != exit) 
{ 
    opt = new String(in.next()); 
    int option; 

    option = Character.getNumericValue(opt.codePointAt(0)); 

    if(course == menu) 
    { 
     //go to menu 
    } 

    else if(course == newGame) 
    { 
     //setup new game 
    } 

    if(course == game) 
    { 
     nGame.timeAtStart=System.currentTimeMillis(); 
     myClassObject.setGame(newGame); 

     ExecutorService executor = Executors.newFixedThreadPool(1); 
     Future<?> future = executor.submit(new Runnable() { 
      @Override 
      public void run() { 
       try{ 
        while(myClassObject.getCommandFromScanner()); 
       } 
       catch(Exception e) {} 
      } 
     }); 

     executor.shutdown(); 

     try { 
      future.get(8, TimeUnit.SECONDS); // 
     } catch (InterruptedException e) { // 
      System.out.println("job was interrupted"); 
     } catch (ExecutionException e) { 
      System.out.println("caught exception: " + e.getCause()); 
     } catch (TimeoutException e) { 
      future.cancel(true); 
      System.out.println("Your game timed out!"); 
     } 

     course.printMessage("TIMEOUT"); 
     course = menu; 

    } 
} 
} 
} 

這是我的代碼的原始版本:

public class Sudoku 
{ 
public static void main(String[] args) throws Exception 
{   
while(course != exit) 
{ 
    opt = new String(in.next()); 
    int option; 

    option = Character.getNumericValue(opt.codePointAt(0)); 

    if(course == menu) 
    { 
     //go to menu 
    } 

    else if(course == newGame) 
    { 
     //setup new game 
    } 

    if(course == game) 
    { 
     nGame.timeAtStart=System.currentTimeMillis(); 
     myClassObject.setGame(newGame); 
     while(myClassObject.getCommandFromScanner()); //while user don't choose 'menu' option, read input from keyboard 
     course = menu; //user choose 'menu', go to menu section 

    } 
    } 
    } 

} 我要爲遊戲的最大時間,然後顯示消息:「超時」和去主菜單

+1

[在Java超時方法(http://stackoverflow.com/questions/8982420/time-out-method-in-java)所有的 – brummfondel

回答

0

中,TimerTask可能會有幫助:

TimerTask task = new TimerTask() 
{ 
    public void run() 
    { 
     if(str.equals("")) 
     { 
      System.out.println("TimeOut. exit..."); 
      System.exit(0); //or other operation. 
     } 
    }  
}; 
Timer timer = new Timer(); 
timer.schedule(task, 10*1000); //10 seconds 
String cmd = myClassObject.getCommandFromScanner(); 
timer.cancel(); //if not timeout ,cancle it. 
System.out.println("you have entered: "+ cmd); 

編號:https://stackoverflow.com/a/5854048/6037575

更新:

設置超時整場比賽:

static void main(){ 
... 
// ready to start the game 
TimerTask task = new TimerTask() 
{ 
    public void run() 
    { 

      System.out.println("TimeOut.Return to the mani menu."); 
      course = menu; // how to return main menu is dependent on you 

    }  
}; 

while(true){ 
// Here the user is playing the game. 
// and if he can work it out before timeout 
// you should do something else to end the game. 
} 

對不起,我不能給你可以工作得取決於你給的代碼真正的代碼,但只有一些僞代碼我認爲這可能是一個可行的解決方案。

+0

首先,我的功能getCommandFromScanner返回布爾的可能的複製。它讀取輸入並返回true,如果用戶寫入命令以填充板上的空位,如果選擇提示,保存遊戲,則返回false,如果用戶想退出到主菜單(例如他可以開始新遊戲),則返回false。只有我的while循環中斷。用戶可以隨時輸入命令,但如果持續時間超過10秒鐘,我想停止遊戲並返回主菜單。 – adnut

+0

那麼你是不是隻等待用戶按'Enter'?如果他按下,則scan.reaLline()返回。而且你可以繼續你的遊戲。但是如果在他按下'Enter'之前已經過了10秒,你就退出了遊戲。 –

+0

我不想退出,只是爲了返回false。但運行方法是void – adnut