Im taking AP Comp Sci, and I got passed this assignment where I have to make a knight move around a chessboard from any given starting point and get as far as it could. I did that, but then I got to thinking, how would I program it to get to all the squares on the board. One way I came up with that I was pretty sure would work, isnt working correctly, maybe somebody can see something wrong with my code
#include<iostream.h>
#include<apmatrix.h>
void PrintMatrix(apmatrix<int>grid);//displays the matrix on the screen
void StartingPoint();//gathers the starting position from the user
void TraceMove(apmatrix<int>board,int row,int col);//defines the recursion algorithem
const int MAXROW=8,MAXCOL=8;//sets the dimension for the board
int counter=0;//starts the counter of the current square position
int x,y;//initializes x and y variables (current location of knight)
int main(){
apmatrix<int>board(9,9,0);//initializes a 9x9 matrix to 0 values
StartingPoint();
TraceMove(board,x,y);
return(0);
}
void StartingPoint(){
cout<<"Please enter starting row (1-"<<MAXROW<<"): ";
cin>>x;
cout<<"Please enter starting column (1-"<<MAXCOL<<"): ";
cin>>y;
}
void PrintMatrix(apmatrix<int>grid){
for(int row=1;row<=MAXROW;row++){
for(int col=1;col<=MAXCOL;col++){
if(grid[row][col]<=9)
cout<<"0"<<grid[row][col]<<" ";
else
cout<<grid[row][col]<<" ";}
cout<<endl;}
}
void TraceMove(apmatrix<int>board,int row,int col){
if(row>=1 && row<=MAXROW && col>=1 && col<=MAXCOL){//boundary check
if(board[row][col]==0){//check to see if its been used
board[row][col]=++counter;
if(counter==64)
PrintMatrix(board);
else{
TraceMove(board,row+1,col-2);
TraceMove(board,row+2,col-1);
TraceMove(board,row+2,col+1);
TraceMove(board,row+1,col+2);
TraceMove(board,row-1,col+2);
TraceMove(board,row-2,col+1);
TraceMove(board,row-2,col-1);
TraceMove(board,row-1,col-2);
}
}
}
}
#include<iostream.h>
#include<apmatrix.h>
void PrintMatrix(apmatrix<int>grid);//displays the matrix on the screen
void StartingPoint();//gathers the starting position from the user
void TraceMove(apmatrix<int>board,int row,int col);//defines the recursion algorithem
const int MAXROW=8,MAXCOL=8;//sets the dimension for the board
int counter=0;//starts the counter of the current square position
int x,y;//initializes x and y variables (current location of knight)
int main(){
apmatrix<int>board(9,9,0);//initializes a 9x9 matrix to 0 values
StartingPoint();
TraceMove(board,x,y);
return(0);
}
void StartingPoint(){
cout<<"Please enter starting row (1-"<<MAXROW<<"): ";
cin>>x;
cout<<"Please enter starting column (1-"<<MAXCOL<<"): ";
cin>>y;
}
void PrintMatrix(apmatrix<int>grid){
for(int row=1;row<=MAXROW;row++){
for(int col=1;col<=MAXCOL;col++){
if(grid[row][col]<=9)
cout<<"0"<<grid[row][col]<<" ";
else
cout<<grid[row][col]<<" ";}
cout<<endl;}
}
void TraceMove(apmatrix<int>board,int row,int col){
if(row>=1 && row<=MAXROW && col>=1 && col<=MAXCOL){//boundary check
if(board[row][col]==0){//check to see if its been used
board[row][col]=++counter;
if(counter==64)
PrintMatrix(board);
else{
TraceMove(board,row+1,col-2);
TraceMove(board,row+2,col-1);
TraceMove(board,row+2,col+1);
TraceMove(board,row+1,col+2);
TraceMove(board,row-1,col+2);
TraceMove(board,row-2,col+1);
TraceMove(board,row-2,col-1);
TraceMove(board,row-1,col-2);
}
}
}
}