1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
const int rows = 8,
columns = 8;
//Current row and column the knight is on
int currentRow = 0,
currentColumn = 0;
//Sets up the 8 move types the knight can make
int horizontal[8] = { 2, 1, -1, -2, -2, -1, 1, 2 },
vertical[8] = { -1, -2, -2, -1, 1, 2, 2, 1 };
//Sets up 8x8 chessboard and initializes to 0
int board[rows][columns] = { 0 };
//Function prototypes
void printBoard(int[][columns]);
bool onBoard(int, int);
bool notVisited(int[][8]);
int main()
{
//Sets the counter at 1
int counter = 1;
//Knight's previous row and column
int pastRow,
pastColumn;
//Starts the move number at move type 0
int moveNumber = 0;
//For loop to move the Knight around the board while the counter is below 65
for (int i = 1; i < 65; i++)
{
pastRow = currentRow;
pastColumn = currentColumn;
//Moves the Knight horizontally and vertically to create an L-shaped move
currentRow += horizontal[moveNumber];
currentColumn += vertical[moveNumber];
//Calls functions to determine if the knight is on the board and that the proposed square has not been visited
if (onBoard(currentRow, currentColumn) && notVisited(board))
{
board[currentRow][currentColumn] = counter;
counter++;
moveNumber = 1;
}
else
{
//Sets the Knight back to its previous position, as the proposed position is invalid
currentRow = pastRow;
currentColumn = pastColumn;
//Increments moveNumber to try the next move
moveNumber++;
}
}
//Displays board
cout << counter << "\n";
cout << endl;
printBoard(board);
cin.get();
cin.get();
return 0;
}
//Function to display the board after the Knight cannot move anymore
void printBoard(int board[rows][columns])
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
cout << board[i][j] << ' ';
cout << endl;
}
}
//Function to check if knight's proposed move is on the board
bool onBoard(int currentRow, int currentColumn)
{
if ((currentRow > 0) && (currentRow < 7) &&
(currentColumn > 0) && (currentColumn < 7))
{
return true;
}
else
{
return false;
}
}
//Function to check if the knight's proposed move is to a square that has not been visited
bool notVisited(int board[][8])
{
if ((board[currentRow][currentColumn] == 0))
{
return true;
}
else
{
return false;
}
}
/*
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 searchArray(int target, int arraySize, int array[])
{
int foundIndex = -1;
for (int i = 0; i < arraySize; i++)
{
if (array[i] == target)
{
foundIndex = i;
break;
}
}
}
*/
|