如何修复循环条件?

时间:2017-05-09 15:09:27

标签: c++

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

但它们也是一个叫做winner_test函数的函数 主要功能必须使用winner_test功能来确定是否还有赢家。要做到这一点,winner_test函数只是确定no_hits_player_1或no_hits_player_2是否等于23.每个板上有23个位置包含船只。因此,当两个棋盘都有23次点击时,游戏已经赢了,程序应该结束。

我的问题是必要的胜者测试函数,并且在我的循环中,保持迭代的条件是如果end_game参数为真,如果它为假则表示用户接受值10 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;

        }

    }


}

1 个答案:

答案 0 :(得分:0)

我会尽力回答这个问题。听起来你的winner_test_function只是检查玩家所做的点击量是否合理。关于循环条件的第二个问题对我来说有点混乱,如果我理解它是有道理的,但它不是我如何解决这个问题。基本上你的说法是游戏结束的唯一方式是player_hits等于23或用户输入10 10.我想知道为什么你希望用户能够通过以下方式结束程序进入的东西?如果玩家击中所有船只,该程序是否应该结束?我可能会遗漏一些东西,但看起来这就是你的说法。如果你希望他们能够通过输入10 10来结束程序,那么你的工作会起作用,但是像@Andrieux说它不会在currentPlayer == 2 if语句中工作,因为你缺少return语句。

should be end_game = false;
return;

附注:为什么要发表cout << "Player 2- enter your move" << " " << "(ie. 0 4)";之类的陈述。 << " " <<部分无关紧要。只需将其写为cout << "Player 2- enter your move (ie. 0 4)";

即可