Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kareltherobot.Robot;
- import kareltherobot.World;
- public class RandomBot extends Robot
- {
- int steps;
- String name;
- int beepersDwn;
- int forward;
- int right;
- int left;
- int backwards;
- public RandomBot(String n, int street, int avenue, Direction direction, int beepers, int s, int bpd, int fwd, int lft, int rght, int back)
- {
- super(street, avenue, direction, beepers);
- steps = s;
- name = n;
- beepersDwn = bpd;
- forward = fwd;
- left = lft;
- right = rght;
- backwards = back;
- }
- public static void main(String[]args)
- {
- World.setVisible(true);
- World.readWorld("Random.kwld");
- World.showSpeedControl(true);
- World.setTrace(false);
- RandomBot bot = new RandomBot("Bob", 50, 50, North, infinity, 0, 0, 0, 0, 0, 0);
- bot.start();
- }
- public void start()
- {
- while(frontIsClear())
- {
- randomize();
- }
- output();
- }
- public void output()
- {
- System.out.println(name + " has taken " + steps + " steps and has put down " + beepersDwn + " beepers down." +
- "\nNot only that, but " + name + " has moved " + forward + " times forward, " + backwards + " times backwards, " + right + " times right, and " + left + " times left.");
- }
- public void move()
- {
- super.move();
- steps++;
- }
- public void putBeeper()
- {
- super.putBeeper();
- beepersDwn++;
- }
- public void turnRight()
- {
- turnLeft();
- turnLeft();
- turnLeft();
- }
- public void turnBack()
- {
- turnLeft();
- turnLeft();
- }
- public void moveRight()
- {
- turnRight();
- move();
- }
- public void moveLeft()
- {
- turnLeft();
- move();
- }
- public void moveBack()
- {
- turnBack();
- move();
- }
- public static double getRandom()
- {
- double a = ((Math.random())*10);
- double b = Math.ceil(a);
- return b;
- }
- public void randomize()
- {
- double a = getRandom();
- if(a==2)
- {
- move();
- forward++;
- }
- if(a==4)
- {
- moveRight();
- right++;
- }
- if(a==6)
- {
- moveLeft();
- left++;
- }
- if(a==8)
- {
- moveBack();
- backwards++;
- }
- if(a==10)
- {
- putBeeper();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement