Skip to content

Instantly share code, notes, and snippets.

@poemdexter
Created February 1, 2012 02:35
Show Gist options
  • Save poemdexter/1714705 to your computer and use it in GitHub Desktop.
Save poemdexter/1714705 to your computer and use it in GitHub Desktop.
rover kata java, junit 1.31.12
package PokerKata;
import org.junit.Assert.*;
public class Rover {
public class Bot {
public String facing;
public int X;
public int Y;
public Bot(String face, int x, int y) {
facing = face;
X = x;
Y = y;
}
}
public Bot createBot(String face, int x, int y) {
return new Bot(face, x, y);
}
public void go(Bot bot, String str) {
if (str.equals("f")) { //forward commands
if (bot.facing.equals("W")) {
bot.X--;
} else if (bot.facing.equals("E")) {
bot.X++;
} else {
if (bot.Y == 1) {
bot.Y = 0;
} else {
bot.Y++;
}
}
}
if (str.equals("b")) //backward commands
{
bot.Y -= 1;
if (bot.Y == -1) {
bot.Y = 1;
}
}
}
}
package PokerKata;
import PokerKata.Rover.Bot;
import org.junit.Assert;
import org.junit.Test;
public class RoverTest {
public RoverTest() { }
@Test
public void testMoveNorth() {
Rover rover = new Rover();
Bot bot = rover.createBot("N", 0, 0);
rover.go(bot, "f");
Assert.assertEquals(0, bot.X);
Assert.assertEquals(1, bot.Y);
Assert.assertEquals("N", bot.facing);
}
@Test
public void testMoveSouth() {
Rover rover = new Rover();
Bot bot = rover.createBot("N",1,1);
rover.go(bot, "b");
Assert.assertEquals(1, bot.X);
Assert.assertEquals(0, bot.Y);
}
@Test
public void testLoopSouth() {
Rover rover = new Rover();
Bot bot = rover.createBot("N",0,0);
rover.go(bot, "b");
Assert.assertEquals(0, bot.X);
Assert.assertEquals(1, bot.Y);
}
@Test
public void testLoopNorth() {
Rover rover = new Rover();
Bot bot = rover.createBot("N",1,1);
rover.go(bot, "f");
Assert.assertEquals(1, bot.X);
Assert.assertEquals(0, bot.Y);
}
@Test
public void testMoveForwardWhileFacingWest() {
Rover rover = new Rover();
Bot bot = rover.createBot("W", 1, 0);
rover.go(bot, "f");
Assert.assertEquals(0, bot.X);
Assert.assertEquals(0, bot.Y);
}
@Test
public void testMoveForwardWhileFacingEast() {
Rover rover = new Rover();
Bot bot = rover.createBot("E", 0, 0);
rover.go(bot, "f");
Assert.assertEquals(1, bot.X);
Assert.assertEquals(0, bot.Y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment