Question
please make changes to this c# program so that it runs until all box are visited. Also print all the outputs until every box is
please make changes to this c# program so that it runs until all box are visited. Also print all the outputs until every box is visited. ( Simply use loops)
using System;
public class KnightTour { static Random randomNumbers = new Random(); static int[,] board;
static int[] horizontal = { 2, 1, -1, -2, -2, -1, 1, 2 }; static int[] vertical = { -1, -2, -2, -1, 1, 2, 2, 1 };
public static void Main(string[] args) { int currentRow; int currentColumn; var moveNumber =0;
board = new int[8, 8];
int testRow; int testColumn;
currentRow = randomNumbers.Next(8); currentColumn = randomNumbers.Next(8);
board[currentColumn, currentRow] = ++moveNumber;
var done = false;
while (!done) { var leagalMove = false;
for(int Movetype =0; Movetype <8 && !leagalMove; Movetype++) { testRow = currentRow + vertical[Movetype]; testColumn = currentColumn + horizontal[Movetype]; leagalMove = CheckMove(testRow, testColumn);
if (leagalMove) { currentRow = testRow; currentColumn = testColumn; board[currentRow, currentColumn] = ++moveNumber;
}
} if(!leagalMove || moveNumber == 64) { done = true;
} } Console.WriteLine($"The tour end with {moveNumber} moves"); PrintBoard();
} public static bool CheckMove(int row, int column) { if (row >= 0 && row < 8 && column >= 0 && column < 8 && board[row,column] ==0) return true; return false; } public static void PrintBoard() { for (int k = 0; k < 8; k++) Console.Write($"{k,3}"); Console.WriteLine(" "); for(int row =0; row < 8; row++) { for (int column = 0; column < 8; column++) Console.Write($"{board[row, column],3}"); Console.WriteLine(); } } }
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