hi i'm working on an assignment and having trouble with this piece of the program.
The game of “23” is a two-player game that begins with a pile of 23 tooth- picks. Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays “23”. The human should always move first. When it is the computer’s turn, it should play according to the following rules:
withdraw 4 – X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.
if there are 2 to 4 toothpicks left, then the computer should withdraw enough toothpicks to leave 1.
if there is one toothpick left, then the computer has to take it and it loses.
When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure that the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.
here is my code. any help will be appreciated.
#include <iostream>
using namespace std;
void compAlgorithm(int totalTp, int compTp, int userTp, int countr) {
do {
if (totalTp > 4) {
compTp = 4 - userTp;
totalTp += totalTp - compTp;
countr--;
}
if (totalTp >= 2 && totalTp <= 4) {
switch (totalTp) {
case 2:
totalTp += totalTp - 1;
countr--;
break;
case 3:
totalTp += totalTp - 2;
countr--;
break;
case 4:
totalTp += totalTp - 3;
countr--;
break;
}
}
} while (countr == 1);
}
int main() {
int userTp = 0;
int compTp = 0;
int totalTp = 23;
int countr = 0;
do {
if (countr == 0) {
cout << "please enter a vale of toothpics between 1 and 3" << endl;
cin >> userTp;
totalTp += totalTp - userTp;
countr++;
}
else if (countr == 1) {
compAlgorithm;
}
} while (totalTp >= 2);
if (totalTp == 1 && countr == 1) {
cout << "you win" << endl;
}
else if (totalTp > 0 && totalTp < 2 && countr == 0) {
cout << "please enter a vale of toothpics between 1 and 2" << endl;
cin >> userTp;
totalTp = totalTp - userTp;
switch (totalTp) {
case 1:
cout << "you win" << endl;
break;
case 2:
cout << "you loose" << endl;
break;
}
}
system("pause");
return 0;
}
thanks in advance.