Good job!! As for certain starting positions not working, Warnsdorff's rule doesn't promise they will all work. However, you can fix that using backtracking, which really kicked my ass yesterday to understand how to implement in recursion...
I solved it by using a list of possible moves, with each object containing the coordinates and accessibility, sorting according to accessibility, and trying to solve the tour recursively from each object of the list.
private bool FinishTour(int x, int y, int moveCount) { if (moveCount == N * N) { return true; } List<(int x, int y, int accessibility)> moves = new List<(int, int, int)>(); int nextX; int nextY; int accessibility; for (int i = 0; i < N; i++) { nextX = x + xMove[i]; nextY = y + yMove[i]; if (IsInLimits(nextX, nextY) && IsEmpty(nextX, nextY)) { accessibility = GetAccesibility(nextX, nextY); moves.Add((nextX, nextY, accessibility)); } } moves.Sort((a, b) => a.accessibility.CompareTo(b.accessibility)); foreach (var move in moves) { board[move.x, move.y] = moveCount; if (FinishTour(move.x, move.y, moveCount + 1)) { return true; } else { board[move.x, move.y] = 0; } } return false; }
Honestly, I am still not sure this is a proper use of backtracking, but it works for now...
Let me know if you want an extra set of eyes on your code :)