Thread: connect 4 problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    24

    connect 4 problem

    how can i show the player by turns???
    i.e.ken's turn then hi's turn
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <limits>
    using namespace std;
    
    // standard Connect Four board size
    const int NROWS = 6;
    const int NCOLS = 7;
    
    //globalvariable declarations
    int board [NROWS][NCOLS] = {0, 0}; // make everything equal 0
    // NOTE: The 8th row is the row that contains the height of the column
    
    // I see what you are trying to do above.
    //   A better way is just to check to see if their is a piece in the
    //   topmost row of the column.
    // Since the 8th row is messing you up, I've removed it.
    
    int playerMove = 1; // which player's move it is.
    int victory; // who has won (0 if no one)
    
    // prototype declarations
    void displayBoard ();
    int inputNewMove ();
    int checkForWin ();
    void gameOver ();
    void logo();
    void playername(string& name1, string&name2);
    void rules();
    
    //----------------------------------------------------------------------------
    void pause()
      {
      // Please don't use system("PAUSE")
      cin.seekg( 0, ios::end );
      cin.clear();
      cout << "Press ENTER to continue..." << flush;
      cin.ignore( numeric_limits<streamsize>::max(), '\n' );
      }
    
    //----------------------------------------------------------------------------
    int main ()
      {
      string name1, name2;
      logo();
      playername(name1, name2);
      rules();
      displayBoard ();
      for (int i = 0 ; i < 15 ; i++)
        {
        cout << name1 << "'s turn." << endl;
    //    cout << board[8][1] << endl;  I presume this is debug info
    //    cout << board[7][1] << endl;  as the user won't care to see it.
    //                                  (besides which, I killed board[7]...)
    
        inputNewMove ();
        displayBoard ();
        victory = checkForWin ();
        if (victory > 0)
          {
          gameOver ();
          return 0;
          }
        }
      pause();
      return 0;
      }
    
    //----------------------------------------------------------------------------
    void displayBoard ()
      {
      // suggestion: use a lookup table
      const char LUT[] = " 12";
    
      cout << "  1 2 3 4 5 6 7" << endl;
      cout << "  --------------";
      for (int boardRow = 0 ; boardRow<NROWS ; boardRow++)// cycle through rows
        {
        cout << endl; // go to the next line
        cout << (boardRow + 1) << "|";
        for (int boardColumn = 0 ; boardColumn<NCOLS ; boardColumn++) // cycle through columns
          {
          cout << LUT[ board[boardRow][boardColumn] ] << ' ';
    /*
          if (board [boardRow][boardColumn] == 0) // empty
            {
            cout << "  ";
            }
          if (board [boardRow][boardColumn] == 1) // player 1's piece
            {
            cout << "1 ";
            }
          if (board [boardRow][boardColumn] == 2) // player 2's piece
            {
            cout << "2 ";
            }
    */
          }
        }
      cout << endl << endl;
      return;
      }
    
    //----------------------------------------------------------------------------
    bool isColumnFull( int col )
      {
      return board[0][col] != 0;
      }
    
    //----------------------------------------------------------------------------
    int inputNewMove()
      {
      int x;
      int height;
    
      for (;;)
        {
        cout << "Column? ";
        cin >> x;
    
        if ((x <= 7) && (x >= 1) && !isColumnFull( x-1 ))  
          {                       //(board[8][x-1] != 8))
          break;
          }
    
        else
          {
          cout << "Choice must be between 1 and 7, and column cannot be full." << endl;
          }
        }
    
    
    // brilliant! however, since board[7] no longer exists... 
    //  board[8][x-1]++; // adds one to the HEIGHT OF THE COLUMN VALUE (ROW 8)
    //  height = (8 - (board[8][x-1])); // determines point in array to fall (simulates the falling piece until it stops)
    
      // search for the next available space
      for (height=NROWS-1; board[height][x-1] != 0; height--);
    
      board [height][x-1] = playerMove; // sets array coordinate to the number of the player
    
    
      //switch move
      if (playerMove == 1)
        playerMove++;
      else
        playerMove--;
    
      return 0;
      }
    
    
    //----------------------------------------------------------------------------
    int checkForWin ()
      {
    
      //test for VERTICAL victory
      for (int i=0; i<NCOLS-3; i++) // columns
        {
        for (int j=0; j<NROWS; j++) // rows
          {
    
          if (board[j][i] == 1 && board[j][i+1] == 1 && board[j][i+2] == 1 && board[j][i+3] == 1)
            {
            return 1;
            }
          if (board[j][i] == 2 && board[j][i+1] == 2 && board[j][i+2] == 2 && board[j][i+3] == 2)
            {
            return 2;
            }
          }
        }
    
      //test HORIZONTAL victory
      for (int i=0; i<NCOLS; i++) // columns
        {
        for (int j=0; j<NROWS-3; j++) // rows
          {
    
          if (board[j][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1)
            {
            return 1;
            }
          if (board[j][i] == 2 && board[j+1][i] == 2 && board[j+2][i] == 2 && board[j+3][i] == 2)
            {
            return 2;
            }
          }
        }
    
      // don't forget to test for diagonal victory
      // (combine what was done for your vertical and horizontal tests)
    
    
      return 0;
      }
    
    
    //----------------------------------------------------------------------------
    void gameOver()
      {
      cout << endl << "Game Over! Player " << victory << "has won!\n" ;
      pause();
      }
    void logo()
    {
    	cout<<"------------Welcome to Connect 4 ----------------"<<endl;
    	cout<<"     ************Connect*************"<<endl;
    	cout<<"    **********************************"<<endl;
    	cout<<"   *****************4******************"<<endl;
    	cout<<"  *****************4*4******************"<<endl;
    	cout<<"  ****************4**4******************"<<endl;
    	cout<<"  **************4444444***************"<<endl;
    	cout<<"   *****************4****************"<<endl;
    	cout<<"    ****************4***************"<<endl;
    	cout<<"\n\n"<<endl;
    }
    void playername(string& name1, string&name2)
    {
    	cout<<"What is player 1's name : ";
    	cin>>name1;
    	cout<<"What is player 2's name : ";
    	cin>>name2;
    	cout<<"Hi "<<name1<<", "<<name2<<endl;
    }
    void rules()
    {
    	cout<<"The following is the rules of the Connect 4 : \n";
    	cout<<"1. Each player in his turn drops one of his checkers down any of the slots \n   in the top of the grid. \n";
    	cout<<"2. The play alternates until one of the players gets four checkers of his \n   colour in a row. The four in a row can be horizontal, vertical, or diagonal.\n";
    	cout<<"3. The first player to get four in a row wins.\n";
    	cout<<"4. If the board is filled with pieces and neither player has 4 in a row, \n   then the game is a draw.\n";
    }
    there is the output
    http://i1128.photobucket.com/albums/...0/CONNECT4.jpg
    thx

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need another variable to store who's turn it is.

    say
    int currentPlayer = 1;

    Then later
    Code:
    if ( currentPlayer == 1 ) {
      currentPlayer = 2;
    } else {
      currentPlayer = 1;
    }
    Now use that to decide whether to display name1 or name2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    24

    Question

    Quote Originally Posted by Salem View Post
    You need another variable to store who's turn it is.

    say
    int currentPlayer = 1;

    Then later
    Code:
    if ( currentPlayer == 1 ) {
      currentPlayer = 2;
    } else {
      currentPlayer = 1;
    }
    Now use that to decide whether to display name1 or name2
    thanks for your help
    now, i can show the names by turns
    but there are still some questions.
    it can't show which one's turn at the first time
    like this : http://i1128.photobucket.com/albums/.../CONNECT42.jpg
    and here is the code
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <limits>
    #include <string>
    using namespace std;
    
    // standard Connect Four board size
    const int NROWS = 6;
    const int NCOLS = 7;
    
    //globalvariable declarations
    int board [NROWS][NCOLS] = {0, 0}; // make everything equal 0
    // NOTE: The 8th row is the row that contains the height of the column
    
    // I see what you are trying to do above.
    //   A better way is just to check to see if their is a piece in the
    //   topmost row of the column.
    // Since the 8th row is messing you up, I've removed it.
    int currentPlayer = 1;
    int playerMove = 1; // which player's move it is.
    int victory; // who has won (0 if no one)
    
    // prototype declarations
    void displayBoard ();
    int inputNewMove ();
    int checkForWin ();
    void gameOver ();
    void logo();
    void playername(string& name1, string&name2);
    void rules();
    
    //----------------------------------------------------------------------------
    
    //----------------------------------------------------------------------------
    int main ()
      {
      string name1, name2;
      logo();
      playername(name1, name2);
      rules();
      displayBoard ();
      for (int i = 0 ; i < 15 ; i++)
        {
        inputNewMove ();
        displayBoard ();
        victory = checkForWin ();
        if (victory > 0)
          {
          gameOver ();
          return 0;
          }
    	if ( currentPlayer == 1 ) {
      cout<<name1 <<"'s turn"<<endl;
    } 
    	else {
      cout<<name2 <<"'s turn"<<endl;
    }
    	currentPlayer++;
    	currentPlayer=currentPlayer%2;
        }
    }
    
    
    //----------------------------------------------------------------------------
    void displayBoard ()
      {
      // suggestion: use a lookup table
      const char LUT[] = " 12";
    
      cout << "  1 2 3 4 5 6 7" << endl;
      cout << "  --------------";
      for (int boardRow = 0 ; boardRow<NROWS ; boardRow++)// cycle through rows
        {
        cout << endl; // go to the next line
        cout << (boardRow + 1) << "|";
        for (int boardColumn = 0 ; boardColumn<NCOLS ; boardColumn++) // cycle through columns
          {
          cout << LUT[ board[boardRow][boardColumn] ] << ' ';
    /*
          if (board [boardRow][boardColumn] == 0) // empty
            {
            cout << "  ";
            }
          if (board [boardRow][boardColumn] == 1) // player 1's piece
            {
            cout << "1 ";
            }
          if (board [boardRow][boardColumn] == 2) // player 2's piece
            {
            cout << "2 ";
            }
    */
          }
        }
      cout << endl << endl;
      return;
      }
    
    //----------------------------------------------------------------------------
    bool isColumnFull( int col )
      {
      return board[0][col] != 0;
      }
    
    //----------------------------------------------------------------------------
    int inputNewMove()
      {
      int x;
      int height;
      for (;;)
        {
        cout << "Column? ";
        cin >> x;
    
        if ((x <= 7) && (x >= 1) && !isColumnFull( x-1 ))  
          {                       //(board[8][x-1] != 8))
          break;
          }
    
        else
          {
          cout << "Choice must be between 1 and 7, and column cannot be full." << endl;
          }
        }
    
    
    // brilliant! however, since board[7] no longer exists... 
    //  board[8][x-1]++; // adds one to the HEIGHT OF THE COLUMN VALUE (ROW 8)
    //  height = (8 - (board[8][x-1])); // determines point in array to fall (simulates the falling piece until it stops)
    
      // search for the next available space
      for (height=NROWS-1; board[height][x-1] != 0; height--);
    
      board [height][x-1] = playerMove; // sets array coordinate to the number of the player
    
    
      //switch move
      if (playerMove == 1)
        playerMove++;
      else
        playerMove--;
    
      return 0;
      }
    
    
    //----------------------------------------------------------------------------
    int checkForWin ()
      {
    
      //test for VERTICAL victory
      for (int i=0; i<NCOLS-3; i++) // columns
        {
        for (int j=0; j<NROWS; j++) // rows
          {
    
          if (board[j][i] == 1 && board[j][i+1] == 1 && board[j][i+2] == 1 && board[j][i+3] == 1)
            {
            return 1;
            }
          if (board[j][i] == 2 && board[j][i+1] == 2 && board[j][i+2] == 2 && board[j][i+3] == 2)
            {
            return 2;
            }
          }
        }
    
      //test HORIZONTAL victory
      for (int i=0; i<NCOLS; i++) // columns
        {
        for (int j=0; j<NROWS-3; j++) // rows
          {
    
          if (board[j][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1)
            {
            return 1;
            }
          if (board[j][i] == 2 && board[j+1][i] == 2 && board[j+2][i] == 2 && board[j+3][i] == 2)
            {
            return 2;
            }
          }
        }
    
      // don't forget to test for diagonal victory
      // (combine what was done for your vertical and horizontal tests)
    
    
      return 0;
      }
    
    
    //----------------------------------------------------------------------------
    void gameOver()
      {
      cout << endl << "Game Over! Player " << victory << "has won!\n" ;
      getchar();
      }
    void logo()
    {
    	cout<<"------------Welcome to Connect 4 ----------------"<<endl;
    	cout<<"     ************Connect*************"<<endl;
    	cout<<"    **********************************"<<endl;
    	cout<<"   *****************4******************"<<endl;
    	cout<<"  *****************4*4******************"<<endl;
    	cout<<"  ****************4**4******************"<<endl;
    	cout<<"  **************4444444***************"<<endl;
    	cout<<"   *****************4****************"<<endl;
    	cout<<"    ****************4***************"<<endl;
    	cout<<"\n\n"<<endl;
    }
    void playername(string& name1, string&name2)
    {
    	cout<<"What is player 1's name : ";
    	cin>>name1;
    	cout<<"What is player 2's name : ";
    	cin>>name2;
    	cout<<"Hi "<<name1<<", "<<name2<<endl;
    }
    void rules()
    {
    	cout<<"The following is the rules of the Connect 4 : \n";
    	cout<<"1. Each player in his turn drops one of his checkers down any of the slots \n   in the top of the grid. \n";
    	cout<<"2. The play alternates until one of the players gets four checkers of his \n   colour in a row. The four in a row can be horizontal, vertical, or diagonal.\n";
    	cout<<"3. The first player to get four in a row wins.\n";
    	cout<<"4. If the board is filled with pieces and neither player has 4 in a row, \n   then the game is a draw.\n";
    }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Please help Me :'( :'( :'(

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So why not put this if/else

    if ( currentPlayer == 1 )

    at the start of the loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Quote Originally Posted by Salem View Post
    So why not put this if/else

    if ( currentPlayer == 1 )

    at the start of the loop?
    you mean like this???
    Code:
    int main ()
      {
      string name1, name2;
      logo();
      playername(name1, name2);
      rules();
      displayBoard ();
      for (int i = 0 ; i < 15 ; i++)
        {
         if ( currentPlayer == 1 ) {
      cout<<name1 <<"'s turn"<<endl;
    } 
    	else {
      cout<<name2 <<"'s turn"<<endl;
    }
    	currentPlayer++;
    	currentPlayer=currentPlayer%2;
        }
        inputNewMove ();
        displayBoard ();
        victory = checkForWin ();
        if (victory > 0)
          {
          gameOver ();
          return 0;
          }
    	
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you asking, or did you try it?

    Well I said move the if/else, not the next two lines which do the "work out the next player, based on the current player" calculation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    thank you
    this problem fixed already

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. problem: online on winxp and linux red hat
    By gemini_shooter in forum Linux Programming
    Replies: 5
    Last Post: 05-29-2005, 02:14 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM