Esempio n. 1
0
        private bool TileMatchesAvailable()
        {
            for (var x = 0; x < BinaryGrid.Width; x++)
            {
                for (var y = 0; y < BinaryGrid.Height; y++)
                {
                    TileData tile = BinaryGrid.Tiles[x, y];
                    if (tile != null)
                    {
                        for (var direction = 0; direction < 4; direction++)
                        {
                            var vector = GetVector((MoveType)direction);
                            var pos    = new XY(x + vector.X, y + vector.Y);

                            var other = BinaryGrid.TileAt(pos);
                            if ((other != null) && (other.Value == tile.Value))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Esempio n. 2
0
        public void Move(MoveType move)
        {
            if (IsGameOver)
            {
                return;
            }

            List <int> Xs;
            List <int> Ys;
            bool       moved  = false;
            var        vector = GetVector(move);

            BuildTransversals(vector, out Xs, out Ys);

            PrepareTiles();
            foreach (var x in Xs)
            {
                foreach (var y in Ys)
                {
                    var pos  = new XY(x, y);
                    var tile = BinaryGrid.TileAt(pos);

                    if (tile != null)
                    {
                        XY fathest, nextPos;
                        FindFarthestPosition(pos, vector, out fathest, out nextPos);
                        var nextTile = BinaryGrid.TileAt(nextPos);

                        if ((nextTile != null) &&
                            (nextTile.Value == tile.Value) &&
                            (nextTile.MergedFrom == null))
                        {
                            RaiseTileRemoved(nextTile);
                            BinaryGrid.RemoveTile(nextPos);

                            tile.MergedFrom = nextTile;
                            tile.Value     *= 2;

                            Score += tile.Value;

                            BinaryGrid.MoveTile(tile, nextPos);
                            RaiseTileMoved(tile);

                            if (tile.Value >= 2048)
                            {
                                HasGameWon = true;
                                RaiseGameWon();
                            }
                            moved = true;
                        }
                        else
                        {
                            if ((pos.X != fathest.X) ||
                                (pos.Y != fathest.Y))
                            {
                                BinaryGrid.MoveTile(tile, fathest);
                                RaiseTileMoved(tile);
                                moved = true;
                            }
                        }
                    }
                }
            }

            if (moved)
            {
                Moves++;
                GameData.MoveCount++;

                AddRandomTile();

                if (!MovesAvailable())
                {
                    IsGameOver = true;
                    RaiseGameOver();
                }
            }
        }