Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // varibles for menu
- void menu ();
- void pvp ();
- void pvc ();
- int k = 0;
- int selection = 0;
- int sticks = 0;
- int n = 0;
- int main(){
- menu();
- return 0;
- }
- void menu ()
- {
- cout << endl; // menu opptions / entering 1 or 2 will take you to diffrent codes
- cout << " Please choose from the following options - \n";
- cout << " 1. Player vs player.\n";
- cout << " 2. Player vs Computer.\n";
- cin >> selection;
- if (selection == 1)
- {
- pvp ();
- }
- else if (selection == 2)
- {
- pvc ();
- }
- }
- // setting the pvp code
- void pvp () {
- // setting varibles
- string player1;
- string player2;
- char yn = 'y';
- // varibles for random turn
- int pick=1, win=0, pturn;
- srand(time(NULL));
- int random = rand();
- // rules
- cout << "Welcome to the 21 Sticks Game on c++" << endl << endl;
- cout << "The goal is to not be the person that has to take the last stick"
- << endl;
- cout << "You can only pick 1,2, or 3 sticks per turn" << endl;
- // getting names
- cout << "player 1 enter your name";
- cin >> player1;
- cout << "player 2 enter your name";
- cin >> player2;
- cout << endl << endl;
- if (random % 2 == 0)
- {
- cout << player1 << " goes first" << endl << endl;
- pturn = 0;
- }
- else
- {
- cout << player2 << " goes first" << endl << endl;
- pturn = 1;
- }
- //loop for player 1 turns
- const int total = 21;
- int n, remainder;
- bool winner = false;
- // varibles for player vs player
- remainder = total;
- while (!winner)
- {
- // code for the turns
- if (remainder >= 0)
- {
- cout << " There are " << remainder << " sticks. " << endl;
- cout << " Player 1, pick a number between 1 and 3. " << endl;
- cin >> n;
- }
- if ( n > 0 && n < 4)
- {
- cout << "You have removed " << n << " sticks." << endl;
- remainder -= n;
- }
- else
- {
- cout << "Invalid number" << endl;
- cout << "Please enter a number between 1 and 3." << endl;
- cin >>n;
- cout << " Player 1 removed " << n << " sticks." <<endl;
- remainder -=n;
- }
- if (remainder >= 0)
- {
- cout << "There are " << remainder << " sticks." << endl;
- cout << " Player 2, pick a number between 1 and 3." << endl;
- cin >> n;
- }
- if ( n > 0 && n < 4)
- {
- cout << "You have removed " << n << " sticks." << endl;
- remainder -= n;}
- else
- {
- cout << "Invalid number" << endl;
- cout << "Please enter a number between 1 and 3." << endl;
- cin >>n;
- cout << "You have removed " << n << " sticks." <<endl;
- remainder -=n;
- }
- if (remainder == 0)
- {
- winner = true;
- cout << "Player 1 wins!";
- }
- }
- }
- // finishing the player vs player code
- // player vs computer code
- void pvc () {
- int sticks=21,n=0;
- }
- void display_left_sticks()
- { sticks=sticks-n;
- cout<<" Remaining sticks = "<<sticks;
- if(sticks==1){cout<<"You loss";} // losing codes
- }
- // the code for the computer to win every time
- void cpu_stick_pick()
- {
- if(n==1){sticks=sticks-3;cout<<" Computer Picked 3 "; n=0;}
- else if(n==2){sticks=sticks-2;cout<<" Computer Picked 2 ";n=0;}
- else if(n==3){sticks=sticks-1;cout<<" Computer Picked 1 ";n=0;}
- }
- int joaquin ()
- {
- cout << "Welcome to the 21 Sticks Game on c++ against a computer" << endl << endl;
- cout << "The goal is to not be the person that has to take the last stick"
- << endl;
- cout << "You can only pick 1,2, or 3 sticks per turn" << endl;
- cout<<" We have 21 sticks ";
- do
- {
- cout<<" Your turn ";
- cin>>n; // setting limits
- if(n!=1&&n!=2&&n!=3){cout<<" Invalid input ";n=0; continue;}
- display_left_sticks();
- cpu_stick_pick();
- display_left_sticks();
- }
- while(sticks!=1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement