2015-11-12 22 views
2

我正在嘗試編寫一個需要通過函數進行輸入驗證的程序。背後的想法很像21石,只有13石,而且電腦總是贏。遊戲以13個寶石開始,並且電腦在第一回閤中總是選擇1,創建4個場景的倍數。這意味着,如果用戶需要3臺電腦需要1臺,用戶需要2臺電腦需要2臺,直到沒有石塊留下。我的問題是我很難得到我的頭部功能以及如何從參數中調用數據,因此任何幫助都將不勝感激!C++中的函數 - 13 Sone遊戲

這是我的沙發。

#include <iostream> 
using namespace std; 
//function prototypes 
bool validPick(int numStones); 
int computerPick(int stones_in_pile, int player2taken); 
int playerPick(int stones_in_pile); 

int main() 
{ 
    int stones_left = 13, P1Taken, P2Taken; 

    cout << "You have shosen to play the game 13 stones against me, the MIGHTY " 
     << "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from" 
     << " the pile on your turn.\nThe player that removes the last stone " 
     << "or stones from the pile wins the game.\nGood Luck... You will need" 
     << " it! I NEVER LOOSE!!" 
     << endl << endl; 

    computerPick(stones_left, P2Taken); 
    playerPick(P1Taken); 
    validPick(stones_left); 

    //game logic here -- This is far from done. 
    stones_left -= P1Taken; 
    stones_left -= P2Taken; 
    return 0; 
} 
/******************************************************************************\ 
* Validate the picked number 1-3 are only valid numbers to choose from.  * 
\******************************************************************************/ 
bool validPick(int numStones) 
{ 
    if((numStones < 1) || (numStones >3)) 
     cout << "Invalid Selection. 1-3 is all you can have!"; 
    else 
     return numStones; 
} 
/******************************************************************************\ 
* Computer's function calls. Should start with 1. We always want the computer * 
* to win the game.                * 
\******************************************************************************/ 
int computerPick(int stones_in_pile, int player2taken) 
{ 
    if(player2taken == 0) 
     stones_in_pile -= 1; 
    else 
    { 
     if(player2taken == 1) 
      stones_in_pile -= 3; 
      else 
       if(player2taken == 2) 
        stones_in_pile -= 2; 
         else 
          stones_in_pile -=1; 
    } 
    return stones_in_pile; 

} 
/******************************************************************************\ 
* Player's Pick function call goes here. The player goes second    * 
\******************************************************************************/ 
int playerPick(int stones_in_pile) 
{ 
    cout << "Please choose the ammount of stones. 1-3 only! : "; 
    cin >> stones_in_pile; 
    return stones_in_pile; 
} 
+1

*我的問題是我很難得到我的頭部函數以及如何從*中的參數調用數據 - 那麼爲什麼不寫一個調用函數並從函數返回的簡單示例,然後熟悉它?這就是任何C++程序員如何接近C++的一個新的或未知的方面 - 簡單程序首先被寫入,然後被學習,然後*新技術被用在實際程序中。 – PaulMcKenzie

回答

0

儘管你應該更好地閱讀初學者的書要比理解C++問這樣的問題,我會盡量解釋通過一個例子什麼是錯在你的代碼:

bool validPick(int numStones) { 
    if((numStones < 1) || (numStones >3)) 
     cout << "Invalid Selection. 1-3 is all you can have!"; 
    else 
     return numStones; 
} 

聲明該函數返回值爲bool。但是,如果if子句中的條件成立,該函數不會返回任何內容,這是一個錯誤。其次,numStonesint,所以當你將它作爲bool返回時,它將被轉換(從intbool),這可能不是你想要的。老實說,我沒有甚至試圖理解你的程序的邏輯,但這個功能的有效版本看起來是這樣的:

bool validPick(int numStones) { 
    if((numStones < 1) || (numStones >3)) { 
     cout << "Invalid Selection. 1-3 is all you can have!"; 
     return false; 
    } 
    return true; 
} 
0

有與生產值,並將這些值是如何傳遞迴功能很多理念。

函數可以通過修改參數或返回一個值來將值傳遞給調用者。

playerPick函數可以修改傳遞的值(通過引用傳遞):

void playerPick(int& stones_in_pile) 
{ 
    cout << "Please choose the ammount of stones. 1-3 only! : "; 
    cin >> stones_in_pile; 
} 

或者通過返回值:

int playerPick(void) 
{ 
    // Local variable to temporarily hold a value. 
    int stones_in_pile = - 1; 

    cout << "Please choose the ammount of stones. 1-3 only! : "; 
    cin >> stones_in_pile; 
    return stones_in_pile; 
} 

注意,後者版本使用本地臨時,變量,編譯器將在函數結束時返回值的一個副本。
我在參數中使用void來強調這裏。

+0

@ tobi303:感謝您的評論,錯字。我糾正了它。 –