Search code examples
javatimerillegalstateexceptiontimertask

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


again. I keep getting an IllegalStateException when I try to attempt a task after one turn. Specifically, mentioning that a task is already scheduled or cancelled. Do you know how to fix this? Here is some relevant code.

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;
    }

}

What this code does is play the game of Gipf with a human agent.

It looks like your post is mostly code; please add some more details. (Netbeans needs to get rid of this if you added details about your code!)


Solution

  • The same way you can't reuse a Timer after you cancel it, you cannot reuse a TimerTask.

    You can create classes extending TimerTask and instantiate them whenever you need to schedule it.