This is the code for the Knights Tour for the Deitel & deitel exercise.
CAN ANYONE HELP ME AND TELL ME WHY IT DOESN'T GO TO NEXT VALUES?
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>
int main()
{
// Initialise the board array
int board[8][8]={0};
int accessibility[8][8]={
{2,3,4,4,4,4,3,2},
{3,4,6,6,6,6,4,3},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{3,4,6,6,6,6,4,3},
{2,3,4,4,4,4,3,2}
};
int access[8]={0};
int access_array[1][3]={0};
int horizontal[8]={2,1,-1,-2,-2,-1,1,2};
int vertical[8]={-1,-2,-2,-1,1,2,2,1};
// Integer that holds the number of moves
int moveNumber = 0;
int move = 1;
int fail=1;
int currentRow = 0, currentColumn = 0;
int Column_Temp= 0, Row_Temp = 0;
int x=0;
// User input : Enter the values for the starting coordinates
do
{
cout << "\n Enter the starting position coordinates: ";
cin >> currentColumn >> currentRow;
board[currentRow][currentColumn] = move;
cout << "First move:" << move << endl;
cout << "Starting coordinates: " << "x=" << currentRow << " y=" << currentColumn << endl;
}
while ((currentColumn < 0) || (currentRow < 0) || (currentColumn >= 8)
|| (currentRow >= 8));
if ((currentColumn > 8 || currentRow > 8) ||
(currentColumn < 0 || currentRow < 0))
{
cout << "End of journey. Numbers invalid. Number of moves:" << move << endl;
exit(0);
}
// Move until we have 64 valid moves
while (move<=64 ) {
cout << "Entering loop1 (while<65)" << endl;
//If this is the last generated move then make move.
if (moveNumber==8)
{
fail=0;
moveNumber=0;
cout << "New move No: " << move << endl;
move++;
board[access_array[0][0]][access_array[0][1]]= move;
cout << " MOVE MADE: " << "x= " << currentRow << " y= " << currentColumn << endl;
cout << endl;
access_array[0][2]=0;
} // end if
// The move is not the last of the 8 combinations
else
{
// Calculate moves until all 8 moves are added to current coordinates
cout << "moveNumber :" << moveNumber << endl;
currentRow += vertical[moveNumber];
currentColumn += horizontal[moveNumber];
cout << "New coordinates: " << "x=" << currentRow << " y=" << currentColumn << endl;
// Test to verify that the calculated move is valid
if (currentRow<8 && currentRow>-1 && currentColumn<8 && currentColumn>-1
&& board[currentRow][currentColumn] ==0)
{
//If the access_array is empty enter the new values.
if (access_array[0][2]==0)
{
access_array[0][0]=currentRow;
access_array[0][1]=currentColumn;
access_array[0][2]=accessibility[currentRow][currentColumn];
cout << "access_array[0][2]= " << access_array[0][2] << endl;
moveNumber++;
cout << "moveNumber: " << moveNumber << endl;
} //end if
//Test if access_array has a value
else if (access_array[0][2]!=0)
{
//Test to see to the current accessibility value is smaller
//than the one just calculated. If it is replace it.
if(accessibility[currentRow][currentColumn]<access_array[0][2])
{
--accessibility[access_array[0][0]][access_array[0][1]];
access_array[0][0]=currentRow;
access_array[0][1]=currentColumn;
access_array[0][2]=accessibility[currentRow][currentColumn];
cout << "access_array[0][2]= " << access_array[0][2] << endl;
moveNumber++;
cout << "moveNumber: " << moveNumber << endl;
} //end if
//If the new availability value is larger than the existing one
//then decrement the new availability[x][y] by one and
//make new move
else if (accessibility[currentRow][currentColumn]>access_array[0][2])
{
--accessibility[currentRow][currentColumn];
moveNumber++;
} //end else
} // end else
//If the current board square is unavailable increment
//the fail move variable
else if (board[currentRow][currentColumn]!=0)
{
cout << "SQUARE IS NOT AVAILABLE" << endl;
cout << endl;
fail++;
moveNumber++;
} // end else
} // end if
//If the move is invalid proceed to generate next move
else
{
cout << "INVALID MOVE: " << currentRow << ", " << currentColumn << endl;;
cout << endl;
moveNumber++;
}//end else
} // end else
} // end while
// Draw the board
cout << "\n";
cout << "A possible solution for the problem is: " << endl;
cout << "\n";
for (int i=0; i!= 8; i++)
{
for (int j=0; j!=8; j++)
{
cout << setw(3) << board[j];
}
cout << "\n";
}
cout << "Number of moves: " << move;
return 0;
}
CAN ANYONE HELP ME AND TELL ME WHY IT DOESN'T GO TO NEXT VALUES?
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>
int main()
{
// Initialise the board array
int board[8][8]={0};
int accessibility[8][8]={
{2,3,4,4,4,4,3,2},
{3,4,6,6,6,6,4,3},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{3,4,6,6,6,6,4,3},
{2,3,4,4,4,4,3,2}
};
int access[8]={0};
int access_array[1][3]={0};
int horizontal[8]={2,1,-1,-2,-2,-1,1,2};
int vertical[8]={-1,-2,-2,-1,1,2,2,1};
// Integer that holds the number of moves
int moveNumber = 0;
int move = 1;
int fail=1;
int currentRow = 0, currentColumn = 0;
int Column_Temp= 0, Row_Temp = 0;
int x=0;
// User input : Enter the values for the starting coordinates
do
{
cout << "\n Enter the starting position coordinates: ";
cin >> currentColumn >> currentRow;
board[currentRow][currentColumn] = move;
cout << "First move:" << move << endl;
cout << "Starting coordinates: " << "x=" << currentRow << " y=" << currentColumn << endl;
}
while ((currentColumn < 0) || (currentRow < 0) || (currentColumn >= 8)
|| (currentRow >= 8));
if ((currentColumn > 8 || currentRow > 8) ||
(currentColumn < 0 || currentRow < 0))
{
cout << "End of journey. Numbers invalid. Number of moves:" << move << endl;
exit(0);
}
// Move until we have 64 valid moves
while (move<=64 ) {
cout << "Entering loop1 (while<65)" << endl;
//If this is the last generated move then make move.
if (moveNumber==8)
{
fail=0;
moveNumber=0;
cout << "New move No: " << move << endl;
move++;
board[access_array[0][0]][access_array[0][1]]= move;
cout << " MOVE MADE: " << "x= " << currentRow << " y= " << currentColumn << endl;
cout << endl;
access_array[0][2]=0;
} // end if
// The move is not the last of the 8 combinations
else
{
// Calculate moves until all 8 moves are added to current coordinates
cout << "moveNumber :" << moveNumber << endl;
currentRow += vertical[moveNumber];
currentColumn += horizontal[moveNumber];
cout << "New coordinates: " << "x=" << currentRow << " y=" << currentColumn << endl;
// Test to verify that the calculated move is valid
if (currentRow<8 && currentRow>-1 && currentColumn<8 && currentColumn>-1
&& board[currentRow][currentColumn] ==0)
{
//If the access_array is empty enter the new values.
if (access_array[0][2]==0)
{
access_array[0][0]=currentRow;
access_array[0][1]=currentColumn;
access_array[0][2]=accessibility[currentRow][currentColumn];
cout << "access_array[0][2]= " << access_array[0][2] << endl;
moveNumber++;
cout << "moveNumber: " << moveNumber << endl;
} //end if
//Test if access_array has a value
else if (access_array[0][2]!=0)
{
//Test to see to the current accessibility value is smaller
//than the one just calculated. If it is replace it.
if(accessibility[currentRow][currentColumn]<access_array[0][2])
{
--accessibility[access_array[0][0]][access_array[0][1]];
access_array[0][0]=currentRow;
access_array[0][1]=currentColumn;
access_array[0][2]=accessibility[currentRow][currentColumn];
cout << "access_array[0][2]= " << access_array[0][2] << endl;
moveNumber++;
cout << "moveNumber: " << moveNumber << endl;
} //end if
//If the new availability value is larger than the existing one
//then decrement the new availability[x][y] by one and
//make new move
else if (accessibility[currentRow][currentColumn]>access_array[0][2])
{
--accessibility[currentRow][currentColumn];
moveNumber++;
} //end else
} // end else
//If the current board square is unavailable increment
//the fail move variable
else if (board[currentRow][currentColumn]!=0)
{
cout << "SQUARE IS NOT AVAILABLE" << endl;
cout << endl;
fail++;
moveNumber++;
} // end else
} // end if
//If the move is invalid proceed to generate next move
else
{
cout << "INVALID MOVE: " << currentRow << ", " << currentColumn << endl;;
cout << endl;
moveNumber++;
}//end else
} // end else
} // end while
// Draw the board
cout << "\n";
cout << "A possible solution for the problem is: " << endl;
cout << "\n";
for (int i=0; i!= 8; i++)
{
for (int j=0; j!=8; j++)
{
cout << setw(3) << board[j];
}
cout << "\n";
}
cout << "Number of moves: " << move;
return 0;
}