简体   繁体   中英

C++ “or” statement in a While loop mixes things up

I'm facing a little problem, it appears that the While loop won't stop either when the arrays board1[i] or board2[i] only contains 0's.

So is it right to write while ((board1[i] == 0) || (board2[i] == 0)) because what I want is when Some of the boards only contains 0's I want the loop too stop.

void ShootAtShip(int board1[], int board2[], string names[], int cap){

const int hit = 0;
int shot = 0;

for (int i = 0; i < cap; i++){

    while ((board1[i] == 0) || (board2[i] == 0)){ //detects if any board has all their ships shot down



        cout << names[1] << " set a position to shoot." << endl;
        cin >> shot;

        while ((shot >= cap) || (shot < 0)) {       //detects if the number is allowed

            cout << "That number is not allowed, "<<  names[1] << " set a position to shoot." << endl;
            cin >> shot;

        }


        if (board1[shot] != 0){

            board1[shot] = 0;
            cout << "Hit!" << endl;
        }

        else{

            cout << "You missed." << endl;
        }

        shot = 0;


        cout << names[0] << " set a position to shoot." << endl;
        cin >> shot;

        while ((shot >= cap) || (shot < 0)) {       //detects if the number is allowed

            cout << "That number is not allowed, " << names[0] << " set a position to shoot." << endl;
            cin >> shot;

        }

        if (board2[shot] != 0){

            board2[shot] = 0;
            cout << "Hit!" << endl;
        }

        else{

            cout << "You missed." << endl;
        }



    }

what I want is when Some of the boards only contains 0's I want the loop too stop.

I suppose you wrote exactly the opposite of what you want:

while ((board1[i] == 0) || (board2[i] == 0))

the above will run if board1[i] is equal to zero OR if board2[i] is equal to zero . If you want to stop it if either of the two is zero, you should rather write

while (!((board1[i] == 0) || (board2[i] == 0)))

notice the ! (not) at the beginning. Similarly you could also write

while (board1[i] != 0 && board2[i] != 0)

just write instead of

while ((board1[i] == 0) || (board2[i] == 0))

write if you want that when two board will need to be 0 at the same time

while (board1[i] != 0 && board2[i] != 0)

otherwise if one of the board will need to be 0 for exit then

while (board1[i] != 0 || board2[i] != 0)

What you're saying is if either board has a zero keep going. What you want to say is until one board has no ones, keep going.

for (int i = 0; i < cap; i++){

while ((board1[i] == 0) || (board2[i] == 0)){ //ACTUALLY DETECTS IF EITHER 
//BOARD HAS A ZERO AT INDEX i 

This segment is wrong. You're starting at index zero and checking to see if EITHER board1 OR board2 has a 0 in that index, then you do the while loop stuff UNTIL board1[i] != 0 && board2[i] != 0. Which means you'll never stop looping once you find an index with a zero, meaning the game won't end.

What you need to do is instead go through each array individually, see if either meets a condition then decide what to do. Personally? I'd look for a one in both arrays. Not all zeroes in either array. That way you aren't looking for a reason to stop. Make your program lazy: Stop unless there's a reason to continue.

Something along the lines of the following to see if either is all zeroes:

bool boardOneAllZeroes = false;
bool boardTwoAllZeroes = false;
//So if board one is all zeroes OR board two is all zeroes, stop looping.
while(!boardOneAllZeroes && !boardTwoAllZeroes)
{
    boardOneAllZeroes = true;
    boardTwoAllZeroes = true;
    //The above two lines basically say "This loop isn't going to keep 
    //going unless you give me a good reason to later on.
    //Next we go through each index in both arrays looking for a one.
    for(int i = 0; i < cap; i++)
    {
        //If we find a one in board one, then board one is not all zeroes. 
        //Set it back to false
        if(board1[i] == 1)
        {
             boardOneAllZeroes = false;
        }
        //Same thing with board two.
        if(board2[i] == 1)
        {
             boardTwoAllZeroes = false;
        }
    }
    //Check to make sure both still have a one, because we don't need to keep going 
    //if both are all zeros.
    if(!boardOneAllZeroes && !boardTwoAllZeroes)
    {
        do game things
    }
}

Essentially, you start by saying at each iteration "Until I'm given a reason to think otherwise, the boards are all zeroes". Then seek to prove the boards AREN'T all zeroes. Then if either is all zeroes then don't do anything and stop the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM