private void PrepareTiles() { BinaryGrid.ForEachTile((tile) => { tile.MergedFrom = null; tile.PreviousPos = tile.Pos; }); }
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(); } } }