2016-07-07 8 views
0

私は、Texas Hold'emのポーカーゲームを実装するUniプロジェクトに取り組んでいます。私は、入力がない場合、プレーヤーがフォールドされる場合、GUIからのユーザー入力を待っている間にゲームを中断する方法に取り組んできました。 私の初期コードはゲームのスレッドでsleep()と呼ばれ、コントローラからはnotify()と呼ばれました。私がこの方法で遭遇した問題は、コントローラが通知したかどうか、またはその睡眠が終わったかどうかを知る方法がないことでした。 これを試してフラグを立てるには、コントローラーにRuntimeExceptionを投げてもらいましたが、ゲームのスレッドでこの例外をキャッチしようとすると大変なトラブルがありました。ここで待機中のRunnableのActionListenerから実行時例外をキャッチ

は私の最小実行可能な例である:

GUI.java

import java.awt.GraphicsConfiguration; 
import java.awt.HeadlessException; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class GUI extends JFrame 
{ 
    private JButton jb; 
    private JPanel jp; 
    private WaitTest wt; 

    public GUI(WaitTest wt) throws HeadlessException 
    { 
     // TODO Auto-generated constructor stub 
     this.wt = wt; 
     jp = new JPanel(); 
     jb = new JButton("Throw Runtime"); 
     jp.add(jb); 
     jb.addActionListener(new TestController(wt)); 
     this.add(jp); 
     pack(); 
     this.setVisible(true); 
    } 

    public GUI(GraphicsConfiguration gc) 
    { 
     super(gc); 
     // TODO Auto-generated constructor stub 
    } 

    public GUI(String title) throws HeadlessException 
    { 
     super(title); 
     // TODO Auto-generated constructor stub 
    } 

    public GUI(String title, GraphicsConfiguration gc) 
    { 
     super(title, gc); 
     // TODO Auto-generated constructor stub 
    } 

} 

TestController.java

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class TestController implements ActionListener 
{ 
    private WaitTest wt; 

    public TestController(WaitTest wt) 
    { 
     this.wt = wt; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e){ 
     // TODO Auto-generated method stub 
     System.out.println("Controller throws runtime"); 
     synchronized(wt.getT()) 
     { 
      throw new RuntimeException("Runtime flying"); 
     } 
    } 
} 

WaitTest.java

import java.util.concurrent.ExecutionException; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 

public class WaitTest implements Runnable 
{ 
    private Thread t; 

    public WaitTest() 
    { 
     // TODO Auto-generated constructor stub 
     this.t = new Thread(); 
    } 

    public Thread getT() 
    { 
     return t; 
    } 

    public void run() 
    { 
     try 
     { 
      synchronized (this) 
      { 
       Thread.sleep(3000); 
       System.out.println("Player folded"); 
      } 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 

      System.out.println("Caught "); 
     } 

     System.out.println("interupt not received"); 

    } 

    public void test() throws InterruptedException 
    { 
     // TODO Auto-generated method stub 
     ExecutorService executor = Executors.newSingleThreadExecutor(); 
     synchronized (t) 
     { 
      Future<?> future = executor.submit(this); 
      try 
      { 
       future.get(); 
      } 
      catch (ExecutionException e) 
      { 
       System.out.println("hooray!! runtime caught"); 
       t.notify(); 
       return; 
      } 
      catch (Exception ex) 
      { 
       System.out.println("Runtime caught"); 
       t.notify(); 
       return; 

      } 
     } 
    } 

} 

Driver.java

public class driver 
{ 

    public driver() 
    { 
     // TODO Auto-generated constructor stub 
    } 

    public static void main(String[] args) throws InterruptedException 
    { 
     // TODO Auto-generated method stub 
     WaitTest wt = new WaitTest(); 
     GUI gui = new GUI(wt); 
     wt.test(); 
     gui.repaint(); 

    } 
} 

巻き込まれることはありませんランタイム例外これをキャッチするための簡単な方法がありますか?またはゲームのループを止めて入力を待つ良い方法がありますか?これは私のスレッド制御/同時実行の最初の試みなので、おそらく単純なものを見落としているでしょう。

答えて

0

これは、WaitTestを(Runnableを実装する)TimerTaskに変換し、Thread()Timer()に変更することで解決できました。タイマーの代わりに、ランタイム例外をスローする必要のその後コントローラで、私はちょうどcancel()呼ば

これは今executorおよび例外

WaitTest.javaルックスで約いじくるの私の目的のために多くのクリーナーですこのように:

import java.util.Timer; 
import java.util.TimerTask; 

public class WaitTest extends TimerTask 
{ 
    private Timer t; 

    public WaitTest() 
    { 
     // TODO Auto-generated constructor stub 
     this.t = new Timer(true); 
    } 
    public void run() 
    { 
     System.out.println("Player folded"); 

    } 

    public void test() 
    { 
     // Schedule task for 5 sec time unless the 
     // controller cancels 
     t.schedule(this, 5000); 

    } 

    public Timer getT() 
    { 
     return t; 
    } 

} 
関連する問題