### Toothpick lab

I'm working on a toothpick game (lab) for C++

unfortunately I keep getting strange results every time the game ends. For instance I'll pick up the last toothpick and the computer will still pick up negative toothpicks, or I'll lose and i'll get the message that I have won. If I move my do while loop I will also get a strange error with my terminal and will close after the 2nd pick up.

here is the details for the game

2) The game of “23” is a two-player game that begins with a pile of 23 toothpicks. 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 the following rules:

a. If there are more than 4 toothpicks left, the computer should withdraw 4 – X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.
b. If there are 2 to 4 toothpicks left, the computer should withdraw enough toothpicks to lave 1.
c. If there is 1 toothpick left, the computer has to take it and loses.

When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include #include #include using namespace std; int main(int argc, char** argv) { //Write a human vs computer program that plays "23" //c_pick is the computer's choice while h_pick is the human choice int c_pick, h_pick; int toothpicks= 23; do{ cout << "Welcome to the toothpick game." << endl; cout << "Please pick up your toothpick(s), choose between 1 and 3.\n"; cin >> h_pick; toothpicks = toothpicks - h_pick; cout << toothpicks << " toothpick(s) remaining" << endl; if (toothpicks == 1 && c_pick == 1) { cout << "Human! You have prevailed!" << endl; break; } if (toothpicks > 4) { c_pick = 4 - h_pick; cout << "The computer took " << c_pick << " toothpick(s)" << endl; } else if (toothpicks == 2) { c_pick = 1; cout << "The computer took " << c_pick << " toothpick(s)" << endl; } else if (toothpicks == 3) { c_pick = 2; cout << "The computer took " << c_pick << " toothpick(s)" << endl; } else if (toothpicks == 4) { c_pick = 3; cout << "The computer took " << c_pick << " toothpick(s)" << endl; } else if (toothpicks == 1) { c_pick = 1; cout << "The computer took " << c_pick << " toothpick(s)" << endl; } toothpicks = toothpicks - c_pick; cout << toothpicks << " toothpick(s) remaining"<3 || h_pick <0) { cout<< "Please enter a correct value." <>h_pick; toothpicks=23; continue; } } while (h_pick != 4); return 0; } ``````
I'm not sure exactly what causes the errors, but here are some clues that might fix them...

1) the best input validation for standard cin would go has follow

 ``123456789101112`` ``````cout << "Please pick up your toothpick(s), choose between 1 and 3.\n"; cin >> h_pick; while(cin.fail() || cin.get() != 10 || h_pick < 1 || h_pick > 3 || h_pick > toothpicks) { cin.sync(); cin.clear(); cout << "Please pick up your toothpick(s), choose between 1 and 3.\n"; cin >> h_pick; }``````

`cin.fail()` will verify that you didn't crash it, for example, if you input letters instead of a number, `cin.get() != 10` will verify that the last character sent to cin is a newline character (instead of a space with more numbers after, for example), and the last three conditions are the ones specific to the input expected by your program. It's in a while loop, so if any of the conditions return true, your input sequence simply restarts, that is until a valid value for h_pick is entered. `cin.sync()` and `cin.clear()` will just reset cin in case it failed or if you entered gibberrish. But the validation sequence must be RIGHT AFTER each call to cin, because if you enter an invalid value, your toothpicks total will be screwed and it will screw the AI's choice, so line 67 to 72 are useless.

2) In line 35 and 62, I think you just need to verify if `toothpicks == 1`, because at this point it doesn't matter what the user's pick is, the game is over.

3) I know what else you should fix but I'll let you go with that, reply if you have more questions :-)

Nice game by the way! ^_^

AeonFlux
Last edited on
Topic archived. No new replies allowed.