Don’t worry; when you get it working and the results are printed out it will make more sense to you.
Don’t forget, you are only printing out the results for successful attempts, so if the mole crosses a boundary you just ignore it (break) and restart the j loop.
I put the successful attempt check at the end of the loop simply to make it easier for you to understand. You are now putting it in the correct place but you need a separate loop to print out the whole successful sequence from the beginning, then you need to break out of the j loop.
Using a function just keeps the code tidier but it’s ok to write the loop where you do your check.
Code:
if (row[j] == 3 && col[j] == 9) // successful attempt
{
successful_attempt(row, 100, col, 100, j + 1);
cnt_sa = cnt_sa + 1;
break;
}
void successful_attempt(int arg1[], int len1, int arg2[], int len2, int max)
{
cout << "*** Successful Attempt ***" << endl;
for (int j= 0; j < max; j++)
{
if (j % 13 == 0 ) // display 13 coordinates per line
cout << "(" << arg1[j] << "," << arg2[j] << ") " << endl;
else
cout << "(" << arg1[j] << "," << arg2[j] << ") ";
}
cout << endl << endl;
}
Note1: srand(time(NULL)); goes at the start of the program before the loops.