Advertisement
dail9583

Connect-Four - Zach Daily

Feb 5th, 2012
3,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. /*
  2.  A simple Connect-Four game using a 2-dimensional array - [row][column]
  3.  The player wins by connecting four of their spots in one of four directions
  4.     - Horizontal
  5.     | Veritcal
  6.     / Diagonal
  7.     \ Diagonal
  8.  
  9.  To be added: AI as an OPTION for player 2
  10.  
  11.  Date:   2-5-2012
  12.  Author: Zach Daily
  13.  */
  14.  
  15. #include <iostream>
  16. using namespace std;
  17.  
  18.  
  19. //The workings of Connect Four
  20. class connectFour{
  21.     int player;
  22.     int score[2];
  23.     int board[6][7];  //6 = Rows, 7 = Columns
  24.     int move;
  25.     bool win;
  26.    
  27. public:
  28.     //Set the player number to one and the scores to zero
  29.     connectFour(){
  30.         player = 1;
  31.         score[0] = 0;
  32.         score[1] = 0;
  33.         win = false;
  34.        
  35.         //Initialize board array
  36.         for (int i = 0; i < 6; i++){
  37.             for (int x = 0; x < 7; x++){
  38.                 board[i][x] = 0;
  39.             }
  40.         }
  41.     }
  42.    
  43.     //Setup the turn by first checking for a win
  44.     //If there is no win, continue by displaying the board
  45.     //and getting the player's move
  46.     void startTurn(){
  47.         checkWin();
  48.         if (win == true){
  49.             clearScreen();
  50.             display();
  51.             cout<< "Player #" << player << " has won!" <<endl <<endl;
  52.         }
  53.         else{
  54.             display();
  55.             gatherMove();
  56.         }
  57.     }
  58.    
  59.     //Display the board
  60.     void display(){
  61.         clearScreen();
  62.         cout<< " 1  2  3  4  5  6  7 " <<endl;
  63.         for (int i = 0; i < 6; i++){
  64.             for (int x = 0; x < 7; x++){
  65.                 cout<< "[" << board[i][x] << "]";
  66.             }
  67.             cout<< endl;
  68.         }
  69.     }
  70.    
  71.     //Gather the player's move
  72.     void gatherMove(){
  73.         int emptySpots = 0;
  74.         cout<< endl << "Player #" << player << ": Move to which space? ";
  75.         cin>> move;
  76.        
  77.         //If the player enters anything above 7, set it to 7
  78.         if (move > 7){move = 7;}
  79.         //If the player enters anything below 0, set it to 0
  80.         if (move < 0){move = 0;}
  81.        
  82.         //Determine where the player's piece falls
  83.         //If something occupies the lowest point already, put it above
  84.         for (int i = 5; i >= 0; i--){
  85.             if (board[i][move - 1] == 0){
  86.                 board[i][move - 1] = player;
  87.                 break;
  88.             }
  89.             else{
  90.                 emptySpots += 1;
  91.             }
  92.         }
  93.        
  94.         //If there are no more rows in a column, tell the player to try again
  95.         if (emptySpots == 6){
  96.             cout<< endl << "There are no empty spots on #" << move << "! Try again!";
  97.             gatherMove();
  98.         }
  99.        
  100.         //Set the next player to move
  101.         if (player == 1){player = 2;}
  102.         else {player = 1;}
  103.        
  104.         //Call start of next turn
  105.         startTurn();
  106.     }
  107.    
  108.     //Check for any winning combinations
  109.     void checkWin(){
  110.         int start = 0;
  111.        
  112.         //Horizontal Win
  113.         for (int i = 0; i < 6; i ++){
  114.             for (start = 0; start <= 3; start++){
  115.                 if ((board[i][start] == board[i][start+1] && board[i][start+1] == board[i][start+2] && board[i][start+2] == board[i][start+3]) && (board[i][start] != 0)){
  116.                     win = true;
  117.                     player = board[i][start];
  118.                     break;
  119.                 }
  120.             }
  121.         }
  122.        
  123.         //Vertical Win
  124.         for (int i = 0; i < 7; i ++){
  125.             for (start = 0; start <= 2; start++){
  126.                 if ((board[start][i] == board[start+1][i] && board[start+1][i] == board[start+2][i] && board[start+2][i] == board[start+3][i]) && (board[start][i] != 0)){
  127.                     win = true;
  128.                     player = board[start][i];
  129.                     break;
  130.                 }
  131.             }
  132.         }
  133.        
  134.         //Diagonal Win - "/"
  135.         for (int i = 3; i < 6; i ++){
  136.             for (start = 0; start <= 3; start++){
  137.                 if ((board[i][start] == board[i-1][start+1] && board[i-1][start+1] == board[i-2][start+2] && board[i-2][start+2] == board[i-3][start+3]) && (board[i][start] != 0)){
  138.                     win = true;
  139.                     player = board[i][start];
  140.                     break;
  141.                 }
  142.             }
  143.         }
  144.        
  145.         //Diagonal Win - "\"
  146.         for (int i = 0; i < 3; i ++){
  147.             for (start = 0; start <= 4; start++){
  148.                 if ((board[i][start] == board[i+1][start+1] && board[i+1][start+1] == board[i+2][start+2] && board[i+2][start+2] == board[i+3][start+3]) && (board[i][start] != 0)){
  149.                     win = true;
  150.                     player = board[i][start];
  151.                     break;
  152.                 }
  153.             }
  154.         }
  155.     }
  156.    
  157.     //Clear the screen
  158.     //Set for OSX currently - Change the inner system call for Windows or Linux
  159.     void clearScreen(){
  160.         system("clear");
  161.     }
  162.    
  163. };
  164.  
  165. //Main function - Nice and small just like I like it!
  166. int main (int argc, char * const argv[]) {
  167.     connectFour play;
  168.     play.startTurn();
  169.    
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement