{"record":{"id":"e189d4fcdf611dc2","repo":"TheAlgorithms/C-Sharp","slug":"board-size-must-be-positive","errorCode":null,"errorMessage":"Board size must be positive.","messagePattern":"Board size must be positive\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/KnightTour/OpenKnightTour.cs","lineNumber":57,"sourceCode":"    /// </returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when <paramref name=\"n\"/> ≤ 0, or when no tour exists / is found for the given <paramref name=\"n\"/>.\n    /// </exception>\n    /// <remarks>\n    /// <para>\n    /// This routine tries every square as a starting point. As soon as a complete tour is found,\n    /// the filled board is returned. If no tour is found, an exception is thrown.\n    /// </para>\n    /// <para>\n    /// <b>Performance:</b> Exponential in the worst case. For larger boards, consider adding\n    /// Warnsdorff’s heuristic (choose next moves with the fewest onward moves) or a hybrid approach.\n    /// </para>\n    /// </remarks>\n    public int[,] Tour(int n)\n    {\n        if (n <= 0)\n        {\n            throw new ArgumentException(\"Board size must be positive.\", nameof(n));\n        }\n\n        var board = new int[n, n];\n\n        // Try every square as a starting point.\n        for (var r = 0; r < n; r++)\n        {\n            for (var c = 0; c < n; c++)\n            {\n                board[r, c] = 1; // first step\n                if (KnightTourHelper(board, (r, c), 1))\n                {\n                    return board;\n                }\n\n                board[r, c] = 0; // backtrack and try next start\n            }\n        }","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/KnightTour/OpenKnightTour.cs#L39-L75","documentation":"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.","triggerScenarios":"Calling Tour(0), Tour(-3), or passing a board size from unparsed/default-zero config input.","commonSituations":"Missing command-line/config value defaulting to 0; integer parse failure swallowed and leaving 0; loop variable misuse in generated calls.","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."],"exampleFix":"// before\nvar n = int.Parse(args[0]); // may be <= 0\nvar board = tour.Tour(n);\n// after\nvar n = int.Parse(args[0]);\nif (n <= 0) throw new ArgumentException(\"Board size must be a positive integer.\");\nvar board = tour.Tour(n);","handlingStrategy":"validation","validationCode":"if (n <= 0) throw new ArgumentException(\"Board size must be a positive integer.\", nameof(n));","typeGuard":"static bool IsValidBoardSize(int n) => n > 0;","tryCatchPattern":"try { board = tour.Tour(n); }\ncatch (ArgumentException ex) { Console.Error.WriteLine(ex.Message); }","preventionTips":["Validate CLI/config integers before use","Reject zero/negative sizes at input parsing","Use int.TryParse with explicit failure handling"],"tags":["argument-out-of-range","knight-tour","validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}