0

Im working on a knights tour problem. And ive run into some issues. Im able to get the program to run randomly till it finds a full tour but how would i go about running it only a 100 times and then printing the average number of moves the knight made before faulting?

```
#include<iostream>
#include<iomanip>
#include<time.h>
using namespace std;
const int MAX_ROWS = 8;
const int MAX_COLUMNS = 8;
const int HorizonalMoves[] = { 2, 1,-1,-2,-2,-1, 1, 2};
const int VerticalMoves[] = {-1,-2,-2,-1, 1, 2, 2, 1};
void InitializeBoard(int Board[][MAX_COLUMNS]);
void DisplayBoard(int Board[][MAX_COLUMNS]);
int PossibleMoves(int CurrentRow, int CurrentColumn, int Board[][MAX_COLUMNS], int MoveCount);
bool IsValidKnightsMove(int CurrentRow, int CurrentColumn, int NewRow, int NewColumn, int MoveCount);
void GetMove(int& CurrentRow, int& CurrentColumn, int& MoveCount, int Board[][MAX_COLUMNS]);
void main()
{
srand((unsigned int)time(0));
int Board[MAX_ROWS][MAX_COLUMNS] = {0};
int CurrentRow = 0;
int CurrentColumn = 0;
int MoveCount = 0;
while (MoveCount < 64)
{
MoveCount = 0;
InitializeBoard(Board);
do
{
GetMove(CurrentRow, CurrentColumn, MoveCount, Board);
}
while (PossibleMoves(CurrentRow, CurrentColumn, Board, MoveCount) > 0 );
cout << MoveCount << endl;
}
}
void GetMove(int& CurrentRow, int& CurrentColumn, int& MoveCount, int Board[][MAX_COLUMNS])
{
int NewRow;
int NewColumn;
if (PossibleMoves(CurrentRow, CurrentColumn, Board, MoveCount) > 0)
{
do
{
int RandomMove = rand()%8;
NewRow = CurrentRow + VerticalMoves[RandomMove];
NewColumn = CurrentColumn + HorizonalMoves[RandomMove];
}
while(NewRow >= MAX_ROWS || NewRow < 0 ||
NewColumn >= MAX_COLUMNS || NewColumn < 0 ||
Board[NewRow][NewColumn] != 0 ||
!IsValidKnightsMove(CurrentRow, CurrentColumn, NewRow, NewColumn, MoveCount));
MoveCount++;
Board[NewRow][NewColumn] = MoveCount;
CurrentRow = NewRow;
CurrentColumn = NewColumn;
}
}
void DisplayBoard(int Board[][MAX_COLUMNS])
{
cout << " 1 2 3 4 5 6 7 8" << endl;
cout << " ------------------------" << endl;
for (int Row = 0; Row < MAX_ROWS; Row++)
{
cout << Row + 1 << "| ";
for (int Column = 0; Column < MAX_ROWS; Column++)
{
cout << setw(3) << Board[Row][Column];
}
cout << endl;
}
}
void InitializeBoard(int Board[][MAX_COLUMNS])
{
for (int Row = 0; Row < MAX_ROWS; Row++)
{
for (int Column = 0; Column < MAX_ROWS; Column++)
{
Board[Row][Column] = 0;
}
}
}
int PossibleMoves(int CurrentRow, int CurrentColumn, int Board[][MAX_COLUMNS], int MoveCount)
{
int Result = 0;
if (MoveCount == 0)
{
Result = 64;
}
else
{
for(int i = 0; i < 8; i++)
{
int Row = CurrentRow + VerticalMoves[i] + 1;
int Column = CurrentColumn + HorizonalMoves[i] + 1;
if (Row > 0 && Row <= MAX_ROWS &&
Column > 0 && Column <= MAX_COLUMNS &&
Board[Row-1][Column-1] == 0)
{
Result++;
}
}
}
return Result;
}
bool IsValidKnightsMove(int CurrentRow, int CurrentColumn, int NewRow, int NewColumn, int MoveCount)
{
bool Result = false;
if (MoveCount > 0)
{
for(int i = 0; i < 8; i++)
{
if(CurrentRow + VerticalMoves[i] == NewRow &&
CurrentColumn + HorizonalMoves[i] == NewColumn )
{
Result = true;
}
}
}
else
{
Result = true;
}
return Result;
}
```