gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:28 26 4
gpt4 key购买 nike

我正在尝试编写一个需要通过函数进行输入验证的程序。它背后的想法很像 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 子句中的条件结果为真,则该函数不返回任何内容,这就是一个错误。其次,numStones 是一个 int,因此当您将它作为 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;
}

关于c++ - C++ 中的函数 - 13 Sone 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33675935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com