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
|
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
const int MAX = 8;
#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
void mossa(int&, int&, int[], int[]);
int main()
{
int scacchiera[MAX][MAX] = { 0 };
int horizontal[MAX] = { 2, 1, -1, -2, -2, -1, 1, 2 };//x
int vertical[MAX] = { -1, -2, -2, -1, 1, 2, 2, 1 };//y
int currentRow = 3;
int currentColumn = 4;
int moveNumber;
int contaRis = 0;
int sicurezza = 0;
srand(time(0));
moveNumber = (rand() % 7);
for (size_t i = 0; i < 64; i++)
{
if (scacchiera[currentRow][currentColumn] != 1)
{
scacchiera[currentRow][currentColumn] = 1;
mossa(currentRow, currentColumn, horizontal, vertical);
}
else
mossa(currentRow, currentColumn, horizontal, vertical);
}
for (size_t i = 0; i < MAX; i++)
{
for (size_t j = 0; j < MAX; j++)
{
if (scacchiera[i][j]==1)
{
contaRis++;
}
}
}
cout << contaRis << endl;
return 0;
}
void mossa(int ¤tRow, int ¤tColumn, int horizontal[], int vertical[])
{
int sicurezza = 0;
int moveNumber = (rand() % 7);
while (((currentRow + vertical[moveNumber] > 7 || currentRow + vertical[moveNumber] < 0) || (currentColumn + vertical[moveNumber] > 7 || currentColumn + vertical[moveNumber] < 0)) && sicurezza <= 1000)
{
sicurezza++;
moveNumber = (rand() % 7);
}
currentRow += vertical[moveNumber];
currentColumn += horizontal[moveNumber];
}
|