Skip to content

Instantly share code, notes, and snippets.

@montycheese
Created August 20, 2015 20:00
Show Gist options
  • Save montycheese/cdf7af0b795c0ca16490 to your computer and use it in GitHub Desktop.
Save montycheese/cdf7af0b795c0ca16490 to your computer and use it in GitHub Desktop.
Knights tour iterative solution
#include <iostream>
#define B_SIZE 8
using std::cout;
using std::cin;
using std::endl;
bool checkBounds(int currentRow, int currentCol, int horiz, int vert);
int move(int currentRow, int currentCol, int * newCoordinates, int step);
void recordMove(int row, int col);
void undo(int currentRow, int currentCol, int step, int * newCoordinates);
void printBoard();
void printAccessibilityBoard();
void generateAccessibilityBoard();
int board[B_SIZE][B_SIZE] = {{0,0,0,0,0,0,0,0}};
int usedSquares[B_SIZE][B_SIZE] = {{0,0,0,0,0,0,0,0}};
//records how many moves are accessible from each position
int accessibilityBoard[B_SIZE][B_SIZE] = {{0,0,0,0,0,0,0,0}};
int moves[64] = {0};
//the step that was used last on a specific move
int stepOnMoveN[64] = {0};
int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
int main(){
int status, currentRow = 3, currentCol = 3, step = 0;
unsigned long moves = 0;
int newCoordinates[2];
//compiler will init the rest of the values to 0
char i;
//start at coord 0,0
usedSquares[currentRow][currentCol] = 1;
while(step < 63){
printBoard();
//cout << "\n";
generateAccessibilityBoard();
/*printAccessibilityBoard();
cout << "Type 'n' for next move\n";
cin >> i;
if (i == 'q')
return -1;
*/
status = move(currentRow, currentCol, newCoordinates,step);
if(status == -1){
stepOnMoveN[step] = 0;
undo(currentRow, currentCol, --step, newCoordinates);
//increment to try next step
stepOnMoveN[step]++;
}
else{
step++;
}
currentRow = newCoordinates[0];
currentCol = newCoordinates[1];
moves++;
cout << "\n Moves made so far: " << moves << "\n" << endl;
//cout << step << "\n" << endl;
}
cout << "You win!\n";
printBoard();
return 0;
}
void generateAccessibilityBoard(){
int count = 0;
for(int i = 0; i < B_SIZE; i++){
for(int j = 0; j < B_SIZE; j++){
//for move in moves
for(int k = 0; k < 8; k++){
if(checkBounds(i, j, horizontal[k],vertical[k]) &&
usedSquares[i + vertical[k]][j + horizontal[k]] == 0)
{
count++;
}
}
accessibilityBoard[i][j] = count;
count = 0;
}
}
}
bool checkBounds(int currentRow, int currentCol, int horiz, int vert){
if( (currentRow + vert < 0) || (currentRow + vert > 7) ||
(currentCol + horiz < 0) || (currentCol + horiz > 7)
){
return false;
}
return true;
}
int move(int currentRow, int currentCol, int * newCoordinates, int step){
//if move is on board and the move hasn't been done, then try it.
int min = -1, i, indexOfMin = 0;
for(i = stepOnMoveN[step]; i < 8; i++){
if (checkBounds(currentRow, currentCol, horizontal[i], vertical[i]) &&
usedSquares[currentRow + vertical[i]][currentCol + horizontal[i]] == 0
){
if(min == -1){
newCoordinates[0] = currentRow + vertical[i];
newCoordinates[1] = currentCol + horizontal[i];
min = accessibilityBoard[newCoordinates[0]][newCoordinates[1]];
indexOfMin = i;
}
else if(accessibilityBoard[currentRow + vertical[i]][currentCol + horizontal[i]] < min){
newCoordinates[0] = currentRow + vertical[i];
newCoordinates[1] = currentCol + horizontal[i];
min = accessibilityBoard[newCoordinates[0]][newCoordinates[1]];
indexOfMin = i;
}
}
}
//no possible moves
if(min == -1){
return -1;
}
else{
usedSquares[newCoordinates[0]][newCoordinates[1]] = 1;
stepOnMoveN[step] = indexOfMin;
//cout << "step on move " << step << " :" << stepOnMoveN[step] << "\n" << endl;
cout << "Moved to: " << newCoordinates[0] << ", " << newCoordinates[1] << endl;
}
return 0;
}
/* undo last move */
//go back to move at step -1 and decrement step
void undo(int currentRow, int currentCol, int step, int * newCoordinates){
usedSquares[currentRow][currentCol] = 0;
newCoordinates[0] = currentRow + (-1 * vertical[stepOnMoveN[step]]);
newCoordinates[1] = currentCol + (-1 * horizontal[stepOnMoveN[step]]);
cout << "UNDO! Moved back to: " << newCoordinates[0] << ", " << newCoordinates[1] << "\nFrom: " << currentRow << ", " << currentCol << "\n" << endl;
}
void printBoard(){
for(int i=0; i< B_SIZE; i++){
for(int j = 0; j < B_SIZE; j++){
if(usedSquares[i][j] == 1)
cout << 1;
else cout << 0;
cout << " ";
}
cout << endl;
}
}
void printAccessibilityBoard(){
for(int i=0; i< B_SIZE; i++){
for(int j = 0; j < B_SIZE; j++){
cout << accessibilityBoard[i][j];
cout << " ";
}
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment