TheAlgorithms/C-Sharp · error
Board size must be positive.
Error message
Board size must be positive.
What it means
This ArgumentException is the size guard at the top of OpenKnightTour.Tour (OpenKnightTour.cs:57). The knight's tour is searched on an n x n board, so a non-positive n would allocate nothing or an invalid board and no tour search is meaningful. It fires when Tour is called with n <= 0. Note the method can also throw later (same ArgumentException type) when no tour exists for small boards such as n = 2, 3, 4.
Solutions
- Pass a positive board size (n >= 1).
- Validate n > 0 at the call site and fail with a clearer user-facing message.
- Fix the config/CLI parsing that yielded 0 or a negative size.
Example fix
// before
var n = int.Parse(args[0]); // may be <= 0
var board = tour.Tour(n);
// after
var n = int.Parse(args[0]);
if (n <= 0) throw new ArgumentException("Board size must be a positive integer.");
var board = tour.Tour(n); Defensive patterns
Strategy: validation
Validate before calling
if (n <= 0) throw new ArgumentException("Board size must be a positive integer.", nameof(n)); Type guard
static bool IsValidBoardSize(int n) => n > 0;
Try / catch
try { board = tour.Tour(n); }
catch (ArgumentException ex) { Console.Error.WriteLine(ex.Message); } Prevention
- Validate CLI/config integers before use
- Reject zero/negative sizes at input parsing
- Use int.TryParse with explicit failure handling
When it happens
Trigger: Calling Tour(0), Tour(-3), or passing a board size from unparsed/default-zero config input.
Common situations: Missing command-line/config value defaulting to 0; integer parse failure swallowed and leaving 0; loop variable misuse in generated calls.
Related errors
- k must be at least 1.
- Number of colors must be positive.
- Load factor must be greater than 0
- Load factor must be less than or equal to 1
- Invalid block size
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/e189d4fcdf611dc2.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Problems/KnightTour/OpenKnightTour.cs:57
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="n"/> ≤ 0, or when no tour exists / is found for the given <paramref name="n"/>.
/// </exception>
/// <remarks>
/// <para>
/// This routine tries every square as a starting point. As soon as a complete tour is found,
/// the filled board is returned. If no tour is found, an exception is thrown.
/// </para>
/// <para>
/// <b>Performance:</b> Exponential in the worst case. For larger boards, consider adding
/// Warnsdorff’s heuristic (choose next moves with the fewest onward moves) or a hybrid approach.
/// </para>
/// </remarks>
public int[,] Tour(int n)
{
if (n <= 0)
{
throw new ArgumentException("Board size must be positive.", nameof(n));
}
var board = new int[n, n];
// Try every square as a starting point.
for (var r = 0; r < n; r++)
{
for (var c = 0; c < n; c++)
{
board[r, c] = 1; // first step
if (KnightTourHelper(board, (r, c), 1))
{
return board;
}
board[r, c] = 0; // backtrack and try next start
}
}View on GitHub (pinned to 96e2905cab)