Question: Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 array of squares: int board[ROW][COL]={0}; A knight may
Concepts: multi-dimension array and the Knight's Tour Problem
A chess board consists of an 8 x 8 "array" of squares:
int board[ROW][COL]={0};
A knight may move perpendicular to the edges of the board, two squares in any of the 4 directions,
then one square at right angles to the two-square move. The following lists all 8 possible moves a
knight could make from board [3][3]:
board[5][4] or
board[5][2] or
board[4][5] or
board[4][1] or
board[1][4] or
board[1][2] or
board[2][5] or
board[2][1] or
8 1
7 2
K
6 3
5 4
Write a function named LegalMove, in class knight, which accepts a ROW and COL parameter for the
knight's current position, and a move number parameter (see table above) for the proposed new position.
The method should return true if the proposed move is legal, and false if not legal. The method
should also pass reference parameters set by the method to the ROW and COL coordinates of the
destination square if the move is legal.
You will need at least the following variables:
const int ROW = 8, COL = 8, MOVES = 8;
int row;
int col;
int board[ROW][COL]; //chess board
int offsets[MOVES][2]; //knight move offsets (delta values for row, col)
int countLegalMoves; //number of legal moves from row, col
int moves[MOVES]; //legal move indices
Tour(int row=0, int col=0);
void ClearBoard();
int GetLegalMoves();
void Move(int move, int sequence); //for brute force
void ShowBoard();
A function should initialize row and col to passed values, and should initialize the offsets array
to the array differences (+/- 1 or 2) for row and col for each of the 8 moves.
GetLegalMoves() should fill the moves array with the move number (1-8) and each legal move that is
possible from row, col, and return the number of moves in the array (0-8) and store this value in
countLegalMoves.
Write some driver code in main that will place a knight at a given position, and test some possible
moves (including some off-the-board moves).
If you have time, code the ClearBoard and the ShowBoard methods as well.
It might be even more interesting to start on a full brute-force type Knight's Tour program. I say
start on, becuase there is not enough time in a class period for even a fast programmer to complete
such a program.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
