Create a chess board as a 2D array Initialize the board to zeroes (note that you
ID: 2247248 • Letter: C
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, 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.
Explanation / Answer
The programming language is not specified. I am giving C and C++ code for the question. The program sets the board size as 3, you can change it to any value by modifying #define SIZE 3 . The final position is shown with * on both sides of the number. Please don't forget to rate the answer if it helped. Thank you very much.
C Program
#include <stdio.h>
#define SIZE 3
void initialize(int board[SIZE][SIZE]);
int main()
{
int board[SIZE][SIZE];
//changes in row and col values, 8 possible combinations
int changes[8][2] = { {2, 1}, //2 down 1 right
{2, -1}, //2 down 1 left
{-1, -2}, //2 left 1 down
{1, -2}, //2 left 1 up
{-2, -1}, //2 up 1 left
{-2, 1}, //2 up 1 right
{-1, 2}, //2 right, 1 up
{1, 2}}; //2 right, 1 down
int row = 0, col = 0; //starting row and col , top left
int nextR = 0, nextC = 0;
int valid;
int move = 1;
initialize(board);
board[row][col] = 1;
while(1)
{
valid = 0;
for(int i = 0; i < 8; i++)
{
nextR = row + changes[i][0];
nextC = col + changes[i][1];
//check if the move is leading to valid position on board
if(nextR >= 0 && nextR < SIZE && nextC >= 0 && nextC < SIZE && board[nextR][nextC] == 0)
{
valid = 1;
break;
}
}
if(!valid)
break;
else
{
row = nextR;
col = nextC;
board[row][col] = ++move;
}
}
char str[7];
sprintf(str, "*%d*", move);
for(int i = 0; i < SIZE; i++)
{
printf(" ");
for(int j = 0; j < SIZE; j++)
{
if(board[i][j] != move)
printf("%-5d",board[i][j]);
else
printf("%-5s", str);
}
}
printf(" ");
}
void initialize(int board[SIZE][SIZE])
{
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
board[i][j] = 0;
}
C ++ program
#include <iostream>
#include <iomanip>
#define SIZE 3
using namespace std;
void initialize(int board[SIZE][SIZE]);
int main()
{
int board[SIZE][SIZE];
//changes in row and col values, 8 possible combinations
int changes[8][2] = { {2, 1}, //2 down 1 right
{2, -1}, //2 down 1 left
{-1, -2}, //2 left 1 down
{1, -2}, //2 left 1 up
{-2, -1}, //2 up 1 left
{-2, 1}, //2 up 1 right
{-1, 2}, //2 right, 1 up
{1, 2}}; //2 right, 1 down
int row = 0, col = 0; //starting row and col , top left
int nextR = 0, nextC = 0;
bool valid;
int move = 1;
initialize(board);
board[row][col] = 1;
while(true)
{
valid = false;
for(int i = 0; i < 8; i++)
{
nextR = row + changes[i][0];
nextC = col + changes[i][1];
//check if the move is leading to valid position on board
if(nextR >= 0 && nextR < SIZE && nextC >= 0 && nextC < SIZE && board[nextR][nextC] == 0)
{
valid = true;
break;
}
}
if(!valid)
break;
else
{
row = nextR;
col = nextC;
board[row][col] = ++move;
}
}
string fin = "*" + to_string(move) + "*";
for(int i = 0; i < SIZE; i++)
{
cout << endl;
for(int j = 0; j < SIZE; j++)
{
if(board[i][j] != move)
cout << left << setw(5) << board[i][j];
else
cout << left << setw(5) << fin;
}
}
cout << endl;
}
void initialize(int board[SIZE][SIZE])
{
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
board[i][j] = 0;
}
output
1 6 3
4 0 *8*
7 2 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.