# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# C++The simplest way to print a knight's closed tour

#### SimonFudaxian

##### New Coder
Knight's Closed Tour, simplest way to get in C++
(Source: https://codingbigdataalgorithm.com/)
int mIndex[336], mStart[64], mEnd[64], mRate[64];
string mNum[64];
void GetTour2()
{
int k1, k2, n1, n2, aCount = -1,aMove, aStart, aEnd, aMin;
string x1, aTour,aNum[70];
char c1[64];
aMove = 0;
aTour = mNum[aMove];
for (k2 = 2; k2 <= 64; k2++)
{
mRate[aMove] = 99;
aStart = mStart[aMove]; aEnd = mEnd[aMove];
aMin = 99;
for (k1 = aStart; k1 <= aEnd; k1++)
{
n1 = mIndex[k1]; mRate[n1] = mRate[n1] - 1;
if (aMin > mRate[n1])
{
aMin = mRate[n1]; aMove = n1;
}
}
aTour = aTour + mNum[aMove];
}
cout << "Representaion of a Knight's Closed Tour:" << endl;
cout << aTour << endl;

for (k2 = 0; k2 <= 6; k2++)
{
x1 = mNum[k2];
if (k2 == 0) x1 = " ";
for (k1 = 0; k1 <= 9; k1++)
{
aCount = aCount + 1;
aNum[aCount] = x1 + mNum[k1];
}
}

aTour.copy(c1,64);
string aShow[64];
for (k1 = 0; k1 <= 63; k1++)
{
n1 = int(c1[k1]) - 48;
aShow[n1] = aNum[k1+1];
}
cout << endl;
aCount = -1;
for (k2 = 1; k2 <= 8; k2++)
{
for (k1 = 1; k1 <= 8; k1++)
{
aCount = aCount + 1;
cout << aShow[aCount] + " ";
}
cout << endl << endl;
}
}

void SetUp1()
{
int k1, k2, n1, n2, n3, aCount = -1, aFilter[64], bFilter[120];
int aStep[] = {12,-21,-19,21,-12,-8,19,8};
string x1, x2;
char c1[64];
for (k1 = 0; k1 <= 63; k1++)
mNum[k1] = char(k1+48);
for (k1 = 0; k1 <= 119; k1++)
bFilter[k1] = 64;
for (k2 = 21; k2 <= 91; k2+= 10)
{
for (k1 = k2; k1<= k2 + 7; k1++)
{
aCount = aCount + 1;
aFilter[aCount] = k1;
bFilter[k1] = aCount;
}
}
aCount = -1;
for (k2 = 0; k2<= 63; k2++)
{
n1 = aFilter[k2];
mStart[k2] = aCount + 1;
for (k1 = 0; k1<= 7; k1++)
{
n2 = n1 + aStep[k1];
n3 = bFilter[n2];
if (n3 != 64)
{
aCount = aCount + 1;
mIndex[aCount] = n3;
}
}
mEnd[k2] = aCount;
}
x1 = "2344443234666643468888644688886446888864468888643466664323444432";
x1.copy(c1,64);
for (k1 = 0; k1<= 63;k1++)
mRate[k1] = int(c1[k1]) - 48;
}

In the year 2032 the world chess federation introduced donut chess, played on a 24 by 24 board, with a 8 by 8 hole in the middle.

How simple would it be to modify your simplest program to accommodate this simple change?

If you want a convincing demonstration of the value of good naming, formatting, and commenting…. This is it.

All kidding aside.

A useful program has routines that can be used by other programs, which means generalising the problem.

A knight's tour can be generalised into, just a tour.

Any movement rules, any shaped board.

The program as it stands now is a one trick pony.

Just my opinion.

I agree. Generalise and re-use.

As always, the trick is to define the API well enough that potential users will prefer to master the API over coding their own solution.
That starts with thinking about how general it should be, for example…
The board could have any geometry - each cell just has a list of ‘adjacent’ cells; that’s ok, but how do you define valid moves?​
The board has a regular geometry - rectangular, hexagonal, >2 dimensions; now we’re talking simple arrays, and valid moves are simply increments in each axis.​
The board is a 2-d rectangular grid; ‘nuff said​
The board has simple bounds - fixed lower and upper limits in each dimension/axis​
The board had arbitrary bounds, eg donut chess​

Personally I think it could be fun to look at the n-dimensional regular geometry case (rectangular and hexagonal?) which allows for a simple definition of valid moves.
You could define upper/lower bounds in each axis, but donut chess etc may be easier by injecting a function that returns onboard/off board Boolean for any set of coordinates?

Anyone interested?