我在C ++课程中用C ++编写了一个23的Toothpick游戏作为家庭作业。我几乎完成了代码,输出看起来就像我想要遵循的那样。
我应该在我的代码中使用一个函数,但我不知道如何使用该函数。程序中的所有内容都应该像它应该的那样工作,除了函数返回0并且0是输出的最后一行,并且该行与我应该遵循的输出不同。所以也许有人可以帮助我找到如何做到这一点。
#include <iostream>
using namespace std;
int computerMove(int numPicksLeft, int humanNumber);
int main()
{
int a, z=0, y=0;
a = computerMove(z, y);
cout << a;
return 0;
}
int computerMove(int numPicksLeft, int humanNumber) {
int number_left=23, n, cpu_turn;
do{
cout << "There are " << number_left << " toothpicks left. Pick 1, 2 or 3 toothpicks: ";
cin >> n;
if (n <= 3)
{
number_left -= n;
if (number_left > 4)
{
cpu_turn = (4 - n); // þar sem n er fjöldi tannstöngla dregnir af notanda.
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 2)
{
cpu_turn = 1;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 3)
{
cpu_turn = 2;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 4)
{
cpu_turn = 3;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 1)
{
cpu_turn = 1;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
cout << "You won!" << endl;
number_left -= cpu_turn;
}
else if (number_left == 0)
{
cpu_turn = 0;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
cout << "I won!" << endl;
}
}
else
cout << "Invalid input. Try again." << endl;
} while (number_left > 0);
return 0;
}
我总是在最后一行得到0,我不想要那个。所以我的问题是。我怎么能使用这个功能,所以它不会像这样?