Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- int main()
- {
- char playAgain = 'Y';
- int totalSticks = 23;
- int humanStickChoice = 1;
- cout<<"**************************** RULES: Remove 1, 2, or 3 sticks per turn.\n";
- cout<<"*Welcome to our Game of 23!* The player who removes the very last stick\n";
- cout<<"**************************** loses. Good luck!\n";
- cout<<endl;
- while (totalSticks > 0 && playAgain == 'Y' || playAgain == 'y')
- {
- cout<<"There are "<<totalSticks<<" sticks left. How many sticks would you like to remove?:\n";
- cin>>humanStickChoice;
- cout<<endl;
- if(totalSticks >= 3){
- if(humanStickChoice != 1 && humanStickChoice != 2 && humanStickChoice != 3){
- while(humanStickChoice != 1 && humanStickChoice != 2 && humanStickChoice != 3){
- cout<<"Invalid choice; you can remove 1, 2, or 3 sticks. Choose again:\n";
- cin>>humanStickChoice;
- }
- }
- totalSticks=totalSticks-humanStickChoice;
- cout<<"Player removes "<<humanStickChoice<<" sticks.\n";
- }
- else if(totalSticks == 3){
- if(humanStickChoice != 1 && humanStickChoice != 2){
- while(humanStickChoice != 1 && humanStickChoice != 2){
- cout<<"Invalid choice; you can remove 1 or 2 sticks. Choose again:\n";
- cin>>humanStickChoice;
- }
- }
- }
- else if(totalSticks == 2){
- if(humanStickChoice != 1){
- while(humanStickChoice != 1){
- cout<<"Invalid choice; as there are 2 sticks remaining, you can remove only one.\n";
- cin>>humanStickChoice;
- }
- }
- totalSticks=totalSticks-humanStickChoice;
- cout<<"Player removes "<<humanStickChoice<<" sticks.\n";
- }
- else if(totalSticks == 1){
- if(humanStickChoice != 1){
- while(humanStickChoice != 1){
- cout<<"Invalid choice; as there is 1 stick remaining, you can remove only one.\n";
- cin>>humanStickChoice;
- }
- }
- int humanStickChoice = 1;
- cout<<"Player removes "<<humanStickChoice<<" stick.\n";
- totalSticks=totalSticks-humanStickChoice;
- cout<<"*************************************************\n";
- cout<<"Player removed the last stick! Computer wins!";
- if(totalSticks == 0){
- cout<<endl<<"Would you like to play again? If so, input Y and hit return.\n";
- cin>>playAgain;
- totalSticks = 23;
- }
- }
- //This block above is where the logic error is occurring; instead of going back to the
- //beginning of the "main" while loop and starting over with the human player starting,
- //the totalSticks count is merely replenished and the program continues to go on with the
- //computer making the first move; the human is supposed to make the first move every game.
- if(totalSticks > 4){
- int computerStickChoice = (4-humanStickChoice);
- cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
- totalSticks=totalSticks-computerStickChoice;
- }
- else if(totalSticks == 4){
- int computerStickChoice = 3;
- cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
- totalSticks=totalSticks-computerStickChoice;
- }
- else if(totalSticks == 3){
- int computerStickChoice = 2;
- cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
- totalSticks=totalSticks-computerStickChoice;
- }
- else if(totalSticks == 2){
- int computerStickChoice = 1;
- cout<<"Computer removes "<<computerStickChoice<<" stick.\n";
- totalSticks=totalSticks-computerStickChoice;
- }
- else if(totalSticks == 1){
- int computerStickChoice = 1;
- cout<<"Computer removes "<<computerStickChoice<<" stick.\n";
- totalSticks=totalSticks-computerStickChoice;
- cout<<"*************************************************\n";
- cout<<"Computer removed the last stick! You win!";
- if(totalSticks == 0){
- cout<<endl<<"Would you like to play again? If so, input Y and hit return.\n";
- cin>>playAgain;
- totalSticks = 23;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement