// Determines if any particular group of cells must overlap with itself // for both the min and max case /*public void Apply(CellLine line) * { * int length = line.Length; * int minimumWidth = line.Hints.Sum() + line.Hints.Count - 1; * int remainder = length - minimumWidth; * * if (remainder >= line.Hints.Max()) * { * return; * } * * int offset = 0; * for (int group = 0; group < line.Hints.Count; group++) * { * int numberKnown = Math.Max(line.Hints[group] - remainder, 0); * int numberUnknown = line.Hints[group] - numberKnown; * offset += numberUnknown; * * for (int i = 0; i < numberKnown; i++) * { * line[offset + i].State = Cell.CellState.Filled; * } * * // Special case: hints give a complete picture of the line * // I.E. remainder = 0, there are no unknown cells * // We can fill in the rest of the 'unknowns' with Blanks. * if (remainder == 0 && group + 1 != line.Hints.Count) * { * line[offset + numberKnown].State = Cell.CellState.Blank; * } * * offset += numberKnown + 1; * } * }*/ public bool Apply(CellLine line) { var min = line.Min(); var max = line.Max(); if (min == null || max == null) { // Min or Max was given conflicting information and cannot be filled. // Can't do anything, so return now. return(false); } bool madeChanges = false; for (int i = 0; i < line.Length; i++) { var minCell = min[i]; var maxCell = max[i]; if (minCell == maxCell) { if (line[i].State != minCell.State) { madeChanges = true; } line[i].State = minCell.State; } } return(madeChanges); }
public void CellLineMaxFunctionsWithSomeKnownCells_5_3_last() { Nonogram n = new Nonogram(5, 1); CellLine l = n.Row(0); l.Hints.AddRange(new int[] { 3 }); l[4].State = Cell.CellState.Filled; var expectedStates = new Cell.CellState[5] { Cell.CellState.Blank, Cell.CellState.Blank, Cell.CellState.Filled, Cell.CellState.Filled, Cell.CellState.Filled }; var expectedFlags = new int[5] { 0, 1, 0, 0, 0 }; var max = l.Max(); Assert.IsNotNull(max); for (int i = 0; i < 5; ++i) { Assert.AreEqual(expectedStates[i], max[i].State); Assert.AreEqual(expectedFlags[i], max[i].Flag); } }
public void CellLineMaxFunctionsWithSomeBlockedCells() { Nonogram n = new Nonogram(5, 1); CellLine l = n.Row(0); l.Hints.AddRange(new int[] { 2, 1 }); l[2].State = Cell.CellState.Blank; var expectedStates = new Cell.CellState[5] { Cell.CellState.Filled, Cell.CellState.Filled, Cell.CellState.Blank, // forced blank Cell.CellState.Blank, Cell.CellState.Filled }; var expectedFlags = new int[5] { 0, 0, 0, 1, 1 }; var max = l.Max(); Assert.IsNotNull(max); for (int i = 0; i < 5; ++i) { Assert.AreEqual(expectedStates[i], max[i].State); Assert.AreEqual(expectedFlags[i], max[i].Flag); } }
public void CellLineMaxReturnsNullWhenNotPossibleToFill() { Nonogram n = new Nonogram(5, 1); CellLine l = n.Row(0); l.Hints.AddRange(new int[] { 2, 2 }); l[1].State = Cell.CellState.Blank; var max = l.Max(); Assert.IsNull(max); }
public void CellLineMaxReturnsNullWhenGivenConflictingInformation() { Nonogram n = new Nonogram(5, 1); CellLine l = n.Row(0); l.Hints.AddRange(new int[] { 3 }); l[0].State = Cell.CellState.Filled; l[4].State = Cell.CellState.Filled; var max = l.Max(); Assert.IsNull(max); }
public void CellLineMaxDoesNotModifyOriginalLine() { Nonogram n = new Nonogram(5, 1); CellLine l = n.Row(0); l.Hints.AddRange(new int[] { 1, 1 }); var max = l.Max(); for (int i = 0; i < 5; ++i) { Assert.AreNotSame(l[i], max[i]); Assert.AreEqual(Cell.CellState.Unknown, l[i].State); Assert.AreEqual(0, l[i].Flag); } }