Help with breaking out of loops

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1606 Views - Last Post: 23 November 2012 - 06:53 AM Rate Topic: -----

#1 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Help with breaking out of loops

Posted 17 November 2012 - 08:29 AM

Hi there I'm making my first Tic Tac Toe game with C++ and SDL.

I've got the game working all the way up to it displaying who won.

My problem is it still checks to see whos won after the screen comes up saying 'X' won, which makes the image flash and stops the game from resetting.

Thanks in advance.

SDL_Event event;

void CApp::OnLoop(){

    int emptytiles = 0;
    bool Finished = true;

    for(int j = 0; j < 9; j++){
        if(Grid[0] == GRID_TYPE_O && Grid[1] == GRID_TYPE_O && Grid[2] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else if(Grid[0] == GRID_TYPE_X && Grid[1] == GRID_TYPE_X && Grid[2] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

         if(Grid[3] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else if(Grid[3] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

         if(Grid[6] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else if(Grid[6] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

         if(Grid[0] == GRID_TYPE_O && Grid[3] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else if(Grid[0] == GRID_TYPE_X && Grid[3] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

         if(Grid[1] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else if(Grid[1] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

         if(Grid[2] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else if(Grid[2] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

        if(Grid[0] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else if(Grid[0] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

         if(Grid[2] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;

        }else  if(Grid[2] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            Finished = false;
            goto Finished;
        }

        //Draw
       for(int k = 0; k <9; k++){
            if(Grid[k] == GRID_TYPE_NONE){
                emptytiles++;
            }
        }

        if(emptytiles == 0){
            Surf_Display  = SDL_SetVideoMode(200, 200, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
            CSurface::OnDraw(Surf_Display, Surf_Draw, 200, 200, NULL, NULL, 200, 200);
            SDL_Delay( 5000 );
        }

        Finished:
        if(Finished == false){
            //If there's an event to handle
            if( SDL_PollEvent( &event ) )
            {
                //If a key was pressed
                if( event.type == SDL_KEYDOWN ){
                Reset();
                continue;
                }
            }
        }


    }
SDL_Flip(Surf_Display);



}




Is This A Good Question/Topic? 0
  • +

Replies To: Help with breaking out of loops

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,069
  • Joined: 10-May 07

Re: Help with breaking out of loops

Posted 17 November 2012 - 08:44 AM

I wouldn't use gotos in C, I would use functions. Then you code your conditionals to take action.

Start Game :
While(!Winner) {
  Player Moves :
  PC Moves :
  Check for Winner :
}
Display Winner


Was This Post Helpful? 0
  • +
  • -

#3 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 17 November 2012 - 09:15 AM

Ok I tried replacing my final if with a while loop that runs when Finished equals false, but now it won't let me place the final 'X' or 'O' it just freezes the game.

Very strange.
Was This Post Helpful? 0
  • +
  • -

#4 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 17 November 2012 - 09:37 AM

I've changed the code slightly so that when a player wins it calls a function, which checks for the key input to reset the board, but it still continues to flicker the 'X' won image.

What should I try?

new function call
if(Grid[0] == GRID_TYPE_O && Grid[1] == GRID_TYPE_O && Grid[2] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();

new reset check function
void CApp::onfinished(){
    if( SDL_PollEvent( &event ) ){
        //If a key was pressed
        if( event.type == SDL_KEYDOWN ){
        Reset();
        }
    }
}


This post has been edited by Bugsy6: 17 November 2012 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 18 November 2012 - 08:50 AM

So after trying a return;, break; and other options I still can't get out of the check whose won loop.

I think I need a while loop but can't figure one out.

How can I simply stop the loop from checking over and after after someone has won?
Was This Post Helpful? 0
  • +
  • -

#6 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 18 November 2012 - 10:54 AM

Ok so I added a while loop, but now we I run the program nothing but a black screen appears.

Any advice?

#include "CApp.h"



void CApp::OnLoop(){

int emptytiles = 0;
bool winner = false;

while(winner != true){
    for(int j = 0; j < 9; j++){
        if(Grid[0] == GRID_TYPE_O && Grid[1] == GRID_TYPE_O && Grid[2] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();
            winner = true;



        }else if(Grid[0] == GRID_TYPE_X && Grid[1] == GRID_TYPE_X && Grid[2] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

         if(Grid[3] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }else if(Grid[3] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

         if(Grid[6] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();



        }else if(Grid[6] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

         if(Grid[0] == GRID_TYPE_O && Grid[3] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();



        }else if(Grid[0] == GRID_TYPE_X && Grid[3] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

         if(Grid[1] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();



        }else if(Grid[1] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

         if(Grid[2] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();



        }else if(Grid[2] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

        if(Grid[0] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();



        }else if(Grid[0] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

         if(Grid[2] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            onfinished();



        }else  if(Grid[2] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);
            onfinished();


        }

        //Draw
       for(int k = 0; k <9; k++){
            if(Grid[k] == GRID_TYPE_NONE){
                emptytiles++;
            }
        }

        if(emptytiles == 0){
            Surf_Display  = SDL_SetVideoMode(200, 200, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
            CSurface::OnDraw(Surf_Display, Surf_Draw, 200, 200, NULL, NULL, 200, 200);
            onfinished();

        }



    }
}


SDL_Flip(Surf_Display);


}

void CApp::onfinished(){
    SDL_Event event;


    if( SDL_PollEvent( &event ) ){
        //If a key was pressed
        if( event.type == SDL_KEYDOWN ){
        Reset();
        }
    }
}







Was This Post Helpful? 0
  • +
  • -

#7 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 19 November 2012 - 06:29 AM

Ok so I've tried a few more things, here is my new while loop.

This now runs and I can lace noughts and crosses but it doesn't display the won image when someone wins.

Just realised I put the wrong conditions in the while, so when I change them I return to the black screen issue.

void CApp::OnLoop(){

int emptytiles = 0;
int Winner = 0;


while(Winner == 1){
    for(int j = 0; j < 9; j++){
        if(Grid[0] == GRID_TYPE_O && Grid[1] == GRID_TYPE_O && Grid[2] == GRID_TYPE_O){
            CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
            Winner = 1;

        }else if(Grid[0] == GRID_TYPE_X && Grid[1] == GRID_TYPE_X && Grid[2] == GRID_TYPE_X){
            CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);

        }
        // more winner checks

      //Draw
       for(int k = 0; k <9; k++){
            if(Grid[k] == GRID_TYPE_NONE){
                emptytiles++;
            }
        }

        if(emptytiles == 0){
            Surf_Display  = SDL_SetVideoMode(200, 200, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
            CSurface::OnDraw(Surf_Display, Surf_Draw, 200, 200, NULL, NULL, 200, 200);


        }



    }
}

SDL_Event event;


    if( SDL_PollEvent( &event ) ){
        //If a key was pressed
        if( event.type == SDL_KEYDOWN ){
        Reset();
        }
    }

SDL_Flip(Surf_Display);


}




This post has been edited by Bugsy6: 19 November 2012 - 06:31 AM

Was This Post Helpful? 0
  • +
  • -

#8 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 19 November 2012 - 07:55 AM

Why does adding a while loop make the program hang on a black screen?

Could someone please take a look, I've read it's to do with an infinite loop, but mine is not infinite it waits for a change from false to true. Which it gets when someone wins.
Was This Post Helpful? 0
  • +
  • -

#9 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 21 November 2012 - 02:47 PM

If someone could just have a quick lok and point me in the right direction that would be great.

Would love to finish my first project.
Was This Post Helpful? 0
  • +
  • -

#10 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Help with breaking out of loops

Posted 21 November 2012 - 03:08 PM

You set winner = 0.

so winner is zero.


Then you immediately have a while loop with the condition that it will be entered ONLY if winner equals one.

So your program never goes inside the while loop. Perhaps winner should be a variable that is brought into your function, as a parameter?
Was This Post Helpful? 0
  • +
  • -

#11 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 21 November 2012 - 03:42 PM

Thanks for the response, but I have changed the code including that part, but it still has a black screen when I run it.

Here it is:

bool Winner = false;

void CApp::OnLoop(){

int emptytiles = 0;


    while(Winner == false){
        for(int j = 0; j < 9; j++){
            if(Grid[0] == GRID_TYPE_O && Grid[1] == GRID_TYPE_O && Grid[2] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);
                SDL_Flip(Surf_Display);
                onfinished();



            }else if(Grid[0] == GRID_TYPE_X && Grid[1] == GRID_TYPE_X && Grid[2] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[3] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);




            }else if(Grid[3] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[6] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[6] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[0] == GRID_TYPE_O && Grid[3] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[0] == GRID_TYPE_X && Grid[3] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[1] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[7] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[1] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[7] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[2] == GRID_TYPE_O && Grid[5] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[2] == GRID_TYPE_X && Grid[5] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

            if(Grid[0] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[8] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);





            }else if(Grid[0] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[8] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }

             if(Grid[2] == GRID_TYPE_O && Grid[4] == GRID_TYPE_O && Grid[6] == GRID_TYPE_O){
                CSurface::OnDraw(Surf_Display, Surf_Owon, 200, 200, NULL, NULL, 200, 200);




            }else  if(Grid[2] == GRID_TYPE_X && Grid[4] == GRID_TYPE_X && Grid[6] == GRID_TYPE_X){
                CSurface::OnDraw(Surf_Display, Surf_Xwon, 200, 200, NULL, NULL, 200, 200);




            }
        }

            //Draw
           for(int k = 0; k <9; k++){
                if(Grid[k] == GRID_TYPE_NONE){
                    emptytiles++;
                }
            }

            if(emptytiles == 0){
                CSurface::OnDraw(Surf_Display, Surf_Draw, 200, 200, NULL, NULL, 200, 200);


            }




    }
}



void CApp::onfinished(){
    SDL_Event event;
    Winner = true;

        if( SDL_PollEvent( &event ) ){

            //If a key was pressed
            if( event.type == SDL_KEYDOWN ){
                Reset();
            }
        }
}




Was This Post Helpful? 0
  • +
  • -

#12 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Help with breaking out of loops

Posted 21 November 2012 - 08:11 PM

Why are you putting all these if Grid[0] && Grid[1] ... type statements into this function, along with display code?

Do one thing in the function, and make it clean and clear:

isLegal() - has the entered move been made to an empty square?
isWon() - has the last move resulted in the end of the game?
showGrid() - print the current Grid

etc.

A CApp::OnLoop() function just tells me nothing about what you are doing here. Descriptive names for your functions and variables are a big help - like you have done for "Winner". That's what you want to do, right there.

I can't help you with the display stuff, but remove the if() statements until you've simplified away whatever is causing your display not to happen. That will be your tripwire to show you what was the problem, all along.
Was This Post Helpful? 0
  • +
  • -

#13 Bugsy6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: Help with breaking out of loops

Posted 22 November 2012 - 05:08 PM

All of the ifs and Grid[0] && Grid[1] are to work out whos the winner. They check the 9 grid positions for sets of three matches for either player to be a winner and then displays a different image for each player winning or a draw. At the moment my beginner c++ brain can't do this in a better way, but I will learn.

The problem at the moment is that the code runs fine I can get a winner and reset the board, but the winners image flashes on the screen and I think that is because the if loops are still checking for a winner.

So I added the while loop to try and break out of the if loops when a winner is found, but the while loop just causes a black screen. If I rmove the while the game works again.

So any help on that would be great, Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1331
  • View blog
  • Posts: 4,565
  • Joined: 19-February 09

Re: Help with breaking out of loops

Posted 22 November 2012 - 07:25 PM

Perhaps you could show the CApp class.

There is a basic CApp class here - SDL Tutorial Basics.


If you have a winner flag and variable to hold which player in the class, it can be set in the loop function and you can draw that information in a draw/render function.

class CApp 
{
  bool winner_found;
  int  winning_player;


Was This Post Helpful? 0
  • +
  • -

#15 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Help with breaking out of loops

Posted 22 November 2012 - 07:33 PM

Display shouldn't be inside a while loop - that's what encompasses the entire game.

Each function - or at least each block of code within a function, should do ONE thing, and clearly. Don't mix multiple threads of logic - say display and who the winner is. Find out if there is a winner, THEN in another block of several statements, display the winner's image.

So it should be:
IF a function tells you the game is over
   then show the winner's image.
   (and before the next move you will leave the game playing loop, as the 
    while test is made)
else 
   show the grid so the game can continue
   


There is no "while winner==false" anything logic here - the while winner==false loop is in the primary game loop, only.

It's like a rear axle on a car, or a steering wheel - there should be only ONE of each, per car. You're adding an extra one to the car, and of course, you could drive it, but it's twice as difficult to get it right.

This post has been edited by Adak: 22 November 2012 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2