C ++ While循环,如果数组等于零则停止

时间:2014-09-28 18:24:41

标签: c++ arrays while-loop

我的游戏有点问题。我想要的是当两个数组中的一个变为全0时,循环将停止。目前,当两个数组都等于零时,循环停止。

我认为问题但没有解决方案是我在一个循环中有两个数组语句,如果array1(border1)已经全部为0,它将从顶部开始运行到底。 你怎么想?

void ShootAtShip(int board1[], int board2[], string names[], int cap) {
    const int hit = 0;
    int shot = 0;
    bool won = false;
    int temp;

    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;
            temp = 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;
            }
        }
    }

    cout << "Testing is while loop stops";
}

2 个答案:

答案 0 :(得分:1)

关键是你必须在每次循环迭代时检查整个电路板的状态。像这样:

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

for (int i = 0; i < cap; i++) 
{
    while ( 1 )
    {
       bool board1HasShips = false;
       bool board2HasShips = false;
       for ( int j = 0; j < cap; j++ ) 
       { 
          if ( board1[j] != 0 ) 
          { 
             board1HasShips = true;
             break;
          }
       }
       for ( int j = 0; j < cap; j++ ) 
       { 
          if ( board2[j] != 0 ) 
          { 
             board2HasShips = true;
             break;
          }
       }

       if ( !board1HasShips || !board2HasShips ) break; 

       // past this point we know that both boards have ships.
       // shoot at ships
    }
}

}

答案 1 :(得分:0)

特别是在编写游戏时,尝试将代码组织到函数中是一个好主意。 就个人而言,我会做这样的事情:

bool isGameOver(int board1[],  int board2[], size_t cap)
{
    bool lost1 = true;
    bool lost2 = true;
    for (size_t i = 0; i < cap && lost1 != false; ++i)
        if (board1[i] != 0)
            lost1 = false;
    if (lost1)
        return true;
    for (size_t i = 0; i < cap && lost2 != false; ++i)
        if (board2[i] != 0)
            lost2 = false;
    return lost2;
}

然后使用它作为条件来打破循环。

因为你使用的是c ++,为什么不将这些板抽象成一个类?这将允许您存储信息,例如每块板上剩余的船数。

另外,请考虑在c ++中使用std::array类模板,这样您就不必单独传递数组大小,并尝试使用size_tstd::size_t进行数组索引。< / p>

相关问题