i know it is a lot to read..... and i have different variables to compare win - the gameboard play itself and an undeclared plr, i did that because i'm not sure what and how it should be used there (so i expected those errors), b4 i added in the checkWin() the board displayed and user entered a col - n nothing else happnd....im in a very bad state... any suggestions as to what i should use or an example of what i can use with my version, wud be appreciated
PS~ im not THAT gud @ c++ so laymans terms with any explanations plzzzzzzzzzzz
thk you
and im gettin an expected primary expression before '[' token error :confused:

/***************************************************************************
**                                                                        **
**                            Five In A Row                               **
**                                                                        **
**                                                                        **
**                                                                        **
**                            By Shade Marshall                           **
**                          (102485@usc.edu.tt)                           **
**                                                                        **
**                             April, 2010                                **
**                                                                        **
****************************************************************************
***************************************************************************/

#include <iostream>
#include <fstream>

using namespace std;

//Class declaration
class fiveInArow
{
private:
        string reallyLikeMyTutors;
public:
       string plr1;
       string plr2;
       int chip1;
       int chip2;
       int gameBoard[10][10];
       int option;
       int rows;
       int cols;

       fiveInArow();//class constructor
       ~fiveInArow();//destructor
       void menu();// Menu Method
       void playerOpt();// User Option to play game
       void drawBoard();//draw board of play
       bool chkChip();//
       void gamePlay();// used to play game
       void checkCol();//used to check availability of play in specified column
       bool diagonalRightWin();// check for right diagonal wins
       bool diagonalLeftWin();// check for left diagonal wins
       bool horizontalWin(); // check for horizontal wins
       bool verticalWin(); // check for vertical wins
       bool checkWin(); // to call all win check functions/methods

 };

 //implementation section
fiveInArow::fiveInArow()
{

            for(int rowCount = 0; rowCount < 10; rowCount++) // for loop to initialize gameboard rows
            {
                for (int colsCount=0; colsCount < 10; colsCount++) // nested for loop to initialize gameboard columns
                {
                    gameBoard[rowCount][colsCount]='_'; // puts an underscore'_'
                }
           }
}//default constructor

fiveInArow::~fiveInArow()
{
}// destructor

void fiveInArow::menu() // Function to display Menu
{

     do
     {
       system("color 1f"); // function which change the background and text color.
       system("title Main Menu"); // function to display the Main Menu on the system window.
       system("cls"); // Function used to clear the screen.

       // Main Menu Display.
       cout << "********************************************"<< endl;
       cout << "***********WELCOME TO 5-In-A-Row************"<< endl;
       cout << "********************************************"<< endl;
       cout << "********** 1. Player1 vs Player2   *********"<< endl;
       cout << "********** 2. Player vs Computer   *********"<< endl;
       cout << "********** 3. View Player Rankings *********"<< endl;
       cout << "********** 4. Exit Game            *********"<< endl;
       cout << "********************************************"<< endl;
       cout << "********************************************"<< endl;

            cout << "Enter an Option from the Menu: " ; // Display Message
            cin >> option; // User input
            system("cls");

       switch(option) // Start of the Switch fuction
       {
               case 1: // Player1 vs Player2
                       playerOpt(); // Player option method call
                       drawBoard();//drawBoard method called
                       gamePlay();// gridPlay method call
                       system("pause");
                      break;
               case 2: // Player vs Computer
                       cout << "Not yet implemented" << endl;
                      break;
               case 3: // View Player Rankings
                       cout << "Not yet implemented" << endl;
                      break;
               case 4: // Exit Game
                       cout << "Have a Nice Day!! :D" <<endl;
                       option = 4;
                      break;
               default:
                       cout <<"Select an Option from the Main Menu: " << endl;
                       break;
       }

     }while(option != 4);

}


void fiveInArow::playerOpt()//User Option to play game
{
     cout << "Player1, please enter your name: ";
     cin >> plr1;
     plr1 = chip1;
     cout << endl;

     cout << "Player2, please enter your name: ";
     cin >> plr2;
     plr2 = chip2;
     cout << endl;

     cout << "\t \t " << plr1 << " \a vs \a " << plr2 << endl;
     cout << endl;
}


void fiveInArow::drawBoard()//board display
{

        for(int rowCount = 0; rowCount < 10; rowCount++) //for loop used to draw gameboard using rows count
        {
                for (int colsCount = 0; colsCount < 10 ;colsCount++) //for loop used to draw gameboard using columns count
                {
                    cout << '|' << gameBoard[rowCount][colsCount]; // draws '|' before value in gameboard
                }
                cout << '|' << endl; // draws '|' afta value in gameboard
        }
        cout << '|' << '0' << '|' <<'1' << '|' << '2' <<
            '|'<<'3'<<'|'<<'4'<<'|'<< '5' <<'|'<<'6'<<'|' <<'7'<< '|' <<'8' << '|' <<'9' << endl;
}



void fiveInArow::gamePlay()//GameBoard Grid display.
{
     rows = 10; // used to set values to the rows of the gameBoard.
     cols = 10; // used to set values to the cols of the gameBoard.


            cout << " Your turn To play: " << plr1 << endl;

            cout << " Select column of board to play: ";
            cin >> option;

            switch(option)
            {
                    case 0: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 1: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 2: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 3: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 4: drawBoard();
                            system ("cls");//linux - clear
                            break;

                    case 5: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 6: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 7: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;


                    case 8: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 9: drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    default:    cout << "\a Select Option from Game Board: " << endl;
                                exit(1);
                                break;
            }

            cout << " Your turn To play: " << plr2 << endl;

            cout << " Select column of board to play: ";
            cin >> option;

            switch(option)
            {
                    case 0: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 1: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 2: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 3: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 4: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 5: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 6: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 7: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;


                    case 8: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    case 9: cout << "Under Construction: col1" << endl;
                            drawBoard();
                            checkCol();
                            system ("cls");//linux - clear
                            break;

                    default:    cout << "\a Select Option from Game Board: " << endl;
                                exit(1);
                                break;
            }
}
bool fiveInArow::chkChip()
{
    if (chip1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void fiveInArow::checkCol()
{

        if (chkChip() == true )  // if it is plr1


            while (gameBoard[rows][cols] != 0) //while choice is not 0 (empty)
            {
            gameBoard[rows --][cols]; // check same column, row above until choice is 0 (empty)
            gameBoard[rows][cols] = 1; //then assign to 1 when choice is 0
            }



            else
                if (chkChip() == false) // else if plr2 and choice is not 0

                while (gameBoard[rows][cols] != 0) // while choice is not 0 (empty)
                {
                    gameBoard[rows --][cols]; //check same column, row above until choie is 0
                    gameBoard[rows][cols] = 2; //then assign to 2 when choice is 0
                }


}



bool fiveInArow::checkWin()
{
     if ((diagonalRightWin() == true) ||
        (diagonalLeftWin() == true) ||
        (horizontalWin() == true) ||
        (verticalWin() == true))
        cout << " We Have A WINNER!!! " << endl;
        cout << "CONGRATULATIONS :D " << endl;
}
bool fiveInArow::diagonalRightWin()
{

     if     ((gameBoard[rows][cols] == [rows-1][cols+1] ) && //why 'expected primary expressio before '[' token error'?
            (gameBoard[rows][cols] == [rows - 2][cols + 2]) &&
            (gameBoard[rows][cols] == [rows - 3][cols + 3]) &&
            (gameBoard[rows][cols] == [rows - 4][cols + 4]) == true) ||

            (gameBoard[rows][cols] == [rows + 1] && [cols - 1]) &&
            (gameBoard[rows][cols] == [rows - 1] && [cols + 1]) &&
            (gameBoard[rows][cols] == [rows - 2] && [cols + 2]) &&
            (gameBoard[rows][cols] == [rows - 3] && [cols + 3]) ||

        (plr == [rows + 2] && [cols - 2]) &&
        (plr == [rows + 1] && [cols - 1]) &&
        (plr == [rows - 1] && [cols + 1]) &&
        (plr == [rows - 2] && [cols + 2]) ||

        (plr == [rows + 3] && [cols - 3]) &&
        (plr == [rows + 2] && [cols - 2]) &&
        (plr == [rows + 1] && [cols - 1]) &&
        (plr == [rows - 1] && [cols + 1]) ||

        (plr == [rows + 4] && [cols - 4]) &&
        (plr == [rows + 3] && [cols - 3]) &&
        (plr == [rows + 2] && [cols - 2]) &&
        (plr == [rows + 1] && [cols - 1]))
        return true;
}

bool fiveInArow::diagonalLeftWin()
{
     if (plr == [rows - 1] && [cols - 1]) &&
        (plr == [rows - 2] && [cols - 2]) &&
        (plr == [rows - 3] && [cols - 3]) &&
        (plr == [rows - 4] && [cols - 4]) ||

        (plr == [rows + 1] && [cols + 1] &&
        (plr == [rows - 1] && [cols - 1]) &&
        (plr == [rows - 2] && [cols - 2]) &&
        (plr == [rows - 3] && [cols - 3]) ||

        (plr == [rows + 2] && [cols + 2]) &&
        (plr == [rows + 1] && [cols + 1]) &&
        (plr == [rows - 1] && [cols - 1]) &&
        (plr == [rows - 2] && [cols - 2]) ||

        (plr == [rows + 3] && [cols + 3]) &&
        (plr == [rows + 2] && [cols + 2]) &&
        (plr == [rows + 1] && [cols + 1]) &&
        (plr == [rows - 1] && [cols - 1]) ||

        (plr == [rows + 4] && [cols + 4]) &&
        (plr == [rows + 3] && [cols + 3]) &&
        (plr == [rows + 2] && [cols + 2]) &&
        (plr == [rows + 1] && [cols + 1])
        return true;
}

bool fiveInArow::horizontalWin()
{
    if  (plr == [rows] && [cols + 1]) &&
        (plr == [rows] && [cols + 2]) &&
        (plr == [rows] && [cols + 3]) &&
        (plr == [rows] && [cols + 4]) ||

        (plr == [rows] && [cols - 1]) &&
        (plr == [rows] && [cols + 1]) &&
        (plr == [rows] && [cols + 2]) &&
        (plr == [rows] && [cols + 3]) ||

        (plr == [rows] && [cols - 1]) &&
        (plr == [rows] && [cols - 2]) &&
        (plr == [rows] && [cols + 1]) &&
        (plr == [rows] && [cols + 2]) ||

        (plr == [rows] && [cols] - 1) &&
        (plr == [rows] && [cols] - 2) &&
        (plr == [rows] && [cols] - 3) &&
        (plr == [rows] && [cols] + 1) ||

        (plr == [rows] && [cols - 1]) &&
        (plr == [rows] && [cols - 2]) &&
        (plr == [rows] && [cols - 3]) &&
        (plr == [rows] && [cols - 4]) ||
        return true;
}

bool fiveInArow::verticalWin()
{
    if  (plr == [rows - 1] && [cols]) &&
        (plr == [rows - 2] && [cols]) &&
        (plr == [rows - 3] && [cols]) &&
        (plr == [rows - 4] && [cols]) ||

        (plr == [rows + 1] && [cols]) &&
        (plr == [rows - 1] && [cols]) &&
        (plr == [rows - 2] && [cols]) &&
        (plr == [rows - 3] && [cols]) ||

        (plr == [rows + 1] && [cols]) &&
        (plr == [rows + 2] && [cols]) &&
        (plr == [rows - 1] && [cols]) &&
        (plr == [rows - 2] && [cols]) ||

        (plr == [rows + 1] && [cols]) &&
        (plr == [rows + 2] && [cols]) &&
        (plr == [rows + 3] && [cols]) &&
        (plr == [rows - 1] && [cols]) ||

        (plr == [rows + 1] && [cols]) &&
        (plr == [rows + 2] && [cols]) &&
        (plr == [rows + 3] && [cols]) &&
        (plr == [rows + 4 ]&& [cols])
        return true;
}





int main()
{



    fiveInArow display;// create an object of the class
    display.menu();//object call of menu method
    display.drawBoard();// object call of drawBoard method
    display.gamePlay();// call of gridPlay method to start playing
    display.checkWin();//check for player win

    system("pause");

    return 0;
}
Fbody commented: There's alot here, but good job on the [code] tags. :) +1

Recommended Answers

All 7 Replies

(gameBoard[rows][cols] == [rows-1][cols+1] )

Not real sure what you are trying to accomplish here.

I think you probably meant (gameBoard[rows][cols] == gameBoard[rows-1][cols+1] ) .

You have this same basic error for every comparison in your fiveInArow::diagonalRightWin() function. At first glance, I'm pretty sure you've replicated it in your other "Win" functions as well, but I didn't do any further digging.

Once you have that corrected, keep an eye on your array limits. I'm a little concerned you may be getting into some overruns under certain circumstances.

debug the code then only come to know the actual problem.I g++ complier compile the following command g++ -g -o outputfilename programname
then execute gdb ./outputfilename and then execute b main then press r (4 run ) and then press s ( 4 setp by step ) for print use print variable or p variablename

thank you very much a simple thing lyk that i was not sure about - it has compiled but i have some syntax errors now to figure out
the grid prints all 95's user names are errors (outputted really wierd) but it does sink to the bottom the 1 time i chose a column b4 the program crashed:$

This is intended to be a professional site. Please don't use "netspeak".

What is the error that you are getting? Can you post the relevant section of your code? You don't need to post the whole thing...

sori but i put it there so if there were any other errors that i missed that someone else saw i could be told but ill take some down in a little bit i described the errors i was gettin right above your comment

I can't understand your error descriptions because of the netspeak and lack of punctuation. Use proper language and punctuation so that you can be understood.

Post what you expect the output to be and what you are actually getting.

Ok, The object of this program is to create a five in a row game where two players can play against each other. the gameBoard is supposed to be filled with 0's at first and the player is supposed to choose a column of play. the program is supposed to check within that column to see where is an available place of play and 'drop' the player's 'chip'(just there number - either 1 or 2) to that position. It is then expected that the program will check for a win every time a player has played.
Thus far the program is allowing for user entry of name and displaying the original gameBoard, but is not outputting the message for user to choose column or anything following that.
it crashes when calling the gamePlay() which calls the checkWin() first, then the checkplr1() and checkplr2() which contains the message for user input of column

void fiveInArow::gamePlay()//GameBoard Grid display.
{

    bool winGame = checkWin();//declares bool variable to save return value of checkWin function called


    while (winGame!= true)
    {
        checkplr1();//user picks col, checkCol()
        checkplr2();
        break;
    }
}


void fiveInArow::checkplr1()
{
    cout<<plr1<<"Enter which column you wish to play in: ";
    cin>>cols;

        for(int rows = 9; rows>=0; rows--)// for loop to place player1's value into column
        {


            if (gameBoard[rows][cols] != 1)
                        {

                            gameBoard[rows][cols] = 1;
                            checkWin();

                        }
            }
             system("cls");
            //system("clear");// Clear Screen function
            drawBoard();//

}



void fiveInArow::checkplr2()
{
    cout<<plr2<< "Enter which column you wish to play in: ";
    cin>>cols;

            for(int rows = 9; rows>=0; rows--)// for loop to place player2's value into column
            {


                        if (gameBoard[rows][cols] != 2)
                        {

                            gameBoard[rows][cols] = 2;
                            checkWin();

                        }
            }


                    system("cls");
                    //system("clear");// Clear Screen function
                    drawBoard();//call of grid function to display player's move

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.