Пример #1
0
 public static Traversals FromVector(Vector v) {
     var traversals = new Traversals();
     if (v.X == 1) {
         traversals.X = traversals.X.Reverse().ToArray();
     }
     if (v.Y == 1) {
         traversals.Y = traversals.Y.Reverse().ToArray();
     }
     return traversals;
 }
Пример #2
0
 void OnGameViewSwip(object sender, SwipeEventArgs e) {
     Vector vector = null;
     switch (e.Direction) {
     case SwipDirection.Up:
         vector = new Vector(x: 0, y: -1);
         break;
     case SwipDirection.Left:
         vector = new Vector(x: -1, y: 0);
         break;
     case SwipDirection.Down:
         vector = new Vector(x: 0, y: 1);
         break;
     case SwipDirection.Right:
         vector = new Vector(x: 1, y: 0);
         break;
     }
     if (vector != null) {
         viewModel.Move(vector);
     }
 }
Пример #3
0
        private FarthestPosition FindFarthestPosition(Cell p, Vector v) {
            Cell cell = p;
            Cell prev;

            do {
                prev = cell;
                cell = new Cell { X = prev.X + v.X, Y = prev.Y + v.Y };
            }
            while (grid.WithinBounds(cell) && grid.CellAvailable(cell));

            return new FarthestPosition {
                Farthest = prev,
                Next = cell
            };
        }
Пример #4
0
        public void Move(Vector vector) {
            // if (this.isGameTerminated()) return;

            var traversals = Traversals.FromVector(vector);
            var moved = false;

            PrepareTiles();

            foreach (var x in traversals.X) {
                foreach (var y in traversals.Y) {
                    var cell = new Cell { X = x, Y = y };
                    var tile = grid.CellContent(cell);

                    if (tile != null) {
                        var positions = FindFarthestPosition(cell, vector);
                        var next = grid.CellContent(positions.Next);

                        if (next != null && next.Value == tile.Value && next.MergedFrom == null) {


                            //grid.RemoveTile(tile);
                            // Converge the two tiles' positions
                            //tile.UpdatePosition(positions.Next);
                            grid.RemoveTile(tile);
                            grid.RemoveTile(next);

                            var merged = new Tile(next.X, next.Y, tile.Value * 2);
                            merged.MergedFrom = new[] { tile, next };
                            grid.InsertTile(merged);

                            Score += merged.Value;

                            if (merged.Value == 2048) {
                                Won = true;
                            }
                        }
                        else {
                            grid.MoveTile(tile, positions.Farthest);
                        }

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

            if (moved) {
                AddRandomTile();
                if (!MovesAvailable()) {
                    Over = true; // game over;
                }
            }

            //this.actuate();
        }