C++ 如何修复循环条件?

C++ 如何修复循环条件?,c++,C++,所以基本上我在为战舰分配任务,主函数在循环中调用fire函数。在这个循环中,每个玩家调用一次fire函数。每次执行此功能后,游戏板参数都会发生变化,如上所示,以反映玩家移动的结果。此外,主要必须评估end_game参数中的值,以确定玩家是否已指示游戏应终止。此评估还必须包括确定玩家是否赢得了游戏,即no_hits参数等于23 还有一个叫做winner_测试函数的函数 主函数必须使用winner_测试函数来确定是否有赢家。为此,winner_test函数仅确定no_hits_player_1或no

所以基本上我在为战舰分配任务,主函数在循环中调用fire函数。在这个循环中,每个玩家调用一次fire函数。每次执行此功能后,游戏板参数都会发生变化,如上所示,以反映玩家移动的结果。此外,主要必须评估end_game参数中的值,以确定玩家是否已指示游戏应终止。此评估还必须包括确定玩家是否赢得了游戏,即no_hits参数等于23

还有一个叫做winner_测试函数的函数 主函数必须使用winner_测试函数来确定是否有赢家。为此,winner_test函数仅确定no_hits_player_1或no_hits_player_2是否等于23。每个板块上有23个包含船只的位置。因此,当两个棋盘都有23支安打时,游戏就赢了,程序应该结束了

我的问题是winner test函数是必需的,在我的循环中,继续迭代的条件是end_game参数是否为true如果为false这意味着用户接受了应该结束循环的值10我的问题是我的循环条件错误,如果是,我如何修复我的代码如下

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
typedef char gameBoard[10][10];
using namespace std;
const int MAX_ROWS = 10;
const int MAX_COLS = 10;
void get_board_data(ifstream&, gameBoard, gameBoard);
void fire(gameBoard, int&, bool&);
void print_board(gameBoard);
bool winner_test(int, int);
int currentplayer = 1;


int main()
{
    int no_hits;
    bool end_game = true;
    ifstream myfile;
    myfile.open("infile.txt");
    gameBoard Player1;
    gameBoard Player2;
    get_board_data(myfile, Player1,Player2);
    //The main function calls the fire function in a loop.  Inside this loop the fire function is called once for each player.
    while (end_game==true) {
        fire(Player2, no_hits, end_game);
        if (end_game == false)
        {
            break;
        }
        fire(Player1, no_hits, end_game);

    }

}


void fire(gameBoard player, int& no_hits, bool& end_game)

{
    int row;
    int col;
    static int player1_hits=0;
    static int player2_hits=0;
    if (currentplayer == 1)
    {
        cout << "Player 1 hits:" << " " << player1_hits << " "<< "player2  hits:" << " " << player2_hits << endl;
        cout << "Player 1 - enter your move" << " " << "(ie. 0 4)";
        cin >> row >> col;
        if (row == 10 && col == 10)
        {
             end_game = false;
             return;
        }

        if (player[row][col] == '#') {
            cout << "hit!!!!!" << endl;
            player[row][col] = 'H';
            player1_hits++;
            currentplayer = 2;
        }
        else if (player[row][col] == '-')
        {
            cout << "miss!!!" << endl;
            player[row][col] = '.';

        }
        currentplayer = 2;

    }
    else if (currentplayer == 2)
    {
        cout << "Player 2- enter your move" << " " << "(ie. 0 4)";
        cin >> row >> col;
        if (row == 10 && col == 10)
        {
            end_game = false;
        }

        if (player[row][col] == '#') {
            cout << "hit!!!!!" << endl;
            player[row][col] = 'H';
            player2_hits++;
            currentplayer = 1;
        }
        else if (player[row][col] == '-')
        {
            cout << "miss!!!" << endl;
            player[row][col] = '.';
            currentplayer = 1;

        }

    }


}

我会尽力回答这个问题。听起来你的winner_test_功能只是检查玩家的点击量,这是有意义的。关于循环条件的第二个问题让我有点困惑,如果我理解的话,这是有意义的,但这不是我如何处理这个问题。基本上你说游戏结束的唯一方法是玩家点击数等于23或用户输入10。我想知道为什么您希望用户能够通过在中输入内容来结束程序?难道程序不应该只在玩家命中所有飞船时结束吗?我可能遗漏了什么,但你似乎就是这么说的。如果您希望他们能够通过输入10结束程序,那么您所做的将起作用,但就像@Andrieux所说的,在currentPlayer==2if语句中它不起作用,因为您缺少return语句

should be end_game = false;
return;

旁注:为什么你要像cout Man这样声明,这个代码太长了。我们不需要所有这些,只要给我们最低限度的必要帮助你。。。确保它符合a的定义。当玩家2输入10时,看起来你忘记了返回。这只是另一个例子,说明了为什么应该避免重复代码。进行任何更改意味着记住在任何数量的其他地方进行相同的更改。调试器。使用调试器。调试器将帮助您单独执行每条语句并观察变量的值。请用调试会话的结果编辑帖子。