Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Paul Fynn Kuemmel
- // This is a code which mimics the game 21 sticks, in which 2 players take turns either taking 1, 2 or 3 sticks
- // Eventually the person who's turn it is when 1 stick remains, is the loser.
- #include <stdio.h>
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- //Declares the variables which will be used in the game.
- int GameChoice, loop, StickNum, sticksTaken, playerTurn, counter;
- string Player1Name, Player2Name, NewGame;
- //Loops while the player still wants to play the game
- loop = 1;
- while(loop == 1){
- //This is the starting menu and introduction to the game
- cout << "Hello! Welcome to 21 sticks! The rules are as following: " << endl;
- cout << "1. You can only select between 1, 2 or 3 sticks" << endl;
- cout << "2. The last person to take the stick loses" << endl;
- cout << "Game options: " << endl;
- cout << "1) Play vs Computer: " << endl;
- cout << "2) Play vs Friend: " << endl;
- cout << "Enter either 1 or 2 to choose: ";
- cin >> GameChoice;
- //This occurs when the player decides to play against a friend
- if(GameChoice==2){
- counter = 1;
- //Total number of sticks
- StickNum = 21;
- //Asks the players for their names
- cout<<"Enter the name of Player 1: ";
- getline(cin, Player1Name);//This ensures that the data is properly collected and stored.
- getline(cin, Player1Name);
- cout<<"Enter the name of Player 2: ";
- getline(cin, Player2Name);
- cout<<"Number of sticks = "<<StickNum<<endl;
- //Gets a random number to see who will started the game
- playerTurn = (rand() % 4) + 1;
- //If the random number is either 1 or 3, then player 1 will start
- if(playerTurn == 1 or playerTurn == 3){
- //Loops uring the turn of player 1
- while(counter == 1){
- //player 1 inputs number of sticks to take out, once a correct value is entered, the loop ends and changes the turn
- do{
- cout<<Player1Name<<" ,Enter number of sticks you want to take out, keep it between 1-3: ";
- cin>>sticksTaken;
- if(sticksTaken < 1 or sticksTaken > 3 ){
- cout<<"The input isn't between the boundaries, so it is invalid."<<endl;
- }
- }while(sticksTaken < 1 or sticksTaken > 3 );
- //Subtracts the selection from the original total
- StickNum-=sticksTaken;
- cout<<StickNum<<endl;
- //If number of sticks is 1 then player 1 wins
- if(StickNum == 1){
- cout<<Player1Name<<" Wins!"<<endl;
- break;
- }
- //If number of sticks is 0 or less than 0 then player 2 wins
- if(StickNum <= 0){
- cout<<Player2Name<<" Wins!"<<endl;
- break;
- }
- //Now, player 2 enters their selection
- //Loop ends when valid input is entered
- do{
- cout<<Player2Name<<" ,Enter the number of sticks you want to take out 1-3: ";
- cin>>sticksTaken;
- if(sticksTaken < 1 or sticksTaken > 3 ){
- cout<<"Invalid input since it is not between 1-3."<<endl;
- }
- }while(sticksTaken < 1 or sticksTaken > 3 );
- StickNum-=sticksTaken;
- cout<<StickNum<<endl;
- //If number of sticks is 1 then player 2 wins
- if(StickNum == 1){
- cout<<Player2Name<<" Wins!"<<endl;
- break;
- }
- //If number of sticks is 0 or less than 0 then player 1 wins
- if(StickNum <= 0){
- cout<<Player1Name<<" Wins!"<<endl;
- break;
- }
- }
- //Gives the option of playing another round
- //Loop ends when vaid input is entered
- do{
- cout<<"Do you want to play another round (Yes or No)? ";
- cin>>NewGame;
- if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
- //Returns to the beginning if Yes or No is entered
- if(NewGame == "yes" or NewGame == "Yes"){
- continue;
- }
- //If yes the while loop at program restarts, and the code continues
- //If no, loop variable is set to 2 which ends the while loop
- if(NewGame == "no" or NewGame == "No"){
- loop++;
- }
- }
- //Now this code is if player 2 starts first, with the random number being either 2 or 4
- if(playerTurn == 2 or playerTurn == 4){
- while(counter == 1){
- //Player 2 inputs the number of sticks to be taken out
- //Loop ends when valid input is entered
- do{
- cout<<Player2Name<<" ,enter number of sticks you want to take out(1-3): ";
- cin>>sticksTaken;
- if(sticksTaken < 1 or sticksTaken > 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while(sticksTaken < 1 or sticksTaken > 3 );
- StickNum -= sticksTaken;
- cout<<StickNum<<endl;
- //If number of sticks remaining is 1 then player 2 wins
- if(StickNum == 1){
- cout<<Player2Name<<" Wins!"<<endl;
- break;
- }
- //If number of sticks is 0 or less than 0 then player 1 wins
- if(StickNum <= 0){
- cout<<Player1Name<<" Wins!"<<endl;
- break;
- }
- //Player 1 inputs the number of sticks to take out
- //Loop ends when valid input is entered
- do{
- cout<<Player1Name<<" ,enter number of sticks you want to take out(1-3): ";
- cin>>sticksTaken;
- if(sticksTaken < 1 or sticksTaken > 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while(sticksTaken < 1 or sticksTaken > 3 );
- StickNum -= sticksTaken;
- cout<<StickNum<<endl;
- //If number of sticks is 1 then player 1 wins
- if(StickNum == 1){
- cout<<Player1Name<<" Wins!"<<endl;
- break;
- }
- //If number of sticks is 0 or less than 0 then player 2 wins
- if(StickNum <= 0){
- cout<<Player2Name<<" Wins!"<<endl;
- break;
- }
- }
- //Asks the user if they want to play another round, and restart the loop
- do{
- cout<<"Do you want to play another round (yes or no) ? ";
- cin>>NewGame;
- if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
- if(NewGame == "yes" or NewGame == "Yes"){
- continue;
- }
- if(NewGame == "no" or NewGame == "No"){
- loop++;
- }
- }
- }
- //Now, this is the code which runs if the player decides to attempt to defeat the Computer.
- if(GameChoice==1){
- counter = 1;
- StickNum = 21;
- cout<<"Enter player 1 name: ";
- getline(cin, Player1Name);
- getline(cin, Player1Name);
- cout<<"Number of sticks = "<<StickNum<<endl;
- playerTurn = (rand() % 4) + 1;;
- if(playerTurn == 1 or playerTurn == 3){
- while(counter == 1){
- //Player 1 inputs the number of sticks to take out
- //Loop ends when valid input is entered
- do{
- cout<<Player1Name<<" ,Enter number of sticks you want to take out(1-3): ";
- cin>>sticksTaken;
- if(sticksTaken < 1 or sticksTaken > 3 ){
- cout<<"Invalid input since it is not between 1-3."<<endl;
- }
- }while(sticksTaken < 1 or sticksTaken > 3 );
- StickNum -= sticksTaken;
- cout<<StickNum<<endl;
- //If number of sticks is 1 then player 1 wins
- if(StickNum == 1){
- cout<<Player1Name<<" Wins!"<<endl;
- break;
- }
- //If number of sticks is 0 or less than 0 then the computer wins
- if(StickNum <= 0){
- cout<<"Computer Wins!"<<endl;
- break;
- }
- //Code for computer to use to win.
- //Computer takes out the certain amount sticks so that the sticks taken over the 2 turns, always equals to 4.
- sticksTaken = 4 - sticksTaken;
- StickNum -= sticksTaken;
- cout<<"Computer takes out "<<sticksTaken<<" sticks."<<endl;
- cout<<StickNum<<endl;
- //If number of sticks is 1 then computer wins
- if(StickNum == 1){
- cout<<"Computer Wins!"<<endl;
- break;
- }
- //If number of sticks is 0 or less than 0 then player 1 wins
- if(StickNum <= 0){
- cout<<Player1Name<<" Wins!"<<endl;
- break;
- }
- }
- do{
- cout<<"Do you want to play another round(yes or no): ";
- cin>>NewGame;
- if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
- if(NewGame == "yes" or NewGame == "Yes"){
- continue;
- }
- if(NewGame == "no" or NewGame == "No"){
- loop++;
- }
- }
- //Computer goes first
- if(playerTurn == 2 or playerTurn == 4){
- while(counter == 1){
- //If the Number of sticks is over 4 then computer takes out a random number between 1 and 3, to make it fairer
- if(StickNum>4){
- sticksTaken = (rand() % 3) + 1;
- }
- // If the Number of sticks is equal to 4 then it takes out 3
- if(StickNum==4){
- sticksTaken=3;
- }
- // If the Number of sticks is equal to 3 then it takes out 2
- if(StickNum==3){ // All of these lines ensure victory for the Computer
- sticksTaken=2;
- }
- // If the Number of sticks is equal to 2 then it takes out 1
- if(StickNum==2){
- sticksTaken=1;
- }
- StickNum-=sticksTaken;
- cout<<"Computer removes "<<sticksTaken<<" sticks."<<endl;
- cout<<StickNum<<endl;
- //If number of sticks is 1 then computer wins
- if(StickNum == 1){
- cout<<"Computer wins!"<<endl;
- counter = 2;
- break;
- }
- //If number of sticks is 0 or less than 0 then player 1 wins
- if(StickNum <= 0){
- cout<<Player1Name<<" Wins!"<<endl;
- counter = 2;
- break;
- }
- /
- do{
- cout<<Player1Name<<" ,Enter number of sticks you want to take out(1-3): ";
- cin>>sticksTaken;
- if(sticksTaken < 1 or sticksTaken > 3 ){
- cout<<"Invalid input since it is not between 1-3."<<endl;
- }
- }while(sticksTaken < 1 or sticksTaken > 3 );
- StickNum-=sticksTaken;
- cout<<StickNum<<endl;
- //If number of sticks is 1 then player 1 wins
- if(StickNum == 1){
- cout<<Player1Name<<" Wins!"<<endl;
- counter = 2;
- break;
- }
- //If number of sticks is 0 or less than 0 then computer wins
- if(StickNum <= 0){
- cout<<"Computer Win!"<<endl;
- counter = 2;
- break;
- }
- }
- do{
- cout<<"Do you want to play another round(yes or no): ";
- cin>>NewGame;
- if(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(NewGame!= "yes" and NewGame != "Yes" and NewGame != "No" and NewGame != "no");
- if(NewGame == "yes" or NewGame == "Yes"){
- continue;
- }
- if(NewGame == "no" or NewGame == "No"){
- loop++;
- }
- }
- }
- else{
- cout<<"Invalid input since you did not enter 1, 2, or 3. "<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement