I'm building a Toothpick Game of 23 in C++ as an homework assignment in my Programming course. I'm almost finished with the code and the output looks exactly like the one that I am suppose to follow.
I am supposed to use a function in my code but I don't know how to use the function. Everything in the program works like it should except the function returns 0 and that 0 is the last line of the output and that's the line that is not identical to the output I am supposed to follow. So maybe someone can help my find out how I can get this right.
#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;
}
I always get this 0 in the last line and I don't want that. So my question is. How can I use this function so it won't be like this?