在 Human-CPU 游戏中安排两个计时器任务时多个计时器运行

Multiple Timers going when two timer tasks are scheduled during a Human-CPU game

再次。当我尝试在一轮后尝试执行任务时,我总是收到 IllegalStateException。具体来说,提到一项任务已经安排或取消。你知道如何解决这个问题吗?这是一些相关代码。

public class Human_Agent implements GipfPlayable {

    String[] edgeSpots;
    String[] columns;
    Random rng;
    GipfGame mg;
    //int turn = 0;
    String str;
    Timer t;
    Integer val;
    Integer myDir;
    public Human_Agent(GipfGame g) {
        this.edgeSpots = g.getEdgeSpots();
        this.columns = g.getColumns();
        rng = new Random(37);
        //turn = 0;
    }
    TimerTask task1 = new TimerTask()
    {
        public void run()
        {

            if (str.equals(""))
            {
                System.out.println("You did not input a valid location.");
                System.exit(0);
            }

        }
    };
    TimerTask task2 = new TimerTask()
    {
        public void run()
        {

            if (val == null)
            {
                System.out.println("You did not input a valid direction.");
                System.exit(0);
            }
        }
    };

    public String getUserInput() throws Exception
    {
        t = new Timer();
        t.schedule(task1, 5*1000);
        Scanner scan = new Scanner(System.in);
        System.out.print("Please provide a starting location: ");
        String startLocation = "";
        String x = scan.nextLine();
        str = x;
        int dirE = 0;
        //t.cancel();
       // Timer t2 = new Timer();
        if (isItValidLocation(x))
        {
        startLocation = x;
        t.cancel();
        t = new Timer();
        t.schedule(task2, 10*1000);
        //t.scheduleAtFixedRate(task2, 5000, 5*1000);
        System.out.print("\n Now, provide a number to move the piece");
         dirE = scan.nextInt();
         t.cancel();
        }
        else
        {
            System.out.println("Invalid Start location.");
            t.cancel();
        }
       // scan.close();
       // t.cancel();
         return str + " " + Integer.toString(dirE);
    }
    @Override
    public String makeGipfMove() {
        String s;
        try
        {
            s = getUserInput();
        }
        catch(Exception e)
        {
        String startLocation = edgeSpots[rng.nextInt(edgeSpots.length)];
        Integer dir = rng.nextInt(6);
        s = startLocation + " " + Integer.toString(dir);
        e.printStackTrace();
        }

//        if ((mg.makeMove(x, dirE)) == false)
//        {
//            System.out.println("Can't move here.");
//        }
        //mg.makeMove(x, dirE);
       // Integer dir = rng.nextInt(6);
        //System.out.println("GIPHS REMAINING: " + )

       return s;
    }
    public boolean isItValidLocation(String a)
    {
        boolean v = false;
        System.out.println("YOU ENTERED: " + a);
        for (int i = 0; i < edgeSpots.length; i++)
        {
            System.out.println(edgeSpots[i]);
            if ((edgeSpots[i].equals(a)) && (v != true))
            {
                 v = true;
            }

        }
        System.out.println(v);
        return v;
    }

}

此代码的作用是与人类代理玩 Gipf 游戏。

看起来你的 post 主要是代码;请添加更多详细信息。 (如果您添加了有关代码的详细信息,Netbeans 需要摆脱它!)

与取消后无法重复使用 Timer 一样,您也无法重复使用 TimerTask

您可以创建 类 扩展 TimerTask 并在需要安排时实例化它们。