I'm trying to write a knights tour prog with iterations.
Basically the knight has a starting point on the chess board and has to visit all the squares only once!
This is the starting code basically but it seems it doesn't work!
(Surprise surprise!)
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>
int main()
{
srand(time(NULL));
// Initialise the board array
int board[8][8]={0};
// Initialise the array that holds all the possible movements
int ran_move[8][2]={1,2, -1,-2, 1,-2, -1,2, 2,1, -2,-1, 2,-1, -2,1};
// Integer that holds the number of moves
int move = 0;
// Random integer
int ran;
// Integers that hold the coordinates of the moves
int x_pos, y_pos ;
int x_pos_temp, y_pos_temp;
do
{
cout << "\n Enter the starting position coordinates: ";
cin >> x_pos >> y_pos;
}
while ((x_pos < 0) || (y_pos < 0) || (x_pos >= 8) || (y_pos >= 8));
while (x_pos > 0 && y_pos > 0 && move < 63)
{
ran = (0 + rand() % 7);
x_pos = x_pos + ran_move[ran][1];
y_pos = y_pos + ran_move[ran][2];
if (x_pos > 8 || y_pos > 8)
{
cout << "End of journey. Number of moves:" << move << endl;
for (int i; i <= 8; i++){
for (int j; j <=8; j++){
cout << setw(3) << board[j];
}
}
}
else if (board[x_pos][y_pos] = 0)
{
move = move++;
board[x_pos][y_pos] = move;
}
else if (board[x_pos][y_pos] != 0)
cout << "End of journey. Number of moves:" << move << endl;
for (int i; i <= 8; i++)
for (int j; j <=8; j++)
cout << board[j];
}
return 0;
}
Basically the knight has a starting point on the chess board and has to visit all the squares only once!
This is the starting code basically but it seems it doesn't work!
(Surprise surprise!)
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>
int main()
{
srand(time(NULL));
// Initialise the board array
int board[8][8]={0};
// Initialise the array that holds all the possible movements
int ran_move[8][2]={1,2, -1,-2, 1,-2, -1,2, 2,1, -2,-1, 2,-1, -2,1};
// Integer that holds the number of moves
int move = 0;
// Random integer
int ran;
// Integers that hold the coordinates of the moves
int x_pos, y_pos ;
int x_pos_temp, y_pos_temp;
do
{
cout << "\n Enter the starting position coordinates: ";
cin >> x_pos >> y_pos;
}
while ((x_pos < 0) || (y_pos < 0) || (x_pos >= 8) || (y_pos >= 8));
while (x_pos > 0 && y_pos > 0 && move < 63)
{
ran = (0 + rand() % 7);
x_pos = x_pos + ran_move[ran][1];
y_pos = y_pos + ran_move[ran][2];
if (x_pos > 8 || y_pos > 8)
{
cout << "End of journey. Number of moves:" << move << endl;
for (int i; i <= 8; i++){
for (int j; j <=8; j++){
cout << setw(3) << board[j];
}
}
}
else if (board[x_pos][y_pos] = 0)
{
move = move++;
board[x_pos][y_pos] = move;
}
else if (board[x_pos][y_pos] != 0)
cout << "End of journey. Number of moves:" << move << endl;
for (int i; i <= 8; i++)
for (int j; j <=8; j++)
cout << board[j];
}
return 0;
}