I use this code to check the winner. lx and ly are the last move of the
player, cell2win refer to how many rows to connect in order to win.
Code:
bool Game_Win(char bTurn, int lx, int ly,int cell2win, int block)
{
int b = 0, n = 1, oTurn, tx, ty;
bool run = true;
// oTurn mean opposite turn
if (bTurn == 'X') oTurn = 'O';
else oTurn = 'X';
run = true; tx = lx; ty = ly;
do {
tx++;
if (tx >= 20) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
run = true; tx = lx; ty = ly;
do {
tx--;
if (tx < 0) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
if (n == cell2win && b < block) return true;
n = 1; b = 0;
run = true; tx = lx; ty = ly;
do {
ty++;
if (ty >= 20) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
run = true; tx = lx; ty = ly;
do {
ty--;
if (ty < 0) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
if (n == cell2win && b < block) return true;
n = 1; b = 0;
run = true; tx = lx; ty = ly;
do {
ty--;
tx--;
if (ty < 0 && tx < 0) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
run = true; tx = lx; ty = ly;
do {
ty++;
tx++;
if (ty >= 20 && tx >= 20) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
if (n == cell2win && b < block) return true;
n = 1; b = 0;
run = true; tx = lx; ty = ly;
do {
ty--;
tx++;
if (ty < 0 && tx >= 20) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
run = true; tx = lx; ty = ly;
do {
ty++;
tx--;
if (ty >= 20 && tx < 0) run = false;
if (board[ty][tx] == bTurn) {
n++;
} else if (board[ty][tx] == oTurn) {
b++;
run = false;
} else {
run = false;
}
} while(run);
if (n == cell2win && b < block) return true;
return false;
}