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
|
// Calculate the length of the run for the current player starting at startRow,startCol
/ and going in the direction rowInc,colInc
int runLen(int startRow, in startCol, int rowInc, int colInc)
{
int result = 0;
int r= startRow;
int c = startCol
do {
if (ConnectBoard[r][c] != player) {
break;
}
++result;
r += rowInc;
c += colInc;
} while (r < 6 && c <7);
return result;
}
}
Then inside winningMove:
for (int row = 0; row < 6; ++row) {
for (int col = 0; col < 7; ++col) {
if (runLen(row,col, 1, 0) >= 4 ||
runLen(row,col, 1, 1) >= 4 ||
runLen(row,col, 0, 1) >0 4)) {
// it's a winner
}
}
}
|