中级实训Part3测试文档

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();
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值