bool TryPlaceQueenOnSquare(Board board, int rank, int file) { if (board.CanIPlaceQueen(rank, file)) { board.OccupySquare(rank, file); board.MarkThreatenedSquares(rank, file); return true; } return false; }
bool TryPlaceQueenOnFile(Board board, int rank, int startingFile) { var queenIsPlaced = false; for (int file = startingFile; file < boardSize; file++) { queenIsPlaced = TryPlaceQueenOnSquare(board, rank, file); if (queenIsPlaced) { break; } } return queenIsPlaced; }
void TryPlaceQueenOnRank(Board board, ref int rank, ref int startingFile) { var queenIsPlaced = TryPlaceQueenOnFile(board, rank, startingFile); if (queenIsPlaced) { startingFile = 0; } else { startingFile = RevertLastQueenPlacement(board, ref rank); } }
int RevertLastQueenPlacement(Board board, ref int rank) { rank = MoveToPreviousRank(rank); var startingFile = RemoveQueenOnRank(board, rank); rank = MoveToPreviousRank(rank); board.ClearAllThreatenings(); board.CalculateAllThreatenings(); return startingFile; }
int RemoveQueenOnRank(Board board, int rank) { var occupiedFile = board.FindOccupiedFileOnRank(rank); board.ClearSquare(rank, occupiedFile); return MoveToNextFile(occupiedFile); }
void FindSolution(Board board) { var startingfile = 0; for (int rank = 0; rank < 8; rank++) { TryPlaceQueenOnRank(board, ref rank, ref startingfile); } }
List<Tuple<int, int>> ExtractSolution(Board board) { List<Tuple<int, int>> result = new List<Tuple<int, int>>(); board.GetAllSquares().ToList().ForEach(square => AddQueenToResult(square, result)); return result; }
public List<Tuple<int, int>> Solve() { var board = new Board(boardSize); FindSolution(board); return ExtractSolution(board); }