Taking an intro to Java class because boredom. The helper class I made to make life a bit easier ;)
Last active
August 29, 2015 14:04
-
-
Save wtfaremyinitials/7c11d12288e0ff75fe1a to your computer and use it in GitHub Desktop.
KarelPlusPlus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.will.karelplusplus; | |
import stanford.karel.SuperKarel; | |
public class KarelPlusPlus extends SuperKarel { | |
private static final long serialVersionUID = 1L; | |
// Turn Right | |
public void turnRight() { | |
for(int i=0; i<3; i++) { | |
this.turnLeft(); | |
} | |
} | |
// Direction Logic | |
private Direction currentDirection = Direction.EAST; | |
public enum Direction { | |
NORTH(0), | |
EAST(1), | |
SOUTH(2), | |
WEST(3); | |
Direction(int id) { | |
this.id = id; | |
} | |
private int id; | |
public int getID() { | |
return this.id; | |
} | |
} | |
// Rotate to face | |
public void face(Direction target) { | |
int turns = target.getID() - currentDirection.getID(); | |
if(turns > 0) { | |
for(int i=0; i<turns; i++) { | |
turnRight(); | |
} | |
} | |
if(turns < 0) { | |
for(int i=0; i>turns; i--) { | |
turnLeft(); | |
} | |
} | |
currentDirection = target; | |
} | |
// Reset to East | |
public void reset() { | |
currentDirection = Direction.EAST; | |
} | |
// Better version of move | |
public void move(Direction target) { | |
face(target); | |
move(); | |
} | |
// Move X number of times | |
public void move(Direction target, int times) { | |
for(int i=0; i<times; i++) { | |
move(target); | |
} | |
} | |
// Move until you hit a wall | |
public void go(Direction target) { | |
move(target); | |
while(frontIsClear()) | |
move(target); | |
} | |
// Attempt to put a beeper | |
public boolean tryPutBeeper() { | |
if(!beepersPresent()) { | |
putBeeper(); | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment