2017-05-09 30 views
-5

所以基本上即時工作的戰艦的任務主要功能在一個循環中調用火災功能。在這個循環中,每個玩家都會調用一次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; 

     } 

    } 


} 
+5

男人,這段代碼太長了。我們並不需要所有這些,只需要給我們最低限度的幫助 – Xyzk

+2

......確保它符合[MCVE]的定義。 –

+0

當玩家2輸入10 10時,你忘記了'返回'。只是避免重複代碼的另一個例子。做出任何改變意味着要記住在任何其他地方做出相同的改變。 –

回答

0

我會盡力回答這個問題。這聽起來像你的winner_test_function只是檢查一個球員的命中數量是有道理的。關於循環條件的第二個問題對我來說有點困惑,如果我理解它是有道理的,但這不是我將如何處理這個問題。基本上你說,遊戲可以結束的唯一方式是如果player_hits等於23或用戶鍵入10 10.我想知道爲什麼你希望用戶能夠通過輸入內容來結束程序?如果玩家擊中所有船隻,該程序是否應該結束?我可能錯過了一些東西,但這似乎就是你說的。如果你希望他們能夠通過輸入10 10來結束程序,那麼你的行爲將會起作用,但是像@Andrieux說的那樣,如果因爲缺少返回語句而導致 if語句不起作用。

should be end_game = false; 
return; 

附註:爲什麼要發表類似cout << "Player 2- enter your move" << " " << "(ie. 0 4)";的陳述。 << " " <<部分是無關緊要的。只要把它寫成cout << "Player 2- enter your move (ie. 0 4)";