示例#1
0
 internal void AddRandomTile(int?valueOverride = null)
 {
     if (grid.CellsAvailable())
     {
         int     value = valueOverride == null ? Random.value < 0.9f ? 2 : 4 : (int)valueOverride;
         DigTile tile  = new DigTile(grid.RandomAvailableCell(), value);
         grid.InsertTile(tile);
     }
 }
示例#2
0
    internal static PredictedMove CalculateMove(int size, PredictedMove previousMove, Direction direction)
    {
        int      score        = (int)previousMove.NewScore;
        Grid2048 previousGrid = new Grid2048(previousMove.Grid);

        bool moved = false;

        Coord   cell;
        DigTile tile;

        Coord      vector     = GetDirectionalOffset(direction);
        Traversals traversals = Traversals.BuildTraversals(size, vector);

        foreach (int x in traversals.x)
        {
            foreach (int y in traversals.y)
            {
                cell = new Coord(x, y);
                tile = previousGrid.CellContent(cell);

                if (tile != null)
                {
                    FarthestPosition positions = FindFarthestPosition(previousGrid, cell, vector);
                    DigTile          next      = previousGrid.CellContent(positions.Next);

                    if (next != null && next.Value == tile.Value && next.MergedFrom == null)
                    {
                        DigTile merged = new DigTile(positions.Next, tile.Value * 2)
                        {
                            MergedFrom = new List <DigTile> {
                                tile, next
                            }
                        };

                        previousGrid.InsertTile(merged);
                        previousGrid.RemoveTile(tile);

                        tile.UpdatePosition(positions.Next);

                        score += merged.Value;
                    }
                    else
                    {
                        MoveTile(previousGrid, tile, positions.Farthest);
                    }

                    if (!PositionsEqual(cell, tile))
                    {
                        moved = true;
                    }
                }
            }
        }

        return(new PredictedMove(moved ? score : -1, previousGrid, direction));
    }