Advertisement
Nalakava

RandomBot

Dec 1st, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. import kareltherobot.Robot;
  2. import kareltherobot.World;
  3.  
  4. public class RandomBot extends Robot
  5. {  
  6.    int steps;
  7.    String name;
  8.    int beepersDwn;
  9.    int forward;
  10.    int right;
  11.    int left;
  12.    int backwards;
  13.    
  14.    public RandomBot(String n, int street, int avenue, Direction direction, int beepers, int s, int bpd, int fwd, int lft, int rght, int back)
  15.    {    
  16.       super(street, avenue, direction, beepers);
  17.       steps = s;
  18.       name = n;
  19.       beepersDwn = bpd;
  20.       forward = fwd;
  21.       left = lft;
  22.       right = rght;
  23.       backwards = back;
  24.    }
  25.    
  26.    public static void main(String[]args)
  27.    {
  28.       World.setVisible(true);
  29.       World.readWorld("Random.kwld");
  30.       World.showSpeedControl(true);
  31.       World.setTrace(false);
  32.      
  33.       RandomBot bot = new RandomBot("Bob", 50, 50, North, infinity, 0, 0, 0, 0, 0, 0);    
  34.       bot.start();  
  35.    }
  36.    
  37.    public void start()
  38.    {
  39.       while(frontIsClear())
  40.       {
  41.          randomize();
  42.       }
  43.      
  44.       output();      
  45.    }
  46.    
  47.    public void output()
  48.    {
  49.       System.out.println(name + " has taken " + steps + " steps and has put down " + beepersDwn + " beepers down." +
  50.       "\nNot only that, but " + name + " has moved " + forward + " times forward, " + backwards + " times backwards, " + right + " times right, and " + left + " times left.");
  51.    }
  52.    
  53.    public void move()
  54.    {
  55.       super.move();
  56.       steps++;
  57.    }
  58.    
  59.    public void putBeeper()
  60.    {
  61.       super.putBeeper();
  62.       beepersDwn++;
  63.    }
  64.    
  65.    public void turnRight()
  66.    {
  67.       turnLeft();
  68.       turnLeft();
  69.       turnLeft();
  70.    }  
  71.    
  72.    public void turnBack()
  73.    {
  74.       turnLeft();
  75.       turnLeft();
  76.    }
  77.    
  78.    public void moveRight()
  79.    {
  80.       turnRight();
  81.       move();
  82.    }
  83.    
  84.    public void moveLeft()
  85.    {
  86.       turnLeft();
  87.       move();
  88.    }
  89.    
  90.    public void moveBack()
  91.    {
  92.       turnBack();
  93.       move();
  94.    }
  95.    public static double getRandom()
  96.    {
  97.       double a = ((Math.random())*10);
  98.       double b = Math.ceil(a);
  99.       return b;
  100.    }
  101.    public void randomize()
  102.    {
  103.       double a = getRandom();
  104.      
  105.       if(a==2)
  106.       {
  107.          move();
  108.          forward++;
  109.       }
  110.      
  111.       if(a==4)
  112.       {
  113.          moveRight();
  114.          right++;
  115.       }
  116.      
  117.       if(a==6)
  118.       {
  119.          moveLeft();
  120.          left++;
  121.       }  
  122.      
  123.       if(a==8)
  124.       {
  125.          moveBack();
  126.          backwards++;
  127.       }
  128.      
  129.       if(a==10)
  130.       {
  131.          putBeeper();
  132.       }
  133.    }
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement