protected override void checkSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { Dictionary <string, int> containers = grid.findContainers(squareNum); row = grid.rows[containers["Row"]]; block = grid.blocks[containers["Block"]]; col = grid.columns[containers["Column"]]; duplicates = new List <char>(); for (int i = 0; i < row.Squares.Count; i++) { if (puzzle.squares[row.Squares[i]].number != '-') { if (!duplicates.Contains(puzzle.squares[row.Squares[i]].number)) { duplicates.Add(puzzle.squares[row.Squares[i]].number); } } if (puzzle.squares[col.Squares[i]].number != '-') { if (!duplicates.Contains(puzzle.squares[col.Squares[i]].number)) { duplicates.Add(puzzle.squares[col.Squares[i]].number); } } if (puzzle.squares[block.Squares[i]].number != '-') { if (!duplicates.Contains(puzzle.squares[block.Squares[i]].number)) { duplicates.Add(puzzle.squares[block.Squares[i]].number); } } } }
protected override void checkSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { used = false; Dictionary <string, int> containers = grid.findContainers(squareNum); row = grid.rows[containers["Row"]]; block = grid.blocks[containers["Block"]]; col = grid.columns[containers["Column"]]; blockCol = new Dictionary <int, List <string> >(); for (int i = 0; i < Convert.ToInt32(Math.Sqrt(block.Squares.Count)); i++) { blockCol.Add(i, new List <string>()); } int currentCol = 0; for (int i = 0; i < block.Squares.Count; i++) { if (puzzle.squares[block.Squares[i]].number == '-') { addBlockColCandidates(block.Squares[i], currentCol, puzzle); } currentCol++; if (currentCol == 3) { currentCol = 0; } } List <string> temp = new List <string>(); for (int i = 0; i < blockCol.Count; i++) { List <string> temp2 = new List <string>(); for (int j = 0; j < blockCol[i].Count; j++) { if (!temp2.Contains(blockCol[i][j])) { temp2.Add(blockCol[i][j]); } } for (int j = 0; j < blockCol.Count; j++) { if (j != i) { for (int k = 0; k < blockCol[j].Count; k++) { if (temp2.Contains(blockCol[j][k])) { temp2.Remove(blockCol[j][k]); } } } } temp = temp.Concat(temp2).ToList <string>(); } for (int i = 0; i < blockCol.Count; i++) { blockCol[i] = blockCol[i].Intersect(temp).ToList <string>(); } }
protected override void checkSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { skip = true; if (puzzle.squares[squareNum].number == '-' && puzzle.squares[squareNum].candidates.Count == 2) { if (squareNum == 22) { Console.Write(""); } rowPossible = new List <int>(); colPossible = new List <int>(); blockPossible = new List <int>(); skip = false; Dictionary <string, int> containers = grid.findContainers(squareNum); row = grid.rows[containers["Row"]]; block = grid.blocks[containers["Block"]]; col = grid.columns[containers["Column"]]; List <string> compare = new List <string>(puzzle.squares[squareNum].candidates); for (int i = 0; i < row.Squares.Count; i++) { if (puzzle.squares[row.Squares[i]].number == '-' && puzzle.squares[row.Squares[i]].candidates.SequenceEqual(compare) && row.Squares[i] != squareNum) { rowPossible.Add(row.Squares[i]); } if (puzzle.squares[col.Squares[i]].number == '-' && puzzle.squares[col.Squares[i]].candidates.SequenceEqual(compare) && col.Squares[i] != squareNum) { colPossible.Add(col.Squares[i]); } if (puzzle.squares[block.Squares[i]].number == '-' && puzzle.squares[block.Squares[i]].candidates.SequenceEqual(compare) && block.Squares[i] != squareNum) { blockPossible.Add(block.Squares[i]); } } } }
protected override bool updateSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { bool used = false; if (candidateCount.Count == 1) { used = true; char answer = ' '; foreach (KeyValuePair <char, int> pair in candidateCount) { answer = pair.Key; } puzzle.squares[squareNum].number = answer; for (int i = 0; i < row.Squares.Count; i++) { if (puzzle.squares[row.Squares[i]].number == '-' && puzzle.squares[row.Squares[i]].candidates.Contains(answer.ToString())) { puzzle.squares[row.Squares[i]].candidates.Remove(answer.ToString()); if (puzzle.squares[row.Squares[i]].candidates.Count == 1) { SingleCandidate temp = new SingleCandidate(); temp.Solve(row.Squares[i], puzzle, grid); SinglesUsed++; SinglesUsed += temp.SinglesUsed; } } } for (int i = 0; i < col.Squares.Count; i++) { if (puzzle.squares[col.Squares[i]].number == '-' && puzzle.squares[col.Squares[i]].candidates.Contains(answer.ToString())) { puzzle.squares[col.Squares[i]].candidates.Remove(answer.ToString()); if (puzzle.squares[col.Squares[i]].candidates.Count == 1) { SingleCandidate temp = new SingleCandidate(); temp.Solve(row.Squares[i], puzzle, grid); SinglesUsed++; SinglesUsed += temp.SinglesUsed; } } } for (int i = 0; i < block.Squares.Count; i++) { if (puzzle.squares[block.Squares[i]].number == '-' && puzzle.squares[block.Squares[i]].candidates.Contains(answer.ToString())) { puzzle.squares[block.Squares[i]].candidates.Remove(answer.ToString()); if (puzzle.squares[block.Squares[i]].candidates.Count == 1) { SingleCandidate temp = new SingleCandidate(); temp.Solve(row.Squares[i], puzzle, grid); SinglesUsed++; SinglesUsed += temp.SinglesUsed; } } } } return(used); }
protected override void updateCandidates(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { used = false; if (!skip) { if (blockPossible.Count == 1) { for (int i = 0; i < block.Squares.Count; i++) { if (puzzle.squares[block.Squares[i]].number == '-' && block.Squares[i] != squareNum && !blockPossible.Contains(block.Squares[i])) { for (int j = 0; j < 2; j++) { if (puzzle.squares[block.Squares[i]].candidates.Contains(puzzle.squares[squareNum].candidates[j])) { puzzle.squares[block.Squares[i]].candidates.Remove(puzzle.squares[squareNum].candidates[j]); used = true; } } } } } if (colPossible.Count == 1) { for (int i = 0; i < col.Squares.Count; i++) { if (puzzle.squares[col.Squares[i]].number == '-' && col.Squares[i] != squareNum && !colPossible.Contains(col.Squares[i])) { for (int j = 0; j < 2; j++) { if (puzzle.squares[col.Squares[i]].candidates.Contains(puzzle.squares[squareNum].candidates[j])) { puzzle.squares[col.Squares[i]].candidates.Remove(puzzle.squares[squareNum].candidates[j]); used = true; } } } } } if (rowPossible.Count == 1) { for (int i = 0; i < row.Squares.Count; i++) { if (puzzle.squares[row.Squares[i]].number == '-' && row.Squares[i] != squareNum && !rowPossible.Contains(row.Squares[i])) { for (int j = 0; j < 2; j++) { if (puzzle.squares[row.Squares[i]].candidates.Contains(puzzle.squares[squareNum].candidates[j])) { puzzle.squares[row.Squares[i]].candidates.Remove(puzzle.squares[squareNum].candidates[j]); used = true; } } } } } } }
protected override void checkSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { Dictionary <string, int> containers = grid.findContainers(squareNum); row = grid.rows[containers["Row"]]; block = grid.blocks[containers["Block"]]; col = grid.columns[containers["Column"]]; blockCandidates = new Dictionary <char, int>(); rowCandidates = new Dictionary <char, int>(); colCandidates = new Dictionary <char, int>(); for (int i = 0; i < row.Squares.Count; i++) { if (puzzle.squares[row.Squares[i]].number == '-' && row.Squares[i] != squareNum) { for (int j = 0; j < puzzle.squares[row.Squares[i]].candidates.Count; j++) { if (!rowCandidates.ContainsKey(puzzle.squares[row.Squares[i]].candidates[j].ToCharArray()[0])) { rowCandidates.Add(puzzle.squares[row.Squares[i]].candidates[j].ToCharArray()[0], 1); } else { rowCandidates[puzzle.squares[row.Squares[i]].candidates[j].ToCharArray()[0]]++; } } } if (puzzle.squares[col.Squares[i]].number == '-' && col.Squares[i] != squareNum) { for (int j = 0; j < puzzle.squares[col.Squares[i]].candidates.Count; j++) { if (!colCandidates.ContainsKey(puzzle.squares[col.Squares[i]].candidates[j].ToCharArray()[0])) { colCandidates.Add(puzzle.squares[col.Squares[i]].candidates[j].ToCharArray()[0], 1); } else { colCandidates[puzzle.squares[col.Squares[i]].candidates[j].ToCharArray()[0]]++; } } } if (puzzle.squares[block.Squares[i]].number == '-' && block.Squares[i] != squareNum) { for (int j = 0; j < puzzle.squares[block.Squares[i]].candidates.Count; j++) { if (!blockCandidates.ContainsKey(puzzle.squares[block.Squares[i]].candidates[j].ToCharArray()[0])) { blockCandidates.Add(puzzle.squares[block.Squares[i]].candidates[j].ToCharArray()[0], 1); } else { blockCandidates[puzzle.squares[block.Squares[i]].candidates[j].ToCharArray()[0]]++; } } } } }
public void addBlockRowCandidates(int square, int row, PuzzleNumbers puzzle) { for (int i = 0; i < puzzle.squares[square].candidates.Count; i++) { if (!blockRow[row].Contains(puzzle.squares[square].candidates[i])) { blockRow[row].Add(puzzle.squares[square].candidates[i]); } } }
public void AddColBlockCandidates(int square, int block, PuzzleNumbers puzzle) { for (int i = 0; i < puzzle.squares[square].candidates.Count; i++) { if (!colBlock[block].Contains(puzzle.squares[square].candidates[i])) { colBlock[block].Add(puzzle.squares[square].candidates[i]); } } }
private void AddRowBlockCandidates(int square, int block, PuzzleNumbers puzzle) { for (int i = 0; i < puzzle.squares[square].candidates.Count; i++) { if (!rowBlock[block].Contains(puzzle.squares[square].candidates[i])) { rowBlock[block].Add(puzzle.squares[square].candidates[i]); } } }
protected override void updateCandidates(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { if (puzzle.squares[squareNum].number == '-') { for (int i = 0; i < duplicates.Count; i++) { string candidate = duplicates[i].ToString(); puzzle.squares[squareNum].candidates.Remove(candidate); } } }
protected override void updateCandidates(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { candidateCount = new Dictionary <char, int>(); for (int i = 0; i < puzzle.squares[squareNum].candidates.Count; i++) { candidateCount.Add(puzzle.squares[squareNum].candidates[i].ToCharArray()[0], 0); } Dictionary <char, int> tempCount = new Dictionary <char, int>(candidateCount); foreach (KeyValuePair <char, int> pair in rowCandidates) { if (tempCount.ContainsKey(pair.Key) && tempCount[pair.Key] == 0) { tempCount.Remove(pair.Key); } } if (tempCount.Count == 1) { candidateCount = tempCount; return; } tempCount = new Dictionary <char, int>(candidateCount); foreach (KeyValuePair <char, int> pair in colCandidates) { if (tempCount.ContainsKey(pair.Key) && tempCount[pair.Key] == 0) { tempCount.Remove(pair.Key); } } if (tempCount.Count == 1) { candidateCount = tempCount; return; } tempCount = new Dictionary <char, int>(candidateCount); foreach (KeyValuePair <char, int> pair in blockCandidates) { if (tempCount.ContainsKey(pair.Key) && tempCount[pair.Key] == 0) { tempCount.Remove(pair.Key); } } if (tempCount.Count == 1) { candidateCount = tempCount; return; } }
protected override bool updateSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { bool used = false; char solution; if (puzzle.squares[squareNum].number == '-' && puzzle.squares[squareNum].candidates.Count == 1) { solution = (puzzle.squares[squareNum].candidates[0].ToCharArray())[0]; puzzle.squares[squareNum].number = solution; used = true; for (int j = 0; j < row.Squares.Count; j++) { if (puzzle.squares[row.Squares[j]].number == '-') { puzzle.squares[row.Squares[j]].candidates.Remove(solution.ToString()); if (puzzle.squares[row.Squares[j]].candidates.Count == 1 && puzzle.squares[row.Squares[j]].number == '-') { SingleCandidate temp = new SingleCandidate(); temp.Solve(row.Squares[j], puzzle, grid); SinglesUsed++; SinglesUsed += temp.SinglesUsed; } } if (puzzle.squares[block.Squares[j]].number == '-' && puzzle.squares[block.Squares[j]].candidates.Contains(solution.ToString())) { puzzle.squares[block.Squares[j]].candidates.Remove(solution.ToString()); if (puzzle.squares[block.Squares[j]].candidates.Count == 1 && puzzle.squares[block.Squares[j]].number == '-') { SingleCandidate temp = new SingleCandidate(); temp.Solve(row.Squares[j], puzzle, grid); SinglesUsed++; SinglesUsed += temp.SinglesUsed; } } if (puzzle.squares[col.Squares[j]].number == '-') { puzzle.squares[col.Squares[j]].candidates.Remove(solution.ToString()); if (puzzle.squares[col.Squares[j]].candidates.Count == 1 && puzzle.squares[col.Squares[j]].number == '-') { SingleCandidate temp = new SingleCandidate(); temp.Solve(row.Squares[j], puzzle, grid); SinglesUsed++; SinglesUsed += temp.SinglesUsed; } } } } return(used); }
protected override bool updateSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { if (used) { for (int i = 0; i < puzzle.squares.Count; i++) { if (puzzle.squares[i].number == '-' && puzzle.squares[i].candidates.Count == 1) { SingleCandidate temp = new SingleCandidate(); temp.Solve(i, puzzle, grid); SinglesUsed++; SinglesUsed += temp.SinglesUsed; } } } return(used); }
public PuzzleSolver(string input, string output_) { bool written = false; invalid = true; try { puzzle = new PuzzleNumbers(input); structure = new PuzzleStructure(puzzle.size); mySolution = new List <List <NumberSquare> >(); possibleSolutions = 0; currentDepth = -1; currentSolvage = new List <List <NumberSquare> >(); output = output_; } catch (Exception e) { Console.WriteLine("Invalid Puzzle"); Console.WriteLine("Input not correct"); Console.Read(); invalid = false; written = true; } InitializeDictionarys(); if (invalid) { originalPuzzle = Clone(puzzle.squares) as List <NumberSquare>; Console.WriteLine(""); invalid = Solve(); } if (invalid) { writePuzzle(); } else if (!written) { Console.WriteLine("Invalid Puzzle"); Console.WriteLine("Unsolvable"); Console.Read(); } }
protected override void updateCandidates(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { for (int i = 0; i < blockRow.Count; i++) { if (blockRow[i].Count > 0) { PuzzleRow update = grid.rows[block.Rows[i]]; for (int j = 0; j < update.Squares.Count; j++) { if (puzzle.squares[update.Squares[j]].number == '-' && !block.Squares.Contains(update.Squares[j])) { for (int k = 0; k < blockRow[i].Count; k++) { if (puzzle.squares[update.Squares[j]].candidates.Contains(blockRow[i][k])) { used = true; puzzle.squares[update.Squares[j]].candidates.Remove(blockRow[i][k]); } } } } } } }
protected override void updateCandidates(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { for (int i = 0; i < rowBlock.Count; i++) { if (rowBlock[i].Count > 0) { PuzzleBlock update = grid.blocks[row.Blocks[i]]; for (int j = 0; j < update.Squares.Count; j++) { if (puzzle.squares[update.Squares[j]].number == '-' && !row.Squares.Contains(update.Squares[j])) { for (int k = 0; k < rowBlock[i].Count; k++) { if (puzzle.squares[update.Squares[j]].candidates.Contains(rowBlock[i][k])) { puzzle.squares[update.Squares[j]].candidates.Remove(rowBlock[i][k]); used = true; } } } } } } }
protected override void updateCandidates(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { throw new NotImplementedException(); }
protected override void checkSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { throw new NotImplementedException(); }
protected override void checkSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid) { if (squareNum == 13) { } used = false; Dictionary <string, int> containers = grid.findContainers(squareNum); row = grid.rows[containers["Row"]]; block = grid.blocks[containers["Block"]]; col = grid.columns[containers["Column"]]; blockRow = new Dictionary <int, List <string> >(); for (int i = 0; i < Convert.ToInt32(Math.Sqrt(block.Squares.Count)); i++) { blockRow.Add(i, new List <string>()); } int currentRow = 0; for (int i = 0; i < block.Squares.Count; i++) { if (puzzle.squares[block.Squares[i]].number == '-') { addBlockRowCandidates(block.Squares[i], currentRow, puzzle); } if ((i + 1) % Convert.ToInt32(Math.Sqrt(block.Squares.Count)) == 0) { currentRow++; } } List <string> temp = new List <string>(); for (int i = 0; i < blockRow.Count; i++) { List <string> temp2 = new List <string>(); for (int j = 0; j < blockRow[i].Count; j++) { if (!temp2.Contains(blockRow[i][j])) { temp2.Add(blockRow[i][j]); } } for (int j = 0; j < blockRow.Count; j++) { if (j != i) { for (int k = 0; k < blockRow[j].Count; k++) { if (temp2.Contains(blockRow[j][k])) { temp2.Remove(blockRow[j][k]); } } } } temp = temp.Concat(temp2).ToList <string>(); } //You need to except off the stuff you havent excepted off of! for (int i = 0; i < blockRow.Count; i++) { blockRow[i] = blockRow[i].Intersect(temp).ToList <string>(); } }
public bool Solve(int squareNumber, PuzzleNumbers puzzle, PuzzleStructure grid) { checkSurroundings(squareNumber, puzzle, grid); updateCandidates(squareNumber, puzzle, grid); return(updateSurroundings(squareNumber, puzzle, grid)); }
protected abstract void checkSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid);
protected abstract bool updateSurroundings(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid);
protected abstract void updateCandidates(int squareNum, PuzzleNumbers puzzle, PuzzleStructure grid);