所以基本上即時工作的戰艦的任務主要功能在一個循環中調用火災功能。在這個循環中,每個玩家都會調用一次fire函數。每次執行此功能後,遊戲板參數都會發生變化(請參閱上文)以反映玩家移動的結果。此外,主要是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;
}
}
}
男人,這段代碼太長了。我們並不需要所有這些,只需要給我們最低限度的幫助 – Xyzk
......確保它符合[MCVE]的定義。 –
當玩家2輸入10 10時,你忘記了'返回'。只是避免重複代碼的另一個例子。做出任何改變意味着要記住在任何其他地方做出相同的改變。 –