Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool ConnectFour::isWin() {
- int count = 0;
- for (int i = 0; i < numRows; i++) {
- count = 0;
- for (int j = 0; j < numCols - 1; j++) {
- if (board[i][j] == board[i][j + 1] && board[i][j] != emptyToken) {
- count++;
- }
- if (board[i][j] == emptyToken || board[i][j] != board[i][j + 1]) {
- count = 0;
- }
- if (count == 3) {
- winningPlayerId = currentPlayerId;
- cout << endl << " " << playerTokens[getWinningPlayerId()] << " Is the Winner via row " << i + 1 << "!" << endl;
- return true;
- }
- }
- }
- for (int i = 0; i < numCols; i++) {
- count = 0;
- for (int j = 0; j < numRows; j++) {
- if (board[j][i] == board[j + 1][i] && board[j][i] != emptyToken) {
- count++;
- }
- if (board[j][i] == emptyToken || board[j][i] != board[j + 1][i]) {
- count = 0;
- }
- if (count == 3) {
- winningPlayerId = currentPlayerId;
- cout << endl << " " << playerTokens[getWinningPlayerId()] << " Is the Winner via column " << i + 1 << "!" << endl;
- return true;
- }
- }
- }
- for (int i = numRows - 1, j = 0; j < numCols; (i > 0 ? i-- : j++)) {
- count = 0;
- for (int a = i, b = j; a < numRows; a++, b++) {
- if (board[a][b] == board[a + 1][b + 1] && board[a][b] != emptyToken) {
- count++;
- }
- if (board[a][b] != board[a + 1][b + 1]) {
- count = 0;
- }
- if (count == 3) {
- winningPlayerId = currentPlayerId;
- cout << " Winner via right diagonal!\n";
- return true;
- }
- }
- }
- for (int i = numRows - 1, j = numCols - 1; j > 0; (i >= 0 ? i-- : j--)) {
- count = 0;
- for (int a = i, b = j;a < numRows ; a++, b--) {
- if (board[a][b] == board[a + 1][b - 1] && board[a][b] != emptyToken) {
- count++;
- }
- if (board[a][b] != board[a + 1][b - 1]) {
- count = 0;
- }
- if (count == 3) {
- winningPlayerId = currentPlayerId;
- cout << " Winner via left diagonal!\n";
- return true;
- }
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement