Advertisement
Guest User

Connect4

a guest
Jul 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int whichRow(char placement[9][7], int column);
  8. int checkWinner(char board[9][7], char key);
  9.  
  10. int main()
  11. {
  12.     cout << "Welcome to Connect 4!\nPlayer 1 has 'O' tokens and Player 2 has 'X' tokens\nLet's get started!\n\n";
  13.  
  14.     //variable decleration
  15.     char gameBoard[9][7];
  16.     int colNum, player, count, row, winner = 0;
  17.     char token[2] = {'O', 'X'};
  18.     bool gameOver = false;
  19.  
  20.  
  21.     //fills the gameboard
  22.     for(int i = 0; i < 9; i++){//rows
  23.         for(int j = 0; j < 7; j++){//columns
  24.             gameBoard[i][j] = ' ';
  25.         }//end for
  26.     }//end for (fill array)
  27.  
  28.     count = 0;
  29.  
  30.     //loops for constant user input until checkWinner returns true
  31.     while(gameOver == false){
  32.         player = count%2;
  33.         cout << "Player " << player +1 << ", Choose a column (1-7):";
  34.         cin >> colNum;
  35.  
  36.         //prompts user to enter a number within the bounds of the board
  37.         while(colNum > 7 || colNum < 1){
  38.             cout << "\nThat column does not exist.\nEnter a different number:";
  39.             cin >> colNum;
  40.             // row = whichRow(gameBoard, colNum-1);
  41.         }//end while
  42.  
  43.         row = whichRow(gameBoard, colNum-1);
  44.  
  45.         //prompts user to enter a different column # if selected column is full
  46.         while(row == -1){
  47.             cout << "That column is full. Please select another: ";
  48.             cin >> colNum;
  49.             row = whichRow(gameBoard, colNum-1);
  50.         }//end while
  51.  
  52.         gameBoard[row][colNum-1] = token[player];
  53.  
  54.         //prints gameboard with tokens
  55.         for(int i = 0; i < 9; i++){//rows
  56.             cout << "|";
  57.             for(int j = 0; j < 7; j++){//columns
  58.                 cout << gameBoard[i][j] << "|";
  59.             }//end for
  60.             cout << endl;
  61.         }//end for
  62.  
  63.         cout << "=============== \n";
  64.         winner = checkWinner(gameBoard, token[player]);
  65.  
  66.         //prints the winner
  67.         if(winner == 1){
  68.             cout << endl << "Player " << player+1 << " is the winner!!!\n";
  69.             cout << "Congratulations, you win 50 coins!\n\n";
  70.             for(int i = 0; i < 50; i++)
  71.                 cout << " coin ";
  72.             cout << "\n\nWhat can you do with these virtual coins? Nothing!\nHave a great day!\n";
  73.             break;
  74.         }//end if Game Over
  75.  
  76.         //used to alternate player turns
  77.         count++;
  78.     }//end while
  79.  
  80.  
  81.     return 0;
  82. }//end main
  83.  
  84. //checks if there is a token in the selected column and returns the index of the row to place the new token
  85. //checks for full rows and returns indicator to prompt user to enter a different row
  86. int whichRow(char placement[9][7], int column){
  87.     int i;
  88.     for(i = 8; i >= 0; i--){
  89.         if(placement[i][column] == ' ')
  90.             return i;
  91.         if(placement[0][column] != ' ')
  92.             return -1;
  93.     }//end for
  94.  
  95.     return i-1;
  96. }//end whichRow
  97.  
  98.  
  99. //checks for a winner after every turn and returns 'true' if there is a winner and 'false' if there is not
  100. int checkWinner(char board[9][7], char key){
  101.     //horizontal search
  102.     for(int i = 0; i < 9; i++){//rows
  103.         for(int j = 0; j < 4; j++){//columns
  104.             if(board[i][j] == key && board[i][j+1] == key && board[i][j+2] == key && board[i][j+3] == key)
  105.                 return true;
  106.         }//end for
  107.     }//end for
  108.  
  109.     //vertical search
  110.     for(int i = 0; i < 7; i++){//columns
  111.         for(int j = 0; j < 6; j++){//rows
  112.             if(board[j][i] == key && board[j+1][i] == key && board[j+2][i] && board[j+3][i] == key)
  113.                 return 1;
  114.         }//end for
  115.     }//end for
  116.  
  117.     //right slope search
  118.     for(int i = 0; i < 6; i++){//rows
  119.         for(int j = 0; j < 4; j++){//columns
  120.             if(board[i][j] == key && board[i+1][j+1] == key && board[i+2][j+2] == key && board[i+3][j+3] == key)
  121.                 return 1;
  122.         }//end for
  123.     }//end for
  124.  
  125.     //left slope search
  126.     for(int i = 0; i < 6; i++){//rows
  127.         for(int j = 6; j > 2; j--){//columns
  128.             if(key == board[i][j] && key == board[i+1][j-1] && key == board[i+2][j-2] && key == board[i+3][j-3])
  129.                 return 1;
  130.         }//end for
  131.     }//end for
  132.  
  133.     return 0;
  134. }//end linSearch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement