Exemplo n.º 1
0
 public Tile cellContent(GamePostion postion)
 {
     if(this.withinBounds(postion)){
         return this.cells[postion.x,postion.y];
     }
     return null;
 }
Exemplo n.º 2
0
    public List<GamePostion> availableCells()
    {
        List<GamePostion> cells =  new List<GamePostion>();
        for(int i = 0; i < this.size;i++){
            for(int j = 0; j < this.size;j++){
                Tile t = this.cells[i,j];
                if(t == null){
                    GamePostion gp = new GamePostion(i,j);
                    cells.Add( gp);
                }
            }
        }

        return cells;
    }
Exemplo n.º 3
0
    public Hashtable buildTraversals(GamePostion vector)
    {
        Hashtable reslut = new Hashtable();
        List<int> x = new List<int>();
        List<int> y = new List<int>();
        for(int i = 0;i < size; i++){
            x.Add(i);
            y.Add(i);
        }

        if(vector.x == 1)
            x.Reverse();
        if(vector.y == 1)
            y.Reverse();

        reslut.Add("x",x);
        reslut.Add("y",y);

        return reslut;
    }
Exemplo n.º 4
0
    public Hashtable findFarthestPosition(GamePostion cell,GamePostion vector)
    {
        //		Debug.Log("findFarthestPosition cell  x:"+cell.x + "   y:"+cell.y);

        GamePostion previous;

        do{
            previous = cell;
            cell = new GamePostion(previous.x+ vector.x,previous.y+ vector.y);
        }while(grid.withinBounds(cell) && grid.cellAvailable(cell));

        Hashtable result = new Hashtable();
        result.Add("farthest",previous);
        result.Add("next",cell);
        //		Debug.Log("之前的坐标farthest x:"+ previous.ToString());
        //		Debug.Log("最近的坐标next x:"+ cell.ToString());
        return result;
    }
Exemplo n.º 5
0
 public bool tileMatchesAvailable()
 {
     Tile tile;
     for(int x=0;x<size;x++){
         for(int y=0;y<size;y++){
             tile = grid.cellContent(new GamePostion(x,y));
             if(tile != null){
                 for(int direction=0;direction<4;direction++){
                     GamePostion vector = getVector((MyDirection)direction);
                     GamePostion cell = new GamePostion(x+vector.x,y+vector.y);
                     Tile other = grid.cellContent(cell);
                     if(other !=null){
                         Tile t = (Tile)other;
                         Tile t1 =(Tile)tile;
                         if(t.value == t1.value){
                             return true;
                         }
                     }
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 6
0
 public bool positionsEqual(GamePostion first,GamePostion second)
 {
     return first.x == second.x && first.y == second.y;
 }
Exemplo n.º 7
0
 public void moveTile(Tile tile,GamePostion cell)
 {
     //		Debug.Log("moveTile:" + cell.ToString());
     grid.cells[tile.x,tile.y] = null;
     Tile o = grid.cells[tile.x,tile.y];
     //		Debug.Log("moveTile null :"+o == null);
     grid.cells[cell.x,cell.y] = tile;
     //		Debug.Log("moveTile hou :"+grid.cells[cell.x,cell.y].ToString());
     //		Debug.Log("before up:"+tile.ToString());
     tile.updatePostion(cell);
     //		Debug.Log("after up:"+tile.ToString());
 }
Exemplo n.º 8
0
    public void move(MyDirection direction)
    {
        //		Debug.Log("移动");
        GamePostion vector = getVector(direction);
        Hashtable traversals = buildTraversals(vector);
        prepareTiles();
        bool moved = false;
        Tile tile;
        List<int> xList = (List<int>)traversals["x"];
        List<int> yList = (List<int>)traversals["y"];
        for(int i=0;i<size;i++){
            int x = xList[i];
            for(int j=0;j<size;j++){
                int y = yList[j];
                GamePostion cell = new GamePostion(x,y);
                tile = grid.cellContent(cell);

                if(tile != null){
                    Hashtable postions = findFarthestPosition(cell,vector);
                    Tile next = grid.cellContent((GamePostion)postions["next"]);
                    if(next !=null && next.value == tile.value && next.mergedFrom == null){

        //						Debug.Log("找到合并的tile :"+tile.ToString());
        //						Debug.Log("找到合并的next :"+next.ToString());
                        Tile merged = new Tile((GamePostion)postions["next"],tile.value*2);
                        merged.mergedFrom = new Tile[]{tile,next};

                        grid.insertTile(merged);
                        grid.removeTile(tile);

                        tile.updatePostion((GamePostion)postions["next"]);

                        score += merged.value;

                        if(merged.value == 2048){
                            //TODO  yingle
                        }

                    }else{
                        moveTile(tile,(GamePostion)postions["farthest"]);
                    }
                    if(!positionsEqual(cell,new GamePostion(tile.x,tile.y))){
                        moved = true;
                    }

                }

            }
        }

        if(moved){
            addRandomTile();
            if(!movesAvailable()){
        //				Debug.Log("GameOver");
                this.over = true;
            }
        //			actuate();
        //			Debug.Log("move le");
        }
        actuate(moved);
        Debug.Log("移动结束");
    }
Exemplo n.º 9
0
 public GamePostion getVector(MyDirection direction)
 {
     GamePostion result;
     switch(direction){
         case MyDirection.DirectionUp:
             result = new GamePostion(0,-1);
             break;
         case MyDirection.DirectionRight:
             result = new GamePostion(1,0);
             break;
         case MyDirection.DirectionDown:
             result = new GamePostion(0,1);
             break;
         case MyDirection.DirectionLeft:
             result = new GamePostion(-1,0);
             break;
         default:
             result = new GamePostion(0,0);
             break;
     }
     return result;
 }
Exemplo n.º 10
0
 public bool cellOccupied(GamePostion postion)
 {
     //Debug.Log("cellOccupied x:"+postion.x + "   y:"+postion.y+"   bool:"+(this.cellContent(postion) != null));
     return this.cellContent(postion) != null;
 }
Exemplo n.º 11
0
 public bool cellAvailable(GamePostion position)
 {
     return !this.cellOccupied(position);
 }
Exemplo n.º 12
0
 public bool withinBounds(GamePostion position)
 {
     //		Debug.Log("withinBounds  x:"+position.x + "   y:"+position.y+"    "+(position.x >= 0 && position.x < this.size && position.y >= 0 && position.y < this.size));
     return position.x >= 0 && position.x < this.size && position.y >= 0 && position.y < this.size;
 }
Exemplo n.º 13
0
 public void updatePostion(GamePostion position)
 {
     //		Debug.Log("updatePostion:" + position.ToString() + "  yuan x:"+this.x+"    y:"+this.y);
     this.x = position.x;
     this.y = position.y;
 }
Exemplo n.º 14
0
 public Tile(GamePostion postion,int value)
 {
     this.x = postion.x;
     this.y = postion.y;
     this.value = value;
 }