A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© cmz456123 中级黑马   /  2016-4-26 22:35  /  445 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

              package unit2;

/**
* Created by 95 on 2016/4/22.
*/
public abstract class Unit {
    private String name;
    private FPoint position;

    public Unit() {
    }

    public Unit(String name, FPoint position) {
        this.name = name;
        this.position = position;
    }

    public Unit(FPoint position) {
        this.position = position;
    }

    public int getX() {
        return position.getX();
    }

    public void setX(int x) {
        position.setX(x);
    }

    public void setY(int y) {
        position.setY(y);
    }

    public int getY() {
        return position.getY();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



    public abstract boolean move() ;

    protected void changePosition(int rollNumber) {
        for(int i=0;i<rollNumber;i++){
            if(arrayExist(bottom)){
                setX(getX()+1);
            }else if(exist(left)){
                setY(getY()-1);
            }else if(exist(right)){
                setY(getY()+1);
            }else{
                System.out.println("判断失败,超出了想象");
            }
            if(getX()==24&&getY()==29){
                return;
            }
        }
    }

    private boolean arrayExist(FPoint[] bottom) {
        for(int i=0;i<bottom.length;i++){
            if(bottom[i].getX() == getX() && bottom[i].getY()==getY()){
                return true;
            }
        }
        return false;
    }



    static int [] right = new int[]{0,8,16,24};
    static FPoint [] bottom = new FPoint[]{new FPoint(0,29),new FPoint(1,29),new FPoint(2,29),new FPoint(3,29),
            new FPoint(4,0),new FPoint(5,0),new FPoint(6,0),new FPoint(7,0),
            new FPoint(8,29),new FPoint(9,29),new FPoint(10,29),new FPoint(11,29),
            new FPoint(12,0),new FPoint(13,0),new FPoint(14,0),new FPoint(15,0),
            new FPoint(16,29),new FPoint(17,29),new FPoint(18,29),new FPoint(19,29),
            new FPoint(20,0),new FPoint(21,0),new FPoint(22,0),new FPoint(23,0),};
    static int [] left = new int[]{4,12,20};

    private boolean exist(int [] array){
        for(int i=0;i<array.length;i++){
            if(array[i]==getX()){
                return true;
            }
        }
        return false;
    }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马