Advertisement
Guest User

isWin (connect 4)

a guest
Feb 25th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. bool ConnectFour::isWin() {
  2. int count = 0;
  3.  
  4. for (int i = 0; i < numRows; i++) {
  5. count = 0;
  6. for (int j = 0; j < numCols - 1; j++) {
  7. if (board[i][j] == board[i][j + 1] && board[i][j] != emptyToken) {
  8. count++;
  9. }
  10. if (board[i][j] == emptyToken || board[i][j] != board[i][j + 1]) {
  11. count = 0;
  12. }
  13. if (count == 3) {
  14. winningPlayerId = currentPlayerId;
  15. cout << endl << " " << playerTokens[getWinningPlayerId()] << " Is the Winner via row " << i + 1 << "!" << endl;
  16. return true;
  17.  
  18. }
  19. }
  20. }
  21.  
  22. for (int i = 0; i < numCols; i++) {
  23. count = 0;
  24. for (int j = 0; j < numRows; j++) {
  25. if (board[j][i] == board[j + 1][i] && board[j][i] != emptyToken) {
  26. count++;
  27. }
  28. if (board[j][i] == emptyToken || board[j][i] != board[j + 1][i]) {
  29. count = 0;
  30. }
  31. if (count == 3) {
  32. winningPlayerId = currentPlayerId;
  33. cout << endl << " " << playerTokens[getWinningPlayerId()] << " Is the Winner via column " << i + 1 << "!" << endl;
  34. return true;
  35. }
  36. }
  37. }
  38.  
  39.  
  40. for (int i = numRows - 1, j = 0; j < numCols; (i > 0 ? i-- : j++)) {
  41. count = 0;
  42. for (int a = i, b = j; a < numRows; a++, b++) {
  43. if (board[a][b] == board[a + 1][b + 1] && board[a][b] != emptyToken) {
  44. count++;
  45. }
  46. if (board[a][b] != board[a + 1][b + 1]) {
  47. count = 0;
  48. }
  49. if (count == 3) {
  50. winningPlayerId = currentPlayerId;
  51. cout << " Winner via right diagonal!\n";
  52. return true;
  53. }
  54. }
  55.  
  56. }
  57.  
  58. for (int i = numRows - 1, j = numCols - 1; j > 0; (i >= 0 ? i-- : j--)) {
  59. count = 0;
  60. for (int a = i, b = j;a < numRows ; a++, b--) {
  61.  
  62. if (board[a][b] == board[a + 1][b - 1] && board[a][b] != emptyToken) {
  63. count++;
  64. }
  65. if (board[a][b] != board[a + 1][b - 1]) {
  66. count = 0;
  67. }
  68. if (count == 3) {
  69. winningPlayerId = currentPlayerId;
  70. cout << " Winner via left diagonal!\n";
  71. return true;
  72. }
  73. }
  74.  
  75. }
  76. return false;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement