Exemplo n.º 1
0
 public void Empty() {
     cells = new Tile[size][];
     for (var x = 0; x < size; x++) {
         Tiles[x] = new Tile[size];
     }
     OnTileChanged(EventArgs.Empty);
 }
Exemplo n.º 2
0
 public void RemoveTile(Tile tile) {
     Tiles[tile.X][tile.Y] = null;
     OnTileChanged(EventArgs.Empty);
 }
Exemplo n.º 3
0
 public void InsertTile(Tile tile) {
     Tiles[tile.X][tile.Y] = tile;
     OnTileChanged(EventArgs.Empty);
 }
Exemplo n.º 4
0
 public void MoveTile(Tile tile, Cell cell) {
     if (!tile.PositionsEqual(cell)) {
         cells[tile.X][tile.Y] = null;
         cells[cell.X][cell.Y] = tile;
         tile.UpdatePosition(cell);
     }
 }
Exemplo n.º 5
0
 private void AddRandomTile() {
     if (grid.CellsAvailable) {
         var val = random.NextDouble() < 0.9 ? 2 : 4;
         var tile = new Tile(grid.RandomAvailableCell(), val);
         grid.InsertTile(tile);
     }
 }
Exemplo n.º 6
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();
        }