public void Reset() { this.Score = 0; Cells = new Cell[this.ColumnCount][]; for (int i = 0; i < this.ColumnCount; ++i) { Cells[i] = new Cell[this.RowCount]; } for (int y = 0; y < this.RowCount; ++y) { for (int x = 0; x < this.ColumnCount; ++x) { Cells[x][y] = new Cell(x, y); } } }
private void MergeCells(Cell SourceCell, Cell DestinationCell) { // Assumes that an appropriate merge CAN definitely be done. if (!(SourceCell.X == DestinationCell.X ^ SourceCell.Y == DestinationCell.Y)) { throw new InvalidOperationException("Cells to be merged must share either a row or column but not both"); } if (DestinationCell.IsEmpty()) { // This is the last available empty cell so take it! DestinationCell.Value = SourceCell.Value; DestinationCell.PreviousPosition = SourceCell.PreviousPosition ?? new Coordinate(SourceCell.X, SourceCell.Y); SourceCell.Value = 0; SourceCell.PreviousPosition = null; } else { if (DestinationCell.WasMerged) { throw new InvalidOperationException("Destination cell has already been merged"); } else if (SourceCell.Value != DestinationCell.Value) { throw new InvalidOperationException("Source and destination cells must have the same value"); } // The next available cell has the same value and hasn't yet // been merged, so lets merge them! DestinationCell.Value *= 2; DestinationCell.WasMerged = true; DestinationCell.PreviousPosition = SourceCell.PreviousPosition ?? new Coordinate(SourceCell.X, SourceCell.Y); SourceCell.Value = 0; SourceCell.PreviousPosition = null; // Update the score Score += DestinationCell.Value; } }