Jumper测试文档
jumper需要完成的test有:
- jumper面对着边界时不进行跳跃
- jumper跳跃的位置在grid外面时不进行跳跃
- jumper跳跃的位置有石头或者花时不进行跳跃
- jumper跳跃的位置为空时进行跳跃
测试前:从左到右依次为jumper1、jumper2、jumper3、jumper4。
测试后:jumper1、jumper2、jumper3不进行跳跃而是转向,jumper4成功跳跃。
JumperTest结果:运行4个测试,一个测试failure(jumper4成功跳跃)。
JumperTest代码:
import static org.junit.Assert.*; import org.junit.Before; import org.junit.After; import org.junit.Test; import info.gridworld.actor.*; import info.gridworld.grid.*; public class JumperTest { private Jumper jumper1; private Jumper jumper2; private Jumper jumper3; private Jumper jumper4; @Before public void setUp() { ActorWorld world = new ActorWorld(); jumper1 = new Jumper(); jumper2 = new Jumper(); jumper3 = new Jumper(); jumper4 = new Jumper(); world.add(new Location(0,0),jumper1); world.add(new Location(1,1),jumper2); world.add(new Location(4,4),jumper3); world.add(new Location(2,4),new Rock()); world.add(new Location(4,5),jumper4); world.add(new Location(3,5),new Flower()); world.show(); } @After public void tearDown() { } @Test public void testJumper1() { assertEquals(jumper1.canMove(),false); } @Test public void testJumper2() { assertEquals(jumper2.canMove(),false); } @Test public void testJumper3() { assertEquals(jumper3.canMove(),false); } @Test public void testJumper4() { assertEquals(jumper4.canMove(),false); } }- Jumper
import info.gridworld.actor.*; import info.gridworld.grid.Grid; import info.gridworld.grid.Location; import java.awt.Color; public class Jumper extends Actor { public Jumper() { setColor(Color.RED); } public Jumper(Color bugColor) { setColor(bugColor); } public void act() { if (canMove()) { move(); } else { turn(); } } public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); } public void move() { Grid gr = getGrid(); if (gr == null) { return; } Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); next = next.getAdjacentLocation(getDirection()); if (gr.isValid(next)) { moveTo(next); } else { removeSelfFromGrid(); } } public boolean canMove() { Grid gr = getGrid(); if (gr == null) { return false; } Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); next = next.getAdjacentLocation(getDirection()); if (!gr.isValid(next)) { return false; } Actor neighbor = gr.get(next); return neighbor == null; } }
JumperRunner
import info.gridworld.actor.*; import info.gridworld.grid.*; public final class JumperRunner { private JumperRunner() { } public static void main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Location(0,0), new Jumper()); world.add(new Location(1,1), new Jumper()); world.add(new Location(4,4), new Jumper()); world.add(new Location(2,4), new Rock()); world.add(new Location(4,5), new Jumper()); world.add(new Location(3,5), new Flower()); world.show(); } }