mercredi 6 février 2019

Bug in randomly generated ships for a battleship game prototype, not overlapping ships

I'm working on a battleship game and at the moment at getting the basic grid done. Made an 11x11 matrix filled with dots and at the margins with coordinates from 0 to 9. The version I'm making has one ship of 5 units length, one of 4, two of 3 and one of two. Each ship is one straight line.

The problem code is generating the ships so that they don't overlap, so what I thought of doing is checking in advance with a while loop that the coordinates generated (but not placed on the matrix yet) won't collide with another after they are placed (the checking is done before the placing) and in case they will, other random values are generated until one good one is found.

At the moment I only have the 5 length one done and trying to generate the 4th one so it doesn't overlap with the first.

I'm having the ships have the 254 ASCII character (that gray square).

The code didn't work, so I added tons of couts to see where the problem actually is, and it's because it doesn't pass an if statement that checks if the current character in the matrix is 254.

I checked spelling, curly brackets, everything, but I just don't get what the problem is.

The important part of the code is this:

ship4=rand()%4; //at what coordinate does the ship start generating, same axis as the ship's verticalness/horizontalness
vorh=rand(); //vorh means 'vertical or horizontal?', v is even and h is uneven
position=rand()%9; //the opposite vertical/horizontalness position of the ship, so it can be more than the ship's size

int OK=1; //memorizes whether the generated ship collides with another one, 0 means it's "ok" to continue

while(OK==1)
{
    OK=0;
    if(vorh%2==0) 
    {
        for(i=ship4; i<=ship4+4-1; i++)
        {
            cout<<i<<position; //this one is the cout for debugging purposes
            if( v[i][position]==254 )
            {
                cout<<"first for"<<endl; //another useless cout
                OK=1;
            }
            else
                cout<<"first else"<<endl; //this else can even not be here
        }

    }
    else
    {
        for(i=ship4; i<=ship4+4-1; i++)
        {
            cout<<i<<position;
            if( v[position][i]==254 )
            {
                cout<<"second for"<<endl;
                OK=1;
            }
            else
                cout<<"second else"<<endl;
        }


    }

    if(OK==1)
    {
        ship4=rand()%4;
        vorh=rand();
        position=rand()%9;
    }

}

The whole code is here in case you want to see it: https://gist.github.com/Lastrevio112/a9556b1253d76fcf820ed1fd6357900b#file-gistfile1-txt

I never get "first for" or "second for" on my screen and always get the "first else" or "second else". The cout<<i<<position line executes though, so the code is getting through the for loop, just not recognizing that if statement as true.




Aucun commentaire:

Enregistrer un commentaire