private static RowDescriptor MakeRowDescriptorFor(Cell[] row) { var rowDescriptor = new RowDescriptor(); int cellCounter = 0; foreach (var cell in row) { if (cell.State == CellState.Filled) { cellCounter++; } else if (cell.State == CellState.Empty && cellCounter > 0) { rowDescriptor.BlockSizes.Add(cellCounter); cellCounter = 0; } } if (row[row.Length - 1].State == CellState.Filled) { rowDescriptor.BlockSizes.Add(cellCounter); } return(rowDescriptor); }
private static RowStatus CheckFilledRowStatus(IList <CellState> row, RowDescriptor rowDescriptor) { int rowIndex = 0; int blockIndex = 0; for (blockIndex = 0; blockIndex < rowDescriptor.BlockSizes.Count; blockIndex++) { int blockSize = rowDescriptor.BlockSizes[blockIndex]; //skip empty cells while (rowIndex < row.Count && row[rowIndex] == CellState.Empty) { rowIndex++; } int foundBlockSize = 0; while (rowIndex < row.Count && row[rowIndex] == CellState.Filled) { rowIndex++; foundBlockSize++; } if (foundBlockSize != blockSize) { return(RowStatus.ContainsErrors); } rowIndex++; } return(RowStatus.FilledCorrectly); }
public static RowStatus GetRowStatus(IList <CellState> row, RowDescriptor rowDescriptor) { if (row.All(cell => cell != CellState.Undefined)) { return(CheckFilledRowStatus(row, rowDescriptor)); } else { return(CheckPartialRowStatus(row, rowDescriptor)); } }
public static RowStatus GetRowStatus(IList <Cell> row, RowDescriptor rowDescriptor) { if (row.All(cell => cell.State != CellState.Undefined)) { return(CheckFilledRowStatus(row.Select(x => x.State).ToList(), rowDescriptor)); } else { return(CheckPartialRowStatus(row.Select(x => x.State).ToList(), rowDescriptor)); } }
public static Nonogram MakeNonogram(int rows, int columns) { var nonogram = new Nonogram(columns, rows); var rand = new Random(); var a = (Array)(new int[] { 5 }); foreach (var r in nonogram.Cells) { foreach (var cell in r) { cell.State = rand.NextDouble() > 0.5 ? CellState.Filled : CellState.Empty; } } var rowsList = new List <RowDescriptor>(rows); for (int i = 0; i < rows; i++) { var row = nonogram.getRow(i); RowDescriptor rowDescriptor = MakeRowDescriptorFor(row); rowsList.Add(rowDescriptor); } nonogram.RowDescriptors = rowsList; var columnList = new List <RowDescriptor>(rows); for (int i = 0; i < columns; i++) { var column = nonogram.getColumn(i); RowDescriptor rowDescriptor = MakeRowDescriptorFor(column); columnList.Add(rowDescriptor); } nonogram.ColumnDescriptors = columnList; return(nonogram); }
// checks only until first undefined is found private static RowStatus CheckPartialRowStatus(IList <CellState> row, RowDescriptor rowDescriptor) { int rowIndex = 0; int blockIndex = 0; for (blockIndex = 0; blockIndex < rowDescriptor.BlockSizes.Count; blockIndex++) { int blockSize = rowDescriptor.BlockSizes[blockIndex]; //skip empty cells while (rowIndex < row.Count && row[rowIndex] == CellState.Empty) { rowIndex++; } if (rowIndex < row.Count && row[rowIndex] == CellState.Undefined) { break; } int foundBlockSize = 0; while (rowIndex < row.Count && row[rowIndex] == CellState.Filled) { rowIndex++; foundBlockSize++; } if (foundBlockSize != blockSize) { //maybe unfinished block? if (rowIndex < row.Count && row[rowIndex] == CellState.Undefined && foundBlockSize < blockSize) { break; } return(RowStatus.ContainsErrors); } rowIndex++; } return(RowStatus.FilledPartially); }
public static IEnumerable <IEnumerable <CellState> > MakePossibleStates(int rowLength, RowDescriptor rowDescriptor) { int filledCells = rowDescriptor.BlockSizes.Sum(); var blocks = rowDescriptor.BlockSizes.Select(blockSize => Enumerable.Repeat(CellState.Filled, blockSize)); return(GeneratePermutations(blocks, rowLength - filledCells + 1)); }