Question
Can you figure out why I am getting the following error in the code below? Unhandled Exception: System.IndexOutOfRangeException: Index was outside the boun ds of
Can you figure out why I am getting the following error in the code below?
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the boun ds of the array. at KnightsTourC.Knight2.Tour() in C:\Users\Shannon\Documents\Visual Studio 20 15\Projects\KnightsTourC\KnightsTourC\Knight2.cs:line 66 at KnightsTourC.Knight2Test.Main(String[] args) in C:\Users\Shannon\Documents \Visual Studio 2015\Projects\KnightsTourC\KnightsTourC\Knight2.cs:line 139 Press any key to continue . . .
Visual C# // 8.22 KnightsTourC
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace KnightsTourC { public class Knight2 { Random randomNumbers = new Random();
// accessibility heuristic p 360 int[,] access = { { 2, 3, 4, 4, 4, 4, 3, 2 }, { 3, 4, 6, 6, 6, 6, 4, 3 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 4, 6, 8, 8, 8, 8, 6, 4 }, { 3, 4, 6, 6, 6, 6, 4, 3 }, { 2, 3, 4, 4, 4, 4, 3, 2 } };
int[,] board; // gameboard int accessNumber; // the current access number
//moves int[] horizontal = { 2, 1, -1, -2, -2, -1, 1, 2 }; int[] vertical = { -1, -2, -2, -1, 1, 2, 2, 1 };
// create a board and attempt a tour public void Tour() { int currentRow; // the row position on the chessboard int currentColumn; // the column position on the chessboard int moveNumber = 0; // the current move number
int testRow; // row position of next possible move int testColumn; // column position of next possible move int minRow = -1; // row position of move with minimum access int minColumn = -1; // column position of move with minimum access
board = new int[8, 8]; // gameboard
// randomize initial board position currentRow = randomNumbers.Next(8); currentColumn = randomNumbers.Next(8);
board[currentRow, currentColumn] = ++moveNumber; bool done = false;
// continue until knight can no longer move while (!done) { accessNumber = 99;
// check all possible moves until we find one that's legal for (int moveType = 0; moveType < board.GetLength(0); moveType++) { // test position of hypothetical moves testRow = currentRow + vertical[moveType]; testColumn = currentColumn + horizontal[moveType];
if (ValidMove(testRow, testColumn)) { // obtain access number if (access[testRow, testColumn] < accessNumber) { // if this is the lowest access number thus far // then set this move to be our next move accessNumber = access[testRow, testColumn];
minRow = testRow; minColumn = testColumn; }
// position access number tried --access[testRow, testColumn]; } }
// traversing done if (accessNumber == 99) // no valid moves done = true; else { //make move currentRow = minRow; currentColumn = minColumn; board[currentRow, currentColumn] = ++moveNumber; } }
Console.WriteLine($"The tour ends with {moveNumber} moves.");
if (moveNumber == 64) Console.WriteLine("This was a full tour!"); else Console.WriteLine("This was not a full tour.");
PrintTour(); }
public bool ValidMove(int row, int column) { // returns false if the move is off the chessboard, or if the knight has already // visited that position. Note: This test stops as soon as it becomes false return (row >= 0 && row < 8 && column >= 0 && column < 8 && board[row, column] == 0); }
public void PrintTour() { Console.Write(" ");
// display numbers for column for (int k = 0; k < 8; k++) Console.Write($"{k, 3}");
Console.WriteLine(" ");
for (int row = 0; row < board.GetLength(0); row++) { Console.Write($"{row, -2}");
for (int column = 0; column < board.GetLength(1); column++) Console.Write($"{board[row, column], 3}");
Console.WriteLine(); } } }
public class Knight2Test { public static void Main(string[] args) { Knight2 application = new Knight2(); application.Tour(); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started