Skip to content

Commit

Permalink
Basic commands for the robot
Browse files Browse the repository at this point in the history
  • Loading branch information
EridanPPCG committed Feb 9, 2016
1 parent 70b0dfc commit fbdaad4
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions Commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
//This class creates methods to govern the basic movements of the robot.
//Created 02/08/2016 by Trevor B. (PPCG user Eridan)

import kareltherobot.*;

public class Commands extends Robot
{
private int xcoord;
private int ycoord;
private Direction dir;
private int beeperCount;

public Commands(int street, int avenue, Direction direction, int beepers)
{
super(street, avenue, direction, beepers);

xcoord=avenue;
ycoord=street;
dir=direction;
beeperCount=beepers;
}

//MOVEMENT

public void move(int count)
{
for(int i=0;i<count;i++)
{
super.move();
}

if(facingNorth())
{
ycoord+=count;
}
else if(facingSouth())
{
ycoord-=count;
}
else if(facingEast())
{
xcoord+=count;
}
else if(facingWest())
{
xcoord-=count;
}
}

public void move()
{
move(1);
}

public void turnLeft(int count)
{
for(int i=0;i<count;i++)
{
super.turnLeft();
}
}

public void turnLeft()
{
turnLeft(1);
}

public void turnRight()
{
turnLeft(3);
}

public void turnAround()
{
turnLeft(2);
}

//DIRECTIONS

public void faceNorth()
{
while(!facingNorth())
{
turnLeft();
}
dir=North;
}

public void faceSouth()
{
while(!facingSouth())
{
turnLeft();
}
dir=South;
}

public void faceEast()
{
while(!facingEast())
{
turnLeft();
}
dir=East;
}

public void faceWest()
{
while(!facingWest())
{
turnLeft();
}
dir=West;
}

//GOTO

public void goTo(int xdest,int ydest)
{
int xdiff=xdest-xcoord;
int ydiff=ydest-ycoord;

if(xdiff>0)
{
faceEast();
}
else
{
faceWest();
}
move(xdiff);

if(ydiff>0)
{
faceNorth();
}
else
{
faceSouth();
}
move(ydiff);
}

//BEEPER MANAGEMENT

public void putBeeper(int count)
{
for(int i=0;i<count;i++)
{
super.putBeeper();
}
beeperCount-=1;
}

public void putBeeper()
{
putBeeper(1);
}

public void pickBeeper(int count)
{
for(int i=0;i<count;i++)
{
super.pickBeeper();
}
beeperCount+=1;
}

public void pickBeeper()
{
pickBeeper(1);
}

public void putAllBeepers()
{
while(anyBeepersInBeeperBag())
{
putBeeper();
}
}

public void pickAllBeepers()
{
while(nextToABeeper())
{
pickBeeper();
}
}
}

0 comments on commit fbdaad4

Please sign in to comment.