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 131
|
// John Ford
// CSC 102
// Feb 2022
// Knights Tour
#include <iostream>
#include <iomanip>
using namespace std;
void resetChessboard(int chessBoard[8][8]);
void makeMove(int chessBoard[8][8]);
int main()
{
int chessBoard[8][8];
resetChessboard(chessBoard);
makeMove(chessBoard);
}
void resetChessboard(int chessBoard[8][8])
{
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
chessBoard[i][j] = 0;
}
}
}
void makeMove(int chessBoard[8][8])
{
int counter = 2;
chessBoard[0][0] = 1;
int currentRow = 0;
int currentColumn = 0;
int horizontal[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[8] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int heuristicChessboard[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 arrayHolder[8] = { 10,10,10,10,10,10,10,10 };
int bestHeuristic = 10;
int bestIndex = 10;
int bestHorizontal = 10;
int bestVertical = 10;
int tester = 0;
int resetCounter1 = 0;
int resetCounter2 = 0;
int totalResets = 0;
// I need to take all the moves the knight can make and grade them based on the heuristic chessboard array
// I then need to make the lowest ranked move on the board
// instead of making the move right away can store the for loop j value into an array that resets after the loop is done
// then pick the best of those moves and preform that move
// possibleMoves[8] is to acceept all the j values that actually work so we can compare them later
int chessOutlineHorizontal[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
char chessOutlineVertical[8] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
for (int i = 0; i < 66; i++) {
bestHeuristic = 10;
bestIndex = 10;
bestHorizontal = 10;
bestVertical = 10;
for (int h = 0; h < 8; h++) {
arrayHolder[h] = 10;
}
for (int j = 0; j < 8; j++) {
if (currentRow + vertical[j] >= 0 && currentRow + vertical[j] <= 7) {
if (currentColumn + horizontal[j] >= 0 && currentColumn + horizontal[j] <= 7) {
if (chessBoard[currentRow + vertical[j]][currentColumn + horizontal[j]] == 0) {
arrayHolder[j] = j;
}
}
}
}
for (int h = 0; h < 8; h++) {
if (arrayHolder[h] >= 0 && arrayHolder[h] <= 8) {
if (heuristicChessboard[currentRow + vertical[h]][currentColumn + horizontal[h]] < bestHeuristic) {
bestIndex = h;
bestVertical = vertical[h];
bestHorizontal = horizontal[h];
bestHeuristic = heuristicChessboard[currentRow + vertical[h]][currentColumn + horizontal[h]];
}
}
if (h == 7 && counter != 65) {
currentRow += bestVertical;
currentColumn += bestHorizontal;
chessBoard[currentRow][currentColumn] = counter;
counter++;
if (bestHorizontal == 10 && counter != 64) {
i = 0;
counter = 0;
resetCounter1++;
totalResets++;
if (resetCounter1 > 8) {
resetCounter1 = 0;
resetCounter2++;
}
chessBoard[resetCounter1][resetCounter2] = 1;
currentColumn = resetCounter1;
currentRow = resetCounter2;
for (int z = 0; z < 8; z++) {
for (int y = 0; y < 8; y++) {
chessBoard[z][y] = 0;
}
}
}
}
}
if (i == 65) {
cout << "After " << totalResets << " tries the knight made a full tour and ended on square " << chessOutlineVertical[currentColumn] << chessOutlineHorizontal[currentRow] << endl << endl;
cout << setw(5) << " A " << "B " << "C " << "D " << "E " << "F " << "G " << "H " << endl << endl;
for (int k = 0; k < 8; k++) {
cout << chessOutlineHorizontal[k];
for (int l = 0; l < 8; l++) {
cout << setw(5) << chessBoard[k][l];
}
cout << endl << endl;
}
cout << endl;
}
}
}
|