Question

I am in a computer Science class and we have started with using Karel the Robot as the introduction to Java OOP. I want to run two Karel the Robots at the same time, performing two different tasks. I have tried looking up solutions on the internet, and I have been successful in making a working thread, however, I am unable to run two Karel the Robots at the same time. Any help on this Concurrent Programming issue would be appreciated. Here's the code I have been using:

package karel;
import kareltherobot.*;
import kareltherobot.Directions;
import kareltherobot.World;
public class KarelSample implements Directions
{
    public static void main(String [] args)  
    {


          Thread Karelrunner = new Thread();
          Karelrunner.start();
 UrRobot Karel = new UrRobot ( 1,5, North, 2);
 Karel.move();
 Karel.move();
 Karel.putBeeper();
 Karel.turnLeft();
 Karel.move();


}




    static
    {   
    World.setVisible(true);
    World.showSpeedControl(true);



}


    class Karelrunner implements Directions {

        UrRobot Karel2 = new UrRobot(8,8, South, 2);
        Karel2.move();
        Karel2.move();
        Karel2.turnLeft();
        Karel2.turnLeft();
        Karel2.putBeeper();
        Karel2.move();
}



}

I have also used the Karel J Robot book's example where the thread setup code would be as follows:

public static void main (String [] args)
{ ...

Karelrunner r = new Karelrunner();
World.setupThread(r);

  . . .
}

Please help in any way you can, I am trying to make a multi-threaded concurrent program. I am new to this and thanks for your time and attention.

Was it helpful?

Solution 2

I have done more research and found a solution to my problem. Thanks kevinsa5 for the idea. For anyone who may have a problem with multi-threading in Karel the Robot or in general, I'll post my code below. Hopefully, it will help in giving you an idea.

package karel;
import kareltherobot.*;
import kareltherobot.Directions;
import kareltherobot.World;
public class KarelSample implements Directions
{
    public static void main(String [] args)  
    {
        new Karelrunner(8,8,South,2); 

        UrRobot Karel = new UrRobot (1,3,North,2);

        Karel.move();
        Karel.move();
        Karel.putBeeper();
        Karel.move();
        Karel.turnLeft();
        Karel.move();
    }   

    static
    {   
        World.setVisible(true);
        World.showSpeedControl(true);
    }
}

class Karelrunner extends UrRobot implements Directions 
{
    public Karelrunner (int street, int avenue, Direction direction, int beepers)
    { 
        super ( street, avenue, direction, beepers);
        World.setupThread(this);
    }

    public void run()
    {
        move();
        move();
        putBeeper();
        turnLeft();
        move();
    }
}   

OTHER TIPS

Instead of thinking about threading, perhaps looking into setting the delay between actions would be more helpful. Example in pseudocode:

create karel_one
create karel_two
while(true){
setdelay(0)
karel_one.act()
karel_two.act()
setdelay(10) // or whatever
}

So it would appear as if they are acting concurrently. I did this a few years ago, but (as you can probably imagine) karel the robot isn't very useful outside of the classroom, so I don't remember the specifics. I think the delay method might be a part of World? Not sure.

EDIT: You can also use this idea to create 'instant' functions like turnRight() instead of waiting forever to turn left three times... ugh.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top