private static BoardCell GetNextCell(ISet <BoardCell> possibleMoves, HashSet <BoardCell> usedCells) { BoardCell nextCell = null; var minSize = int.MaxValue; foreach (var cell in possibleMoves) { var moves = GetAllPossibleMoves(cell, usedCells); if (moves.Count > 0 && moves.Count < minSize) { minSize = moves.Count; nextCell = cell; } } if (nextCell == null && possibleMoves.Count == 1) { return(new List <BoardCell>(possibleMoves)[0]); } return(nextCell); }
private static void RunKnightsTour(BoardCell currCell) { var usedCells = new HashSet <BoardCell>(BoardCell.RowColComparer); var nextCell = currCell; var idx = 1; while (true) { board[nextCell.Row, nextCell.Col] = idx; idx++; usedCells.Add(nextCell); var possibleMoves = GetAllPossibleMoves(nextCell, usedCells); nextCell = GetNextCell(possibleMoves, usedCells); if (nextCell == null) { return; } } }
private static bool IsValidBoardCell(BoardCell cell) { return(cell.Row >= 0 && cell.Row < board.GetLength(0) && cell.Col >= 0 && cell.Col < board.GetLength(1)); }