Question: : Create a chess board as a 2D array Initialize the board to zeroes (note that you can do this at the time of declaration,
: Create a chess board as a 2D array Initialize the board to zeroes (note that you can do this at the time of declaration, but later in the assignment you will need to re-initialize the board after it has been populated with values, so you should consider writing a function to do this now) Move a "knight" around the board using only moves that are legal for a knight Ensure that the knight does not move off of the board or move to a square that has already been visited Mark each square visited with the number of the corresponding move (the starting square will be marked 1) Display the values of the squares on the board such that the user can track the knight's movements. For example, a 3x3 board might look like 1 6 3 4 0 8 7 2 5 For this part of the assignment you are not expected to complete a tour At this point your program only needs to: start from any square (my program starts at the top left) move until there are no more legal moves left display the board with the path of the moves noted as above It is helpful to mark the final move some how to make it easier to see where the tour finished. My program adds an asterisk on each side of the final move number.
I have the code made, but I am still running into some errors and do not know what I am getting wrong. Please see below.\
#include
using namespace std; int main() { int board[8][8];
int moves[8][2] = { {2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1,2}, {1,2}, {2,1} };
int row = 0; int col = 0; int newRow = 0; int newCol = 0; int mover = 1;
setToZero(board);
board[row][col] = 1;
bool possible_to_move = true;
while (possible_to_move) { possible_to_move = false; //initialized to false for (int i = 0; i < 8; i++) //to check possibility of an available move { newRow = row + moves[i][0]; // row increases newCol = col + moves[i][1]; // column increases
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && board[newRow][newCol] == 0) {
possible_to_move = true; //value becomes true if a possibility is found break;
} } if (possible_to_move) { row = newRow; col = newCol; board[row][col] = ++mover; }
}
//function for displaying the board int i, j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { cout << " " << board[i][j] << " "; } cout << " " << endl;
}
cin.get(); cin.get(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
