Click here to Skip to main content
15,305,430 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
class cGameManager
{
private:
    int width, height;
    int score1, score2;
    char up1, down1, up2, down2;
    bool quit;
    cBall * ball;
    cPaddle *player1;
    cPaddle *player2;

public:
    cGameManager(int w, int h)
    {
        srand(time(NULL));
        quit = false;
        up1 = 'w'; up2 = 'i';
        down1 = 's'; down2 = 'k';
        score1 = score2 = 0;
        width = w; height = h;
        ball = new cBall(w / 2, h / 2);
        player1 = new cPaddle(1, h/2 - 3);
        player2 = new cPaddle(w - 2, h/2 - 3);
    }

    ~ cGameManager()
    {
        delete ball, player1, player2;
    }

    void ScoreUp(cPaddle * player)
    {
        if(player == player1)
            score1++;
        else if(player == player2)
            score2++;
        ball->Reset();
        player1->Reset();
        player2->Reset();
    }

    void Draw()
    {
        system("clear");
        for(int i = 0; i < width + 2; i++)
            cout << "\u2593";
        cout << endl;
        for(int i = 0; i < height; i++)
        {
            for(int j = 0; j < width; j++)
            {
                int ballx = ball->getX();
                int bally = ball->getY();
                int player1x = player1->getX();
                int player2x = player2->getX();
                int player1y = player1->getY();
                int player2y = player2->getY();
                
                if(j == 0)
                    cout << "\u2593";
    
                if(ballx == j && bally == i)
                    cout << "O";//ball
                else if(player1x == j && player1y == i)
                    cout << "\u2588";//player1
                else if(player2x == j && player2y == i)
                    cout << "\u2588";//player2
                else if(player1x == j && player1y + 1 == i)
                    cout << "\u2588";//player1
                else if(player1x == j && player1y + 2 == i)
                    cout << "\u2588";//player1
                else if(player1x == j && player1y + 3 == i)
                    cout << "\u2588";//player1
                else if(player2x == j && player2y + 1 == i)    
                    cout << "\u2588";//player2
                else if(player2x == j && player2y + 2 == i)
                    cout << "\u2588";//player2
                else if(player2x == j && player2y + 3 == i)
                    cout << "\u2588";//player2
                else
                    cout << " "; 
    
                if(j == width - 1)
                    cout << "\u2593";
            }
            cout << endl;
        }

        for(int i = 0; i < width + 2; i++)
        {
            cout << "\u2593";
        }
        cout << endl;
    }

    void Input()
    {
        ball->Move();

        int ballx = ball->getX();
        int bally = ball->getY();
        int player1x = player1->getX();
        int player2x = player2->getX();
        int player1y = player1->getY();
        int player2y = player2->getY();
    
        cbreak();
        while(true)
        {
            char current = getch();
            if(current == up1)
                if(player1y > 0)
                    player1->moveUp();
            if(current == up2)
                if(player2y > 0)
                    player2->moveUp();
            if(current == down1)
                if(player1y + 4 < height)
                    player1->moveDown();
            if(current == down2)
                if(player2y + 4 < height)
                    player2->moveDown();
            
            if(ball->getDirection() == STOP)
                ball->randomDirection();
            
            if(current == 'q')
                quit = true;
        }

    void Logic()
    {
        int ballx = ball->getX();
        int bally = ball->getY();
        int player1x = player1->getX();
        int player2x = player2->getX();
        int player1y = player1->getY();
        int player2y = player2->getY();

        //left paddle
        for(int i = 0; i<4; i++)
            if(ballx == player1x + 1)
                if(bally == player1y + i)
                    ball->changeDirection((edir)((rand() %3) + 4);
        
        //right paddle
        for(int i = 0; i<4; i++)
            if(ballx == player2x - 1)
                if(bally == player2y + i)
                    ball->changeDirection((edir)((rand() %3) + 1);
    
        //bottom wall
        if(bally == height - 1)
            ball->changeDirection(ball->getDirection() == DOWNRIGHT ? UPRIGHT: UPLEFT);
        //top wall
        if(bally == 0)
            ball->changeDirection(ball->getDirection() == UPRIGHT ? DOWNRIGHT: DOWNLEFT);
        //right wall
        if(ballx == width - 1)
            ScoreUp(player1);
        //left wall
        if(ballx == 0)
            ScoreUp(player2);
    }

    void Run()
    {
        while(!quit)
        {
            Draw();
            Input();
            Logic();
        }
    }
};

int main()
{
    cGameManager c(40, 20);
    c.Run();
    return 0;
}
I keep getting errors from running this part of my program(There is more if needed):
ponggame.cpp:278:1: error: expected ‘}’ at end of input
 }
 ^
ponggame.cpp:102:1: note: to match this ‘{’
 {
 ^
ponggame.cpp: In member function ‘void cGameManager::Input()’:
ponggame.cpp:227:5: error: a function-definition is not allowed here before ‘{’ token
     {
     ^
ponggame.cpp:262:5: error: a function-definition is not allowed here before ‘{’ token
     {
     ^
ponggame.cpp: In member function ‘int cGameManager::main()’:
ponggame.cpp:276:7: error: ‘class cGameManager’ has no member named ‘Run’
     c.Run();
       ^~~
ponggame.cpp: At global scope:
ponggame.cpp:278:1: error: expected unqualified-id at end of input
 }
 ^


What I have tried:

I have looked up videos looked at stack overflow and looked here and found nothing usefull. help please! Thanks in advance!
Posted
Updated 27-Apr-21 11:44am
v3
Comments
Patrice T 27-Apr-21 16:03pm
   
Obviously the code snipset do not enough lines to match the error messages.
MARLON LESUEUR 27-Apr-21 16:06pm
   
Have you scrolled down because all of the errors are in cgamemanager class and int main wich are clearly there
MARLON LESUEUR 27-Apr-21 16:09pm
   
P.S. My code has correct indenting.I was just to lazy to go through and indent again
Rick York 27-Apr-21 16:19pm
   
It's pretty easy to see when it is indented correctly after it is de-tabbed. This site doesn't like tabs in code.
Rick York 27-Apr-21 19:52pm
   
One thing I recommend is to avoid using literal values. For example, in the Logic function the paddle loops iterate four times. Why? What is the significance of 4? The random number calls have a similar thing going on. Other examples are all of the text strings displayed. Those should be const values defined somewhere with descriptive names.

Another thing - this is C++. Why not make a player object? It can have a paddle and up/down values. A team object could have a score and two player objects. That can help make your code much more modular and easy to deal with.

You're definitely missing brackets when you call
C++
ball->changeDirection((edir)((rand() %3) +4);
for both "left paddle" and "right paddle". There might be other errors, too, but without an example that compiles, who can tell? And no, that's not meant to be an invitation to post the whole thing.
   
Comments
MARLON LESUEUR 27-Apr-21 16:02pm
   
Ok, but that does not solve the fact that linux is saying I haven't called Run, or Input is not supposed to be called or main is part of gamemanager. Thanks for the advice tho I will change it
MARLON LESUEUR 27-Apr-21 16:07pm
   
Also in a lot of tutorials I have seen they don't really put brackets for if loops.
k5054 27-Apr-21 17:45pm
   
Hmm.. Maybe we need to define terms. I've always called () brackets, {} braces and [] square brackets. With that in mind, lets rewrite the code, as pasted, with a slightly different format:
ball->changeDirection(
                         (
                              edir
                         )
                         (
                             (
                                 rand() %3
                             ) +4
                         );
Clearly, there's a missing closing bracket ) for the call to changeDirection.
Rick York 27-Apr-21 18:01pm
   
For me, () are parentheses or parens, {} are braces, and [] are brackets. Sort of close.
You aren't "running" that code: you are compiling it. You cannot run code until it compiles without errors (and preferably without warnings either).

Start by looking at the actual error message - begin with the first:
ponggame.cpp:278:1: error: expected ‘}’ at end of input
 }
 ^
That tells you a lot of things, if you just look:
The file the error was found in:  ponggame.cpp
The line it was found on:         278
The column on which it was found: 1
The problem that was found:       "expected ‘}’ at end of input"
And it also shows you the actual line and a pointer to where the problem is:
 }
 ^

Probably, that's the last line of the file, and the error is that it expects a "}" to match up with an "{" you have inserted - if the brackets don't match up, you will always get an error.

So somewhere in your code you forgot to close a function definition, loop body, conditional block, or similar. For example:
C++
void foo(void)
   {
   ...
 
void bar(void)
   {
   ...
   }
Because you forgot to end the foo method it will give you an error when it gets to the end of the file because that's the last point at which you might have put it!

If your code was indented properly - and it isn't, not consistently even in that small sample - it would probably be really obvious. So start off but indenting properly and matching up your open and close brackets.

When you fix that, compile again, and the chances are many of the other error will have gone as well.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900