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.
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();
}
}