Exemplo n.º 1
0
 private static bool CheckDirection(GridModel grid, Direction direction)
 {
     var vector = Vector.FromDirection(direction);
     foreach (int x in grid.TraversalsX(direction))
     {
         foreach (int y in grid.TraversalsY(direction))
         {
             var cell1 = grid[x, y];
             var cell2 = grid[x + vector.x, y + vector.y];
             if (cell1 != null && cell2 != null)
             {
                 if (cell1.IsOccupied && cell2.IsAvailable)
                 {
                     return true;
                 }
                 if (cell1.IsOccupied && cell2.IsOccupied)
                 {
                     if (cell1.Tile.Value == cell2.Tile.Value)
                         return true;
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 2
0
        public bool Move(GridModel grid, Direction direction, out int score)
        {
            score = 0;
            if (direction == Direction.None)
                return false;

            var vector = Vector.FromDirection(direction);
            var moved = false;
            var alreadyMerged = new List<TileModel>();

            // Traverse the grid in the right direction and move tiles
            foreach (int x in grid.TraversalsX(direction))
            {
                foreach (int y in grid.TraversalsY(direction))
                {
                    CellModel cell = grid[x, y];
                    if (cell.IsOccupied)
                    {
                        TileModel tile = cell.Tile;
                        CellModel nextCell;
                        CellModel farthestCell = grid.FindFarthestPosition(cell, vector, out nextCell);

                        TileModel nextTile = (nextCell == null) ? null : nextCell.Tile;

                        // Only one merger per row traversal?
                        if (nextTile != null && nextTile.Value == tile.Value && !alreadyMerged.Contains(nextTile))
                        {
                            var merged = new TileModel(tile, nextTile);
                            alreadyMerged.Add(merged);
                            tile.Position = null;
                            nextTile.Position = null;
                            merged.Position = nextCell;
                            moved = true;

                            // Update the score
                            score += (int)Math.Pow(2, merged.Value);
                        }
                        else
                        {
                            var oldPosition = tile.Position;
                            tile.Position = farthestCell;
                            if ((oldPosition.PosX != tile.Position.PosX)
                                || (oldPosition.PosY != tile.Position.PosY))
                                moved = true;
                        }
                    }
                }
            }
            return moved;
        }
Exemplo n.º 3
0
 private int CountMerges(Direction direction, GridModel grid)
 {
     var vector = Vector.FromDirection(direction);
     int merges = 0;
     foreach (int x in grid.TraversalsX(direction))
     {
         foreach (int y in grid.TraversalsY(direction))
         {
             var cell1 = grid[x, y];
             var cell2 = grid[x + vector.x, y + vector.y];
             if (cell1 != null && cell2 != null)
             {
                 if (cell1.IsOccupied && cell2.IsOccupied)
                 {
                     if (cell1.Tile.Value == cell2.Tile.Value)
                         merges++;
                 }
             }
         }
     }
     return merges;
 }