C++ C+中的函数+;-13人游戏
我试图编写一个程序,需要通过函数进行输入验证。它背后的想法很像21块石头,只有13块,电脑总是赢。游戏以13块石头开始,电脑在第一回合总是选择1,创造出4的倍数。这意味着,如果用户使用3台计算机使用1台计算机,用户使用2台计算机使用2台计算机,依此类推,直到没有石头残留。我的问题是,我很难了解函数以及如何从函数中的参数调用数据,因此非常感谢您的帮助 这就是我目前所拥有的C++ C+中的函数+;-13人游戏,c++,C++,我试图编写一个程序,需要通过函数进行输入验证。它背后的想法很像21块石头,只有13块,电脑总是赢。游戏以13块石头开始,电脑在第一回合总是选择1,创造出4的倍数。这意味着,如果用户使用3台计算机使用1台计算机,用户使用2台计算机使用2台计算机,依此类推,直到没有石头残留。我的问题是,我很难了解函数以及如何从函数中的参数调用数据,因此非常感谢您的帮助 这就是我目前所拥有的 #include <iostream> using namespace std; //function proto
#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;
}
#包括
使用名称空间std;
//功能原型
bool validPick(整数);
int computerPick(int stones\u in\u Pill,int Player2 Taken);
int playerPick(int stones\u in\u pill);
int main()
{
int stones_left=13,p1取,p2取;
尽管你应该通过询问这样的问题来理解初学者的C++,但是我会尝试通过一个例子来解释你的代码中有什么错误:
bool validPick(int numStones) {
if((numStones < 1) || (numStones >3))
cout << "Invalid Selection. 1-3 is all you can have!";
else
return numStones;
}
对于产生值的函数以及这些值是如何传递回来的,有许多哲学
函数可以通过修改参数或返回值将值传递回调用方
playerPick
功能可以修改传递的值(通过引用传递):
void playerPick(内部和石头堆)
{
一堆堆石头;
}
或者通过返回一个值:
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;
}
int-playerPick(无效)
{
//用于临时保存值的局部变量。
int stones_in_pile=-1;
一堆堆石头;
返回堆中的石头;
}
请注意,后一个版本使用一个本地临时变量,编译器将在函数结束时返回该值的副本。
我在这里使用的参数是 Vult<代码>。我的问题是,我很难把我的头脑围绕函数和数据从参数内调用——所以为什么不写一个简单的函数调用和返回的例子,然后熟悉它?这就是C++程序员如何接近一个新的。或者C++的不知名的方面——简单的程序先写,一旦学习,新的技术就在实际的程序中使用。@ toBi303:谢谢复习,打字。我已经改正了。
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;
}