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
|
#include <iostream>
using namespace std;
int showboard(int chessboard[8][8])
{
for (int x = 0; x < 8; x++)
{
cout << "\t";
for (int y = 0; y < 8; y++)
{
cout << " " <<chessboard[y][x] << " ";
}
cout << endl;
}
return 0;
}
int main (){
int chessboard[8][8];
int x=0,y=0, counter=0;
int movx [8] = { 2, 1, -1 , 2, -2, -1, 1, 2};
int movy [8] = { -1, -2, -2, -1, 1, 2, 2, 1};
int xy =1;
for (int i=0; i<8;i++)
{
for (int j=0; j<8;j++)
{
chessboard[i][j] =0;
}
}
do
{
for (int i=0; i<8;i++)
{
cout << "Counter is " <<i << " " << endl;
chessboard [x][y] =xy;
cout << "chessboard positions is " << x << " " << y << endl;
cout << " x+movx[i] = " << x+movx[i] << endl;
cout << " y+movy[i] = " << y+movy[i] << endl;
if ( ((x+movx[i])<0) || ((y+movy[i]) < 0))
{
cout << "case 1" << endl;
continue;
}
else if (((x+movx[i]) >7)|| ((y+movy[i]) >7))
{
cout << "case 2" << endl;
continue;
}
else if ((chessboard[x+movx[i]][y+movy[i]]) == 1)
{
cout << "case 3" << endl;
continue;
}
else if ((chessboard[x+movx[i]][y+movy[i]]) == 0)
{
x += movx[i];
y += movy[i];
xy++;
chessboard[x][y] = xy;
counter++;
cout <<" counter is " << counter << endl;
break;
}
else
cout <<" Unexpected error " << endl;
}
showboard ( chessboard);
system("pause");
} while (counter <64);
system("pause");
return 0;
}
|