Example #1
0
 public static NumberCell Merge(NumberCell merger, NumberCell mergee)
 {
     merger.Value   += mergee.Value;
     merger.Merged   = true;
     merger.Position = mergee.Position;
     return(merger);
 }
Example #2
0
        private static NumberCell[,] PopulateCellArrays(string[] cellLines)
        {
            var cells = new NumberCell[4, 4];

            for (int y = 0; y < rows; y++)
            {
                var values = cellLines[y].Split();
                for (int x = 0; x < columns; x++)
                {
                    var position = Tuple.Create(y, x);
                    cells[y, x] = new NumberCell(int.Parse(values[x]), position);
                }
            }
            return(cells);
        }
Example #3
0
        private static void MergeOrMove(Tuple <int, int> moveFactor)
        {
            int changeY, changeX;

            changeY = changeX = 1;
            if (moveFactor.Item1 < 0)
            {
                changeY = -1;
            }
            if (moveFactor.Item2 < 0)
            {
                changeX = -1;
            }

            // Process actions "bottom up" to produce expected merge behavior, see Sample 5
            for (int y = changeY == 1? 3 : 0; InRange(y); y -= changeY)
            {
                for (int x = changeX == 1 ? 3 : 0; InRange(x); x -= changeX)
                {
                    var cell = cells[y, x];
                    var pos  = cell.Position;
                    pos = AddPositions(pos, moveFactor);
                    var newY = pos.Item1;
                    var newX = pos.Item2;
                    if (InRange(newY) && InRange(newX))
                    {
                        var nextCell = cells[newY, newX];
                        if (cells[newY, newX].Value == 0)
                        {
                            cells[newY, newX] = cell;
                            cell.SetPosition(pos);
                            cells[y, x] = NumberCell.EmptyCell;
                            actions++;
                        }
                        else if (!(cell.Merged || nextCell.Merged) && cell.Value == nextCell.Value)
                        {
                            cell = NumberCell.Merge(cell, nextCell);
                            cells[newY, newX] = cell;
                            cell.SetPosition(pos);
                            cells[y, x] = NumberCell.EmptyCell;
                            actions++;
                        }
                    }
                }
            }
        }