Question: C++ : This mini project builds off lab 1. This is the knight's tour. For this project, take the code from your last project. The
C++ : This mini project builds off lab 1. This is the knight's tour.
For this project, take the code from your last project.
The program will do the following:
Ask the user for a starting position.
Places the knight in that starting position and calculates all legal moves. Each move will be numbered.
The user will choose a number and the knight will move to that space and recalculate the number of legal moves.
With each move you will also give the user the option to either start a new game or exit the program.
The program ends when the user wishes.
Lab 1
#include
#include
using namespace std;
const int ROWS = 8;
const int COLS = 8;
class Board{
private:
//Initialising the board as an instance variable
string arr[ROWS][COLS];
public:
//Creating a default constructor
Board()
{
}
//The function refers to the board as an instance variable,so now the board need to be passed as a parameter
void legalMoves(int currentRow, int currentCol)
{
//Initialize array with empty spaces
for (int r = 0; r < ROWS; r++)
{
for (int c = 0; c < COLS; c++)
{
this->arr[r][c] = "[ ]";
}
}
cout << "Your current position on the board is [" << currentRow << "][" << currentCol << "] ";
this->arr[currentRow][currentCol] = "[X]";
//Show the current position on the board
cout << " 0 1 2 3 4 5 6 7 ";
for (int r = 0; r < ROWS; r++)
{
cout << r;
for (int c = 0; c < COLS; c++)
{
cout << this->arr[r][c];
}
cout << endl;
}
int row, col;
cout << "For this position legal moves are: ";
// to move up
row = currentRow - 2; col = currentCol + 1;
if (row >=0 && row < 8 && col >=0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
row = currentRow - 1; col = currentCol + 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
// to move right
row = currentRow + 1; col = currentCol + 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
row = currentRow + 2; col = currentCol + 1;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
// to move down
row = currentRow + 2; col = currentCol - 1;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
row = currentRow + 1; col = currentCol - 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
// to move left
row = currentRow - 1; col = currentCol - 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
row = currentRow - 2; col = currentCol - 1;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "board[" << row << "][" << col << "] or ";
this->arr[row][col] = "[L]";
}
// Show the board with all legal moves from the current position
// Legal moves will be indicated as 'L'
cout << " 0 1 2 3 4 5 6 7 ";
for (int r = 0; r < ROWS; r++)
{
cout << r;
for (int c = 0; c < COLS; c++)
{
cout << this->arr[r][c];
}
cout << endl;
}
}
};
int main()
{
//Creating a board of size equal to rows and cols const values
Board b;
int row, col;
cout << "Your current position on the board (input first row, then column): ";
cin >> row >> col;
//Calling the function which is now a method of the class
b.legalMoves(row, col);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
