PowerKarel.java
Retired DISLab
package hufs.dislab.karel; import stanford.karel.KarelProgram; import stanford.karel.SuperKarel; public class PowerKarel extends SuperKarel { public PowerKarel() { } public void turn(int direction) { switch(direction) { case KarelProgram.EAST : turnEast(); break; case KarelProgram.WEST : turnWest(); break; case KarelProgram.SOUTH : turnSouth(); break; case KarelProgram.NORTH : turnNorth(); break; default : throw new IllegalArgumentException("wrong direction"); } } public void turnNorth() { if (facingSouth()) { turnAround(); } else if (facingEast()) { turnLeft(); } else if (facingWest()) { turnRight(); } } public void turnSouth() { if (facingNorth()) { turnAround(); } else if (facingEast()) { turnRight(); } else if (facingWest()) { turnLeft(); } } public void turnEast() { if (facingWest()) { turnAround(); } else if (facingNorth()) { turnRight(); } else if (facingSouth()) { turnLeft(); } } public void turnWest() { if (facingEast()) { turnAround(); } else if (facingNorth()) { turnLeft(); } else if (facingSouth()) { turnRight(); } } public void moveNorth() { turnNorth(); move(); } public void moveSouth() { turnSouth(); move(); } public void moveEast() { turnEast(); move(); } public void moveWest() { turnWest(); move(); } public boolean northIsBlocked() { if (facingNorth()) { return frontIsBlocked(); } else if (facingEast()) { return leftIsBlocked(); } else if (facingWest()) { return rightIsBlocked(); } else { turnAround(); boolean b = frontIsBlocked(); turnAround(); return b; } } public boolean southIsBlocked() { if (facingSouth()) { return frontIsBlocked(); } else if (facingEast()) { return rightIsBlocked(); } else if (facingWest()) { return leftIsBlocked(); } else { turnAround(); boolean b = frontIsBlocked(); turnAround(); return b; } } public boolean eastIsBlocked() { if (facingEast()) { return frontIsBlocked(); } else if (facingNorth()) { return rightIsBlocked(); } else if (facingSouth()) { return leftIsBlocked(); } else { turnAround(); boolean b = frontIsBlocked(); turnAround(); return b; } } public boolean westIsBlocked() { if (facingWest()) { return frontIsBlocked(); } else if (facingNorth()) { return rightIsBlocked(); } else if (facingSouth()) { return leftIsBlocked(); } else { turnAround(); boolean b = frontIsBlocked(); turnAround(); return b; } } public boolean northIsClear() { return !northIsBlocked(); } public boolean southIsClear() { return !southIsBlocked(); } public boolean eastIsClear() { return !eastIsBlocked(); } public boolean westIsClear() { return !westIsBlocked(); } }