### The game of "23"

Hi everyone,
I am a beginner at Cpp and I've been practicing by programing this "23" game. But it doesn't work as I expected..... I wish some of you guys could help me with it.
I'd appreciate that!

rule:

The game of “23” is a two-player game that begins with a pile of 23 toothpicks. Players take turn, 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:
1. If there are more that 4 toothpicks, then the computer should draw 4X toothpicks, where X is the number of toothpicks the human drew on the previous move.
2. If there are 2 to 4 toothpicks left, then the computer should withdraw enough toothpicks to leave 1.
3. If there is 1 toothpick left, then the computer has to take it and lose.
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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` #include using namespace std; int k, i; int main () { k = 23; // k is the toothpicks remained during the current round of game. i = 0; // i is the number of toothpicks withdrawn by the user, not the computer. while (k > 4) { cout << "Please input a number between 1 and 3: "; cin >> i; if (( i > k ) || ( i != 1 || i != 2 || i != 3) ) { cout << " Input " << i << " not acceptable! Please input another number." ; cin >> i; } else { k = k-i ; } cout << " There are " << k << " toothpicks left. Press enter to continue." << endl; } while ( k >= 2 && k <= 4) { cout << "Please input a number between 1 and 3: "; cout << " Congratulations! You win. "; } while (k == 1) { cout << " Congratulations! You win. "; } return 0; }``````

Sorry I just found that there's a logical error in my line..
So I will rewrite this. However, I'm confused where to start the "while" loop and how to nest one loop in the other? Should I put the " cin i" in the "while" loop?
I think I am just misunderstood the use of loop.
Last edited on
Topic archived. No new replies allowed.