daily pastebin goal
58%
SHARE
TWEET

21 sticks

Porr011 Jan 18th, 2019 (edited) 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. // varibles for menu
  6. void menu ();
  7. void pvp ();
  8. void pvc ();
  9. int k = 0;
  10. int selection = 0;
  11. int sticks = 0;
  12. int n = 0;
  13.  
  14. int main(){
  15.     menu();
  16.     return 0;
  17.    
  18. }
  19.  
  20. void menu ()
  21. {
  22.   cout << endl;         // menu opptions / entering 1 or 2 will take you to diffrent codes
  23.   cout << " Please choose from the following options - \n";
  24.   cout << " 1. Player vs player.\n";
  25.   cout << " 2. Player vs Computer.\n";
  26.   cin >> selection;
  27.  
  28.   if (selection == 1)
  29.     {
  30.       pvp ();
  31.     }
  32.   else if (selection == 2)
  33.     {
  34.       pvc ();
  35.     }
  36. }
  37.  
  38. // setting the pvp code    
  39. void pvp () {
  40.  
  41. // setting varibles
  42.     string player1;
  43.     string player2;
  44.     char yn = 'y';
  45.     // varibles for random turn
  46.     int pick=1, win=0, pturn;
  47.     srand(time(NULL));
  48.     int random = rand();
  49.  
  50.     // rules
  51.     cout << "Welcome to the 21 Sticks Game on c++" << endl << endl;
  52.     cout << "The goal is to not be the person that has to take the last stick"
  53.       << endl;
  54.     cout << "You can only pick 1,2, or 3 sticks per turn" << endl;
  55.  
  56.     // getting names
  57.     cout << "player 1 enter your name";
  58.     cin >> player1;
  59.     cout << "player 2 enter your name";
  60.     cin >> player2;
  61.     cout << endl << endl;
  62.    
  63.      if (random % 2 == 0)
  64.         {
  65.             cout << player1 << " goes first" << endl << endl;
  66.             pturn = 0;
  67.         }
  68.  
  69.  
  70.         else
  71.         {
  72.             cout << player2 << " goes first" << endl << endl;
  73.             pturn = 1;
  74.         }
  75.  
  76.     //loop for player 1 turns
  77.     const int total = 21;
  78.     int n, remainder;
  79.     bool winner = false;
  80.     // varibles for player vs player
  81.     remainder = total;
  82.    
  83.     while (!winner)
  84.     {
  85.         // code for the turns
  86.         if (remainder >= 0)
  87.         {
  88.             cout << " There are " << remainder << " sticks. " << endl;
  89.             cout << " Player 1, pick a number between 1 and 3. " << endl;
  90.             cin >> n;
  91.         }
  92.             if ( n > 0 && n < 4)
  93.             {
  94.                 cout << "You have removed " << n << " sticks." << endl;
  95.                 remainder -= n;
  96.             }
  97.             else
  98.             {
  99.                 cout << "Invalid number" << endl;
  100.                 cout << "Please enter a number between 1 and 3." << endl;
  101.                 cin >>n;
  102.                 cout << " Player 1 removed " << n << " sticks." <<endl;
  103.                 remainder -=n;
  104.             }
  105.             if (remainder >= 0)
  106.             {
  107.                 cout << "There are " << remainder << " sticks." << endl;
  108.                 cout << " Player 2, pick a number between 1 and 3." << endl;
  109.                 cin >> n;
  110.             }
  111.             if ( n > 0 && n < 4)
  112.             {
  113.                 cout << "You have removed " << n << " sticks." << endl;
  114.                 remainder -= n;}
  115.             else
  116.             {
  117.                 cout << "Invalid number" << endl;
  118.                 cout << "Please enter a number between 1 and 3." << endl;
  119.                 cin >>n;
  120.                 cout << "You have removed " << n << " sticks." <<endl;
  121.                 remainder -=n;
  122.             }
  123.             if (remainder == 0)
  124.             {
  125.                 winner = true;
  126.                 cout << "Player 1 wins!";
  127.                
  128.             }
  129.     }
  130.    
  131. }
  132. // finishing the player vs player code
  133.  
  134. // player vs computer code
  135. void pvc () {
  136.  
  137.     int sticks=21,n=0;
  138. }
  139. void display_left_sticks()
  140. {   sticks=sticks-n;
  141.     cout<<" Remaining sticks = "<<sticks;
  142.     if(sticks==1){cout<<"You loss";} // losing codes
  143. }
  144.  
  145. // the code for the computer to win every time
  146. void cpu_stick_pick()
  147. {
  148. if(n==1){sticks=sticks-3;cout<<" Computer Picked 3 "; n=0;}
  149. else if(n==2){sticks=sticks-2;cout<<" Computer Picked 2 ";n=0;}
  150. else if(n==3){sticks=sticks-1;cout<<" Computer Picked 1 ";n=0;}
  151. }
  152.  
  153. int joaquin ()
  154. {
  155.     cout << "Welcome to the 21 Sticks Game on c++ against a computer" << endl << endl;
  156.     cout << "The goal is to not be the person that has to take the last stick"
  157.       << endl;
  158.     cout << "You can only pick 1,2, or 3 sticks per turn" << endl;
  159.     cout<<" We have 21 sticks ";
  160.     do
  161.     {
  162.     cout<<" Your turn ";
  163.     cin>>n; // setting limits
  164.     if(n!=1&&n!=2&&n!=3){cout<<" Invalid input ";n=0; continue;}
  165.     display_left_sticks();
  166.     cpu_stick_pick();
  167.     display_left_sticks();
  168.     }
  169.     while(sticks!=1);
  170.     return 0;
  171. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top