C++中的函数-13 Sone游戏

Functions in C++ - The 13 Sone game

本文关键字:Sone 游戏 函数 C++      更新时间:2023-10-16

我正在尝试编写一个程序,该程序需要通过函数进行输入验证。它背后的想法很像21块石头,只有13块,电脑总是赢的。游戏以13块石头开始,计算机在第一回合总是选择1,创建4的倍数场景。这意味着,如果用户拿3台电脑拿1台,用户拿2台电脑拿2台,以此类推,直到没有石头残留为止。我的问题是,我很难理解函数以及如何从中的参数调用数据,所以如果能提供任何帮助,我们将不胜感激!

这就是我的沙发。

#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;
}

尽管你应该读一本初学者的书,而不是通过问这样的问题来理解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子句中的条件为true,则函数不会返回任何内容,这是一个错误。其次,numStonesint,所以当您将其作为bool返回时,它将被转换(从int转换为bool),这可能不是您想要的。老实说,我甚至没有试着理解你程序的逻辑,但这个函数的有效版本可能是这样的:

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

函数产生值以及这些值是如何传递回来的,有很多哲学。

函数可以通过修改参数或返回值将值传递回调用方。

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进行强调