. ├── src │ ├── Jumper.java │ └── JumperRunner.java │ └── JumperTest.java ├── designreport.md ├── testreport.md └── ReadmeCoding exercises
You are asked to create a class called Jumper. This actor can move forward two cells in each move. It “jumps” over rocks and flowers. It does not leave anything behind it when it jumps.
-
Inception: clarify the details of the problem:
a. What will a jumper do if the location in front of it is empty, but the location two cells in front contains a flower or a rock?
此时jumper转向。b. What will a jumper do if the location two cells in front of the jumper is out of the grid?
此时jumper转向。c. What will a jumper do if it is facing an edge of the grid?
此时jumper转向。d. What will a jumper do if another actor (not a flower or a rock) is in the cell that is two cells in front of the jumper?
此时jumper转向。e. What will a jumper do if it encounters another jumper in its path?
此时jumper转向。f. Are there any other tests the jumper needs to make?
No.* You should answer the above questions simply in your design
report. -
Elaboration: solve the following problems:
a. Which class should Jumper extend?
Actor class.b. Is there an existing class that is similar to the Jumper class?
有,Bug class。c. Should there be a constructor? If yes, what parameters should be specified for the constructor?
Yes, color.d. Which methods should be overridden?
public void act(); public void turn(); public void move(); public boolean canMove();
e. What methods, if any, should be added?
public void turn(); public void move(); public boolean canMove();
f. What is the plan for testing the class?
- 测试离边界只有一格时不会越界
- 测试在边界时不会越界
- 测试正前第二格为空时,正前一格分别为空时能移动
- 测试正前第二格为空时,正前一格分别为flower时能移动
- 测试正前第二格为空时,正前一格分别为rock时能移动
- 测试正前第二格为空时,正前一格分别为bug时不能移动
- 测试正前第二格有actor时不能移动
* You don’t need to write down your answers to this group of questions, just think about them and go to the construction part.
-
Construction: Implement the Jumper and JumperRunner classes.
Jumper.javaimport info.gridworld.actor.Actor; import info.gridworld.actor.Flower; import info.gridworld.actor.Rock; 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 JumperColor) { setColor(JumperColor); } 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()); Location doubleNext = next.getAdjacentLocation(getDirection()); if (gr.isValid(next) && gr.isValid(doubleNext)) moveTo(doubleNext); else removeSelfFromGrid(); // Flower flower = new Flower(getColor()); // flower.putSelfInGrid(gr, loc); } public boolean canMove() { Grid gr = getGrid(); if (gr == null) return false; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (!gr.isValid(next)) //正前方一格无效 return false; Location doubleNext = next.getAdjacentLocation(getDirection()); if (!gr.isValid(doubleNext)) //正前方第二格无效 return false; Actor neighbor = gr.get(next); Actor doubleNeighbor = gr.get(doubleNext); return (doubleNeighbor == null) && ( (neighbor == null) || (neighbor instanceof Flower) || (neighbor instanceof Rock) ); // true:正前方一格是空/花/石头且正前方第二格是空 } }
JumperRunner.java
import info.gridworld.actor.ActorWorld; import info.gridworld.actor.Flower; import info.gridworld.actor.Rock; import info.gridworld.actor.Bug; import info.gridworld.actor.Critter; public class JumperRunner { public static void main(String[] args) { // TODO Auto-generated method stub ActorWorld world = new ActorWorld(); world.add(new Jumper()); world.add(new Flower()); world.add(new Rock()); world.add(new Bug()); // world.add(new Critter()); world.show(); } }
-
Test: Carry out the test plan with junit to verify that the Jumper class meets the specification.
JumperTest.java:import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import info.gridworld.actor.ActorWorld; import info.gridworld.grid.Location; import info.gridworld.actor.Flower; import info.gridworld.actor.Rock; import info.gridworld.actor.Bug; public class JumperTest { private Jumper jumper1; // 测试离边界只有一格时 private Jumper jumper2; // 测试在边界时 private Jumper jumper3; // 测试正前第二格为空时,正前一格分别为空 private Jumper jumper4; // 测试正前第二格为空时,正前一格分别为flower private Jumper jumper5; // 测试正前第二格为空时,正前一格分别为rock private Jumper jumper6; // 测试正前第二格为空时,正前一格分别为bug private Jumper jumper7; // 测试正前第二格有actor @Before public void setUp() { ActorWorld world = new ActorWorld(); jumper1 = new Jumper(); jumper2 = new Jumper(); jumper3 = new Jumper(); jumper4 = new Jumper(); jumper5 = new Jumper(); jumper6 = new Jumper(); jumper7 = new Jumper(); world.add(new Location(1,0),jumper1); world.add(new Location(0,1),jumper2); world.add(new Location(5,0),jumper3); world.add(new Location(5,1),jumper4); world.add(new Location(5,2),jumper5); world.add(new Location(5,3),jumper6); world.add(new Location(5,6),jumper7); world.add(new Location(4,1),new Flower()); world.add(new Location(4,2),new Rock()); world.add(new Location(4,3),new Bug()); world.add(new Location(3,6),new Flower()); world.show(); } @Test public void testJumper1() { assertEquals(jumper1.canMove(),false); } @Test public void testJumper2() { assertEquals(jumper2.canMove(),false); } @Test public void testJumper3() { assertEquals(jumper3.canMove(),true); } @Test public void testJumper4() { assertEquals(jumper4.canMove(),true); } @Test public void testJumper5() { assertEquals(jumper5.canMove(),true); } @Test public void testJumper6() { assertEquals(jumper6.canMove(),false); } @Test public void testJumper7() { assertEquals(jumper7.canMove(),false); } }
测试布局:
测试结果:
结果与预期一致,成功实现。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)