Advertisement
markyrocks

c++ connect4

Apr 22nd, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1.  
  2. #include <Windows.h>
  3. #include <iostream>
  4. #include<vector>;
  5.  
  6. #define BLACK 1
  7. #define RED 255
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void display(byte board[7][6]) {
  13.     cout << " 1 | 2 | 3 | 4 | 5 | 6 | 7 |";
  14.     for (int i = 0; i < 6; i++) {
  15.         cout << "\n____________________________\n ";
  16.         for (int j = 0; j < 7; j++) {
  17.             if (!board[j][i]) { cout << " "; }
  18.             else if (board[j][i] == BLACK) { cout << "B"; }
  19.             else { cout << "R";}
  20.             cout << " | ";
  21.         }
  22.        
  23.     }
  24.     cout << '\n';
  25. }
  26.  
  27. byte checkwinner(byte board[7][6]) {
  28.  
  29.     for (int i = 0; i < 6; i++) {
  30.         for (int j = 0; j < 7; j++) {
  31.             if (board[j][i]) {
  32.                 if (j + 3 < 7) {//checks left to right
  33.                     if ((board[j][i] + board[j + 1][i] + board[j + 2][i] + board[j + 3][i]) / 4 == BLACK) {
  34.                         return BLACK;
  35.                     }
  36.                     else if ((board[j][i] + board[j + 1][i] + board[j + 2][i] + board[j + 3][i]) / 4 == RED) {
  37.                         return RED;
  38.                     }
  39.                 }
  40.                 if (i + 3 < 6) {//check from top to bottom
  41.                     if ((board[j][i] + board[j][i+1] + board[j][i+2] + board[j][i+3]) / 4 == BLACK) {
  42.                         return BLACK;
  43.                     }
  44.                     else if ((board[j][i] + board[j][i + 1] + board[j][i + 2] + board[j][i + 3]) / 4 == RED) {
  45.                         return RED;
  46.                     }
  47.                 }
  48.                 if (j + 3 < 7 && i + 3 < 6) { //diag down and right
  49.                     if ((board[j][i] + board[j+1][i + 1] + board[j+2][i + 2] + board[j+3][i + 3]) / 4 == BLACK) {
  50.                         return BLACK;
  51.                     }
  52.                     else if ((board[j][i] + board[j+1][i + 1] + board[j+2][i + 2] + board[j+3][i + 3]) / 4 == RED) {
  53.                         return RED;
  54.                     }
  55.                 }
  56.                 if (j - 3 > -1 && i + 3 < 6) {
  57.                     if ((board[j][i] + board[j - 1][i + 1] + board[j - 2][i + 2] + board[j - 3][i + 3]) / 4 == BLACK) {
  58.                         return BLACK;
  59.                     }
  60.                     else if ((board[j][i] + board[j - 1][i + 1] + board[j - 2][i + 2] + board[j - 3][i + 3]) / 4 == RED) {
  61.                         return RED;
  62.                     }
  63.                 }
  64.                    
  65.             }
  66.         }
  67.  
  68.     }
  69.     return 0;
  70. }
  71.  
  72.  
  73. bool full(int move,byte board[7][6]) {
  74.     if (move > 0 && move < 8) {
  75.         return !board[move - 1][0];
  76.     }
  77.     return false;
  78.    
  79. }
  80.  
  81. void moveLogic(int move, byte player,byte board[7][6]) {
  82.     for (int i = 5; i > -1; i--) {
  83.         if (!board[move - 1][i]) {
  84.             board[move - 1][i] = player;
  85.             break;
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91. int main()
  92. {
  93.     byte board[7][6]{ 0 };
  94.     bool flip = true;
  95.     //player 1 is always black 2 = red;
  96.  
  97.     do {
  98.        
  99.         display(board);
  100.         int move = 0;
  101.         while (!full(move,board)) {
  102.             cout << " player " << (flip ? 1 : 2) << " please select a move ....\n";
  103.             scanf_s("%d", &move);
  104.         }
  105.         moveLogic(move, flip ? BLACK : RED, board);
  106.         flip = !flip;
  107.     } while (!checkwinner(board));
  108.  
  109.     display(board);
  110.     cout << "player " << (checkwinner(board) == RED ? "RED " : "BLACK ") << "WINS!!!!!!!!!!";
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement