Advertisement
Guest User

Untitled

a guest
Sep 12th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char playAgain = 'Y';
  9. int totalSticks = 23;
  10. int humanStickChoice = 1;
  11.  
  12. cout<<"**************************** RULES: Remove 1, 2, or 3 sticks per turn.\n";
  13. cout<<"*Welcome to our Game of 23!* The player who removes the very last stick\n";
  14. cout<<"**************************** loses. Good luck!\n";
  15. cout<<endl;
  16.  
  17. while (totalSticks > 0 && playAgain == 'Y' || playAgain == 'y')
  18. {
  19. cout<<"There are "<<totalSticks<<" sticks left. How many sticks would you like to remove?:\n";
  20. cin>>humanStickChoice;
  21. cout<<endl;
  22.  
  23. if(totalSticks >= 3){
  24. if(humanStickChoice != 1 && humanStickChoice != 2 && humanStickChoice != 3){
  25. while(humanStickChoice != 1 && humanStickChoice != 2 && humanStickChoice != 3){
  26. cout<<"Invalid choice; you can remove 1, 2, or 3 sticks. Choose again:\n";
  27. cin>>humanStickChoice;
  28. }
  29. }
  30. totalSticks=totalSticks-humanStickChoice;
  31. cout<<"Player removes "<<humanStickChoice<<" sticks.\n";
  32. }
  33. else if(totalSticks == 3){
  34. if(humanStickChoice != 1 && humanStickChoice != 2){
  35. while(humanStickChoice != 1 && humanStickChoice != 2){
  36. cout<<"Invalid choice; you can remove 1 or 2 sticks. Choose again:\n";
  37. cin>>humanStickChoice;
  38. }
  39. }
  40. }
  41. else if(totalSticks == 2){
  42. if(humanStickChoice != 1){
  43. while(humanStickChoice != 1){
  44. cout<<"Invalid choice; as there are 2 sticks remaining, you can remove only one.\n";
  45. cin>>humanStickChoice;
  46. }
  47. }
  48. totalSticks=totalSticks-humanStickChoice;
  49. cout<<"Player removes "<<humanStickChoice<<" sticks.\n";
  50. }
  51. else if(totalSticks == 1){
  52. if(humanStickChoice != 1){
  53. while(humanStickChoice != 1){
  54. cout<<"Invalid choice; as there is 1 stick remaining, you can remove only one.\n";
  55. cin>>humanStickChoice;
  56. }
  57. }
  58. int humanStickChoice = 1;
  59. cout<<"Player removes "<<humanStickChoice<<" stick.\n";
  60. totalSticks=totalSticks-humanStickChoice;
  61. cout<<"*************************************************\n";
  62. cout<<"Player removed the last stick! Computer wins!";
  63. if(totalSticks == 0){
  64. cout<<endl<<"Would you like to play again? If so, input Y and hit return.\n";
  65. cin>>playAgain;
  66. totalSticks = 23;
  67. }
  68. }
  69. //This block above is where the logic error is occurring; instead of going back to the
  70. //beginning of the "main" while loop and starting over with the human player starting,
  71. //the totalSticks count is merely replenished and the program continues to go on with the
  72. //computer making the first move; the human is supposed to make the first move every game.
  73. if(totalSticks > 4){
  74. int computerStickChoice = (4-humanStickChoice);
  75. cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
  76. totalSticks=totalSticks-computerStickChoice;
  77. }
  78. else if(totalSticks == 4){
  79. int computerStickChoice = 3;
  80. cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
  81. totalSticks=totalSticks-computerStickChoice;
  82. }
  83. else if(totalSticks == 3){
  84. int computerStickChoice = 2;
  85. cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
  86. totalSticks=totalSticks-computerStickChoice;
  87. }
  88. else if(totalSticks == 2){
  89. int computerStickChoice = 1;
  90. cout<<"Computer removes "<<computerStickChoice<<" stick.\n";
  91. totalSticks=totalSticks-computerStickChoice;
  92. }
  93. else if(totalSticks == 1){
  94. int computerStickChoice = 1;
  95. cout<<"Computer removes "<<computerStickChoice<<" stick.\n";
  96. totalSticks=totalSticks-computerStickChoice;
  97. cout<<"*************************************************\n";
  98. cout<<"Computer removed the last stick! You win!";
  99. if(totalSticks == 0){
  100. cout<<endl<<"Would you like to play again? If so, input Y and hit return.\n";
  101. cin>>playAgain;
  102. totalSticks = 23;
  103. }
  104. }
  105. }
  106.  
  107.  
  108.  
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement