A program that will move the knight around a chessboard. The board is represente
ID: 3788310 • Letter: A
Question
A program that will move the knight around a chessboard. The board is represented by an 8-by-8 double-subscripted array board. Each of the squares is ini- Chapter 4 Ids from an array that whether the pen is curfloor with its pen up. Chapter 4 Arrays 299 tialized to zero. We describe each of the eight possible moves in terms of both their horizontal and vertical components. For example, a move of type O, consists of moving two squares horizontally to the right and one square vertically upward. Move 2 consists of moving one square horizontally to the left and two squares vertically upward. Horizontal moves to the left and vertical moves upward are indicated with negative numbers. The eight moves may be described by two single-subscripted arrays, horizontal and vertical, as follows: horizotal [0] = 2, horizontal [1] = 1, horizontal [2] = -1, horizontal [3] = -2, horizontal [4] = -2, horizontal [5] = -1, horizontal [6] = 1, horizontal [7] = 2, vertical [0] = -1, vertical [1] = -2, vertical [2] = -2, vertical [3] = -1, vertical [4] = 1, vertical [5] = 2, vertical [6] = 2, vertical [7] = 1. Let the variables currentRow and currentCoIumn indicate the row and column of the knight's current position. To make a move of type moveNumber, where moveNumber is between O and 7, your program uses the statements, currentRow += vertical [ moveNumber ]; currentCoIumn horizontal [ moveNumber ]; Keep a counter that varies from 1 to 64. Record the latest count in each square the knight moves to. Remember to test each potential move to see if the knight has already visited that square, and, of course, test every potential move to make sure that the knight does not land off the chessboard. Write a program to move the knight around the chessboard. After this please develop a accessibility to shape the move. Please do not use define 8!! Please write in C++!! Any help would be greatly appreciated!! Thank you in advance!!
Explanation / Answer
//C++ program for given problem statement.
//In this code ,it prints the latest position of square.Counting started from bottom row and left most column.
#include <iostream>
using namespace std;
int main()
{
int horizontal[]={2,1,-1,-2,-2,-1,1,2};
int vertical[]={-1,-2,-2,-1,1,2,2,1};
int currentRow=0,currentColumn=0;
int chessboard[8][8]; //2D array for maitaining whether the position already visited or not.
int n,counter=0;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
chessboard[i][j]=0;
}
cout<<"enter the initial position of knight :";
cin>>currentRow>>currentColumn;
chessboard[currentRow][currentColumn]=1;
cout<<"Initial location :"<<(8*currentRow)+currentColumn+1<<" ";
cout<<"Enter the number of moves : ";
cin>>n;
cout<<" ";
while(n--)
{
int t;cin>>t;
int r_move=currentRow-vertical[t];
int c_move=currentColumn+horizontal[t];
if(r_move>=0 && r_move<=7 && c_move<=7 && c_move>=0) //checks whether this move goes outside the chessboard
{
counter=(8*r_move)+c_move+1; //calculate location of new square
cout<<"Latest square location is : "<<counter<<" ";
// cout<<r_move<<" "<<c_move<<" ";
if(chessboard[r_move][c_move]==1) //checks whether this location previously visited or not
{
cout<<"Location previously visited ";
}
chessboard[r_move][c_move]=1;
//updating currentRow and currentColumn
currentColumn=c_move;
currentRow=r_move;
}
else
{
cout<<"Invalid move: out of chessboard ";
}
}
}
Output:
sagar@sagar-Lenovo-G580:~/codes/schegg$ g++ chessboard.cpp
sagar@sagar-Lenovo-G580:~/codes/schegg$ ./a.out
enter the initial position of knight :2 3
Initial location :20
Enter the number of moves : 2
7
Latest square location is : 14
3
Latest square location is : 26
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.