Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.48 KB | None | 0 0
  1. //Paul Fynn Kuemmel
  2. // This is a code which mimics the game 21 sticks, in which 2 players take turns either taking 1, 2 or 3 sticks
  3. // Eventually the person who's turn it is when 1 stick remains, is the loser.
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main()
  10. {  
  11.     //Declares the variables which will be used in the game.
  12.     int GameChoice, loop, StickNum, sticksTaken, playerTurn, counter;
  13.     string Player1Name, Player2Name, NewGame;
  14.     //Loops while the player still wants to play the game
  15.     loop = 1;
  16.     while(loop == 1){
  17.        
  18.         //This is the starting menu and introduction to the game
  19.          cout << "Hello! Welcome to 21 sticks! The rules are as following: " << endl;
  20.     cout << "1. You can only select between 1, 2 or 3 sticks" << endl;
  21.     cout << "2. The last person to take the stick loses" << endl;
  22.     cout << "Game options: " << endl;
  23.     cout << "1) Play vs Computer: " << endl;
  24.     cout << "2) Play vs Friend: " << endl;
  25.     cout << "Enter either 1 or 2 to choose: ";
  26.     cin >> GameChoice;
  27.        
  28.         //This occurs when the player decides to play against a friend
  29.         if(GameChoice==2){
  30.            
  31.             counter = 1;
  32.            
  33.             //Total number of sticks  
  34.             StickNum = 21;
  35.              
  36.             //Asks the players for  their names  
  37.             cout<<"Enter the name of Player 1: ";
  38.             getline(cin, Player1Name);//This ensures that the data is properly collected and stored.
  39.             getline(cin, Player1Name);
  40.    
  41.            
  42.             cout<<"Enter the name of Player 2: ";
  43.             getline(cin, Player2Name);
  44.                
  45.             cout<<"Number of sticks = "<<StickNum<<endl;
  46.            
  47.             //Gets a random number to see who will started the game
  48.             playerTurn = (rand() % 4) + 1;
  49.            
  50.             //If the random number is either 1 or 3, then player 1 will start
  51.             if(playerTurn == 1 or playerTurn == 3){
  52.                
  53.                 //Loops uring the turn of player 1
  54.                 while(counter == 1){
  55.                     //player 1 inputs number of sticks to take out, once a correct value is entered, the loop ends and changes the turn
  56.                
  57.                     do{
  58.                         cout<<Player1Name<<" ,Enter number of sticks you want to take out, keep it between 1-3: ";
  59.                         cin>>sticksTaken;
  60.                         if(sticksTaken < 1 or sticksTaken > 3 ){
  61.                             cout<<"The input isn't between the boundaries, so it is invalid."<<endl;
  62.                         }
  63.                     }while(sticksTaken < 1 or sticksTaken > 3 );
  64.                    
  65.                     //Subtracts the selection from the original total
  66.                     StickNum-=sticksTaken;
  67.                     cout<<StickNum<<endl;
  68.                    
  69.                     //If number of sticks is 1 then player 1 wins
  70.                     if(StickNum == 1){
  71.                         cout<<Player1Name<<" Wins!"<<endl;
  72.                         break;
  73.                     }
  74.                    
  75.                     //If number of sticks is 0 or less than 0 then player 2 wins
  76.                     if(StickNum <= 0){
  77.                         cout<<Player2Name<<" Wins!"<<endl;
  78.                         break;
  79.                     }
  80.                    
  81.                     //Now, player 2 enters their selection
  82.                     //Loop ends when valid input is entered
  83.                     do{
  84.                         cout<<Player2Name<<" ,Enter the number of sticks you want to take out 1-3: ";
  85.                         cin>>sticksTaken;
  86.                         if(sticksTaken < 1 or sticksTaken > 3 ){
  87.                             cout<<"Invalid input since it is not between 1-3."<<endl;
  88.                         }
  89.                     }while(sticksTaken < 1 or sticksTaken > 3 );
  90.                        
  91.                     StickNum-=sticksTaken;
  92.                     cout<<StickNum<<endl;
  93.                    
  94.                     //If number of sticks is 1 then player 2 wins
  95.                     if(StickNum == 1){
  96.                         cout<<Player2Name<<" Wins!"<<endl;
  97.                         break;
  98.                     }
  99.                    
  100.                     //If number of sticks is 0 or less than 0 then player 1 wins  
  101.                     if(StickNum <= 0){
  102.                         cout<<Player1Name<<" Wins!"<<endl;
  103.                         break;
  104.                     }
  105.                        
  106.                 }
  107.                
  108.                 //Gives the option of playing another round
  109.                 //Loop ends when vaid input is entered
  110.                 do{
  111.                     cout<<"Do you want to play another round (Yes or No)? ";
  112.                     cin>>NewGame;
  113.                     if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
  114.                         cout<<"Invalid input since you did not enter yes or no";
  115.                     }
  116.                 }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
  117.                
  118.                 //Returns to the beginning if Yes or No is entered    
  119.                 if(NewGame == "yes" or NewGame == "Yes"){
  120.                     continue;
  121.                 }
  122.                        
  123.                 //If yes the while loop at program restarts, and the code continues
  124.                 //If no, loop variable is set to 2 which ends the while loop
  125.                 if(NewGame == "no" or NewGame == "No"){
  126.                     loop++;
  127.                 }
  128.             }
  129.            
  130.             //Now this code is if player 2 starts first, with the random number being either 2 or 4
  131.             if(playerTurn == 2 or playerTurn == 4){
  132.                    
  133.                 while(counter == 1){
  134.                    
  135.                     //Player 2 inputs the number of sticks to be taken out
  136.                     //Loop ends when valid input is entered
  137.                     do{
  138.                         cout<<Player2Name<<" ,enter number of sticks you want to take out(1-3): ";
  139.                         cin>>sticksTaken;
  140.                         if(sticksTaken < 1 or sticksTaken > 3 ){
  141.                             cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  142.                         }
  143.                     }while(sticksTaken < 1 or sticksTaken > 3 );
  144.                        
  145.                     StickNum -= sticksTaken;
  146.                     cout<<StickNum<<endl;
  147.                    
  148.                     //If number of sticks remaining is 1 then player 2 wins
  149.                     if(StickNum == 1){
  150.                         cout<<Player2Name<<" Wins!"<<endl;
  151.                         break;
  152.                     }
  153.                    
  154.                     //If number of sticks is 0 or less than 0 then player 1 wins
  155.                     if(StickNum <= 0){
  156.                         cout<<Player1Name<<" Wins!"<<endl;
  157.                         break;
  158.                     }
  159.                    
  160.                     //Player 1 inputs the number of sticks to take out
  161.                     //Loop ends when valid input is entered    
  162.                     do{
  163.                         cout<<Player1Name<<" ,enter number of sticks you want to take out(1-3): ";
  164.                         cin>>sticksTaken;
  165.                         if(sticksTaken < 1 or sticksTaken > 3 ){
  166.                             cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  167.                         }
  168.                     }while(sticksTaken < 1 or sticksTaken > 3 );
  169.                        
  170.                     StickNum -= sticksTaken;
  171.                     cout<<StickNum<<endl;
  172.                    
  173.                     //If number of sticks is 1 then player 1 wins
  174.                     if(StickNum == 1){
  175.                         cout<<Player1Name<<" Wins!"<<endl;
  176.                         break;
  177.                     }
  178.                    
  179.                     //If number of sticks is 0 or less than 0 then player 2 wins  
  180.                     if(StickNum <= 0){
  181.                         cout<<Player2Name<<" Wins!"<<endl;
  182.                         break;
  183.                     }
  184.                        
  185.                 }
  186.                 //Asks the user if they want to play another round, and restart the loop
  187.                 do{
  188.                     cout<<"Do you want to play another round (yes or no) ? ";
  189.                     cin>>NewGame;
  190.                     if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
  191.                         cout<<"Invalid input since you did not enter yes or no";
  192.                     }
  193.                 }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
  194.                        
  195.                 if(NewGame == "yes" or NewGame == "Yes"){
  196.                     continue;
  197.                 }
  198.                        
  199.                 if(NewGame == "no" or NewGame == "No"){
  200.                     loop++;
  201.                 }
  202.                
  203.             }
  204.         }
  205.        
  206.        
  207.        
  208.        
  209.         //Now, this is the code which runs if the player decides to attempt to defeat the Computer.
  210.         if(GameChoice==1){
  211.             counter = 1;
  212.             StickNum = 21;
  213.             cout<<"Enter player 1 name: ";
  214.             getline(cin, Player1Name);
  215.             getline(cin, Player1Name);
  216.            
  217.             cout<<"Number of sticks = "<<StickNum<<endl;
  218.                
  219.             playerTurn = (rand() % 4) + 1;;
  220.            
  221.            
  222.             if(playerTurn == 1 or playerTurn == 3){
  223.                
  224.                 while(counter == 1){
  225.                    
  226.                     //Player 1 inputs the number of sticks to take out
  227.                     //Loop ends when valid input is entered  
  228.                     do{
  229.                         cout<<Player1Name<<" ,Enter number of sticks you want to take out(1-3): ";
  230.                         cin>>sticksTaken;
  231.                         if(sticksTaken < 1 or sticksTaken > 3 ){
  232.                             cout<<"Invalid input since it is not between 1-3."<<endl;
  233.                         }
  234.                     }while(sticksTaken < 1 or sticksTaken > 3 );
  235.                        
  236.                     StickNum -= sticksTaken;
  237.                     cout<<StickNum<<endl;
  238.                    
  239.                     //If number of sticks is 1 then player 1 wins
  240.                     if(StickNum == 1){
  241.                         cout<<Player1Name<<" Wins!"<<endl;
  242.                         break;
  243.                     }
  244.                    
  245.                     //If number of sticks is 0 or less than 0 then the computer wins  
  246.                     if(StickNum <= 0){
  247.                         cout<<"Computer Wins!"<<endl;
  248.                         break;
  249.                     }
  250.                    
  251.                     //Code for computer to use to win.
  252.                     //Computer takes out the certain amount sticks so that the sticks taken over the 2 turns, always equals to 4.
  253.                     sticksTaken = 4 - sticksTaken;
  254.                     StickNum -= sticksTaken;
  255.                     cout<<"Computer takes out "<<sticksTaken<<" sticks."<<endl;
  256.                     cout<<StickNum<<endl;
  257.                    
  258.                     //If number of sticks is 1 then computer wins
  259.                     if(StickNum == 1){
  260.                         cout<<"Computer Wins!"<<endl;
  261.                         break;
  262.                     }
  263.                    
  264.                     //If number of sticks is 0 or less than 0 then player 1 wins  
  265.                     if(StickNum <= 0){
  266.                         cout<<Player1Name<<" Wins!"<<endl;
  267.                         break;
  268.                     }
  269.                    
  270.                 }
  271.                
  272.                 do{
  273.                     cout<<"Do you want to play another round(yes or no): ";
  274.                         cin>>NewGame;
  275.                         if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
  276.                             cout<<"Invalid input since you did not enter yes or no";
  277.                         }
  278.                     }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
  279.                        
  280.                     if(NewGame == "yes" or NewGame == "Yes"){
  281.                         continue;
  282.                     }
  283.                        
  284.                     if(NewGame == "no" or NewGame == "No"){
  285.                         loop++;
  286.                     }
  287.             }
  288.                
  289.  
  290.            
  291.            
  292.             //Computer goes first
  293.             if(playerTurn == 2 or playerTurn == 4){
  294.                
  295.                 while(counter == 1){
  296.                    
  297.                     //If the Number of sticks is over 4 then computer takes out a random number between 1 and 3, to make it fairer
  298.                     if(StickNum>4){
  299.                         sticksTaken = (rand() % 3) + 1;
  300.                     }
  301.                    
  302.                     // If the Number of sticks is equal to 4 then it takes out 3
  303.                     if(StickNum==4){
  304.                         sticksTaken=3;
  305.                     }
  306.                    
  307.                     // If the Number of sticks is equal to 3 then it takes out 2
  308.                     if(StickNum==3){                // All of these lines ensure victory for the Computer
  309.                         sticksTaken=2;
  310.                     }
  311.                    
  312.                     // If the Number of sticks is equal to 2 then it takes out 1
  313.                     if(StickNum==2){
  314.                         sticksTaken=1;
  315.                     }
  316.                    
  317.                     StickNum-=sticksTaken;
  318.                     cout<<"Computer removes "<<sticksTaken<<" sticks."<<endl;
  319.                     cout<<StickNum<<endl;
  320.                    
  321.                     //If number of sticks is 1 then computer wins
  322.                     if(StickNum == 1){
  323.                         cout<<"Computer wins!"<<endl;
  324.                         counter = 2;
  325.                         break;
  326.                     }
  327.                    
  328.                     //If number of sticks is 0 or less than 0 then player 1 wins
  329.                     if(StickNum <= 0){
  330.                         cout<<Player1Name<<" Wins!"<<endl;
  331.                         counter = 2;
  332.                         break;
  333.                     }
  334.                    
  335.                     /
  336.                     do{
  337.                         cout<<Player1Name<<" ,Enter number of sticks you want to take out(1-3): ";
  338.                         cin>>sticksTaken;
  339.                         if(sticksTaken < 1 or sticksTaken > 3 ){
  340.                             cout<<"Invalid input since it is not between 1-3."<<endl;
  341.                         }
  342.                     }while(sticksTaken < 1 or sticksTaken > 3 );
  343.                        
  344.                     StickNum-=sticksTaken;
  345.                     cout<<StickNum<<endl;
  346.                    
  347.                     //If number of sticks is 1 then player 1 wins
  348.                     if(StickNum == 1){
  349.                         cout<<Player1Name<<" Wins!"<<endl;
  350.                         counter = 2;
  351.                         break;
  352.                     }
  353.                    
  354.                     //If number of sticks is 0 or less than 0 then computer wins  
  355.                     if(StickNum <= 0){
  356.                         cout<<"Computer Win!"<<endl;
  357.                         counter = 2;
  358.                         break;
  359.                     }  
  360.                    
  361.                 }
  362.                
  363.                 do{
  364.                     cout<<"Do you want to play another round(yes or no): ";
  365.                         cin>>NewGame;
  366.                         if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
  367.                             cout<<"Invalid input since you did not enter yes or no";
  368.                         }
  369.                     }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
  370.                        
  371.                     if(NewGame == "yes" or NewGame == "Yes"){
  372.                         continue;
  373.                     }
  374.                        
  375.                     if(NewGame == "no" or NewGame == "No"){
  376.                         loop++;
  377.                     }
  378.             }
  379.  
  380.         }
  381.        
  382.         else{
  383.             cout<<"Invalid input since you did not enter 1, 2, or 3. "<<endl;
  384.         }
  385.  
  386.     }
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement