Problem when saving in snake game

1

Hi, I'm new to this c ++ and I was doing a simple snake game I already have almost everything complete the only problem esque I want to do when I lose save the scores and names the file is already created but when the game starts the save function starts and does not let play

this is the functions and the main to not put as much code:

bool record(){
string playerName; 
char option;
string display;

std::ofstream myfile("snake.txt", std::ofstream::out | std::ofstream::app);

system("cls");


cout<<"Ingrese su nombre: "<<endl;
cin>>playerName;

myfile<<playerName<<" "<<puntos<<endl;

myfile.close();

do{


cout<<"Quieres ver los records pasados? (y/n)"<<endl;
cin>>option;

switch(option){
    case 'y':
        ifstream myfile ("snake.txt");
        if (myfile.is_open()){
            system("cls");

            while ( getline (myfile,display) ){

                  cout << display << '\n';
            }
            system("pause");
            myfile.close();
        }
    break;  
}
}while(option != 'n');

}

bool game_over()
{
 if(y == 3 || y == 23 || x == 2 || x == 77)
 return false;
 for(int j = tam - 1; j > 0; j--){
         if(cuerpo[j][0] == x &&  cuerpo[j][1] == y){
         return false;
         }
 }
 if(y != 3 || y != 23 || x != 2 || x != 77){
    record();
 }
 return true;         
 }

Main:

int main()
{
OcultaCursor();

pintar();
gotoxy(xc, yc); printf("%c", 4);

while(tecla != ESC && game_over())
{
     borrar_cuerpo();
     guardar_posicion();
     dibujar_cuerpo();
     comida();
     teclear();
     teclear();


     if(dir == 1) y--;
     if(dir == 2) y++;
     if(dir == 3) x++;
     if(dir == 4) x--;

     Sleep(velocidad);
}

cout<<puntos<<endl;
    if(game_over()){
    record();
}

pintar();
return 0;
}

I hope you can help me. Thanks

    
asked by user29916 13.02.2017 в 21:53
source

1 answer

4

Values x , y , and dir where you start the game?

You do

if (y == 3 || y == 23 || x == 2 || x == 77)
    return false;

...

if (y != 3 || y != 23 || x != 2 || x != 77) {
    record();
}

Unless you start in x == 2 || x == 77 or y == 3 || y == 23 , you will enter the second if( ) , with what you will save. After saving, you continue the while( ) , so you call back game_over( ) . Where you check again, and you can continue in that loop forever. It depends on how you start x , y and dir .

EDITO

According to the new data, you will repeat the saved loop forever.

At the start of the game, every call to game_over( ) , y != 23 is fulfilled always . In the main loop, you make

 if(dir == 3) x++;

You change the value of x , but y remains the same.

You should examine the internal logic of game_over( ) . However, since you say it works, a possible solution is to use an auxiliary variable.

For example int started = 0 . You set it to 1 in main( ) , within while( ) . And you change the second if( ) within game_over( ) .

if( started && ( y != 3 || y != 23 || x != 2 || x != 77 ) )
  record();

return true;

With those changes, it should work.

    
answered by 13.02.2017 в 22:13