C++中的函数-13 Sone游戏
Functions in C++ - The 13 Sone game
我正在尝试编写一个程序,该程序需要通过函数进行输入验证。它背后的想法很像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,则函数不会返回任何内容,这是一个错误。其次,numStones
是int
,所以当您将其作为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
进行强调
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 提高基于组件的游戏引擎的效率
- C++中的函数-13 Sone游戏