Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- int choice;
- int loop;
- int remaining;
- int take_Amount;
- int randomized_Amount;
- int count;
- string firstplayer_Name;
- string secondplayer_Name;
- string play_Again;
- //ensures program repeats
- loop = 1;
- while(loop == 1){
- //What would you like to do
- cout<<"Cube game"<<endl;
- cout<<"1) PLayer vs Player "<<endl;
- cout<<"2) Player vs Computer"<<endl;
- cout<<"3) Rules of Game"<<endl;
- //choose where to go
- cout<<"Enter the number 1, 2, or 3: ";
- cin>>choice;
- //Code run for player v player
- if(choice==1){
- count = 1;
- remaining = 21;
- //Asks players for names
- cout<<"Enter user 1 name: ";
- getline(cin, firstplayer_Name);
- getline(cin, firstplayer_Name);
- cout<<"Enter user 2 name: ";
- getline(cin, secondplayer_Name);
- cout<<"Number of cubes = "<< remaining <<endl;
- //It is decided randomly who get sto go first
- randomized_Amount = (rand() % 4) + 1;
- //Player 1 goes first
- if(randomized_Amount== 1 or randomized_Amount == 3){
- //loop
- while(count == 1){
- //User 1 inputs number of cubes to take out
- //Loop ends when valid input is entered
- do{
- cout<< firstplayer_Name <<" enter number of cubes you want to take out from 1-3: ";
- cin>>take_Amount;
- if(take_Amount < 1 or take_Amount > 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while(take_Amount < 1 or take_Amount > 3 );
- //Subtracts number of cubes by the number of cubes taken out
- remaining -=take_Amount;
- cout<<take_Amount<<endl;
- //If number of cubes is 1 then player 1 wins
- if(remaining == 1){
- cout<< firstplayer_Name <<" wins"<<endl;
- break;
- }
- //If number of cubes is 0 or less than 0 then player 2 wins
- if(remaining <= 0){
- cout<< secondplayer_Name <<" wins"<<endl;
- break;
- }
- //Player 2 inputs number of cubes to take out
- //Loop ends when valid input is entered
- do{
- cout<< secondplayer_Name <<" enter number of cubes you want to take out from 1-3: ";
- cin>> take_Amount;
- if(take_Amount < 1 or take_Amount > 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while(take_Amount < 1 or take_Amount > 3 );
- remaining -= take_Amount;
- cout<< remaining <<endl;
- //If number of cubes is 1 then user 2 wins
- if(remaining == 1){
- cout<< secondplayer_Name <<" wins"<<endl;
- break;
- }
- //If number of cubes is 0 or less than 0 then player 1 wins
- if(remaining <= 0){
- cout<< firstplayer_Name <<" wins"<<endl;
- break;
- }
- }
- //Gives 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>> play_Again;
- if( play_Again != "yes" and play_Again != "Yes" and play_Again!= "No" and play_Again != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
- //Goes back to start of loop if yes
- if(play_Again == "yes" or play_Again == "Yes"){
- continue;
- }
- //If yes the while loop at program keeps going so programs starts from top
- //If no, loop variable is set to 2 which ends while loop
- if(play_Again == "no" or play_Again == "No"){
- loop++;
- }
- }
- //Code for if random_start = 2 or 4
- //User 2 goes first
- if(randomized_Amount == 2 or randomized_Amount == 4){
- while(count == 1){
- //User 2 inputs number of cubes to take out
- //Loop ends when valid input is entered
- do{
- cout<< secondplayer_Name <<" enter number of cubes you want to take out from 1-3: ";
- cin>>take_Amount;
- if(take_Amount < 1 or take_Amount> 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while(take_Amount< 1 or take_Amount > 3 );
- remaining -= take_Amount;
- cout<<remaining<<endl;
- //If number of cubes is 1 then player 2 wins
- if( remaining == 1){
- cout<< secondplayer_Name <<" wins"<<endl;
- break;
- }
- //If number of cubes is 0 or less than 0 then user 1 wins
- if( remaining <= 0){
- cout<< firstplayer_Name <<" wins"<<endl;
- break;
- }
- //User 1 inputs number of cubes to take out
- //Loop ends when valid input is entered
- do{
- cout<< firstplayer_Name <<" enter number of cubes you want to take out(1-3): ";
- cin>> take_Amount;
- if( take_Amount < 1 or take_Amount > 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while(take_Amount < 1 or take_Amount > 3 );
- remaining -= take_Amount;
- cout<<remaining<<endl;
- //If number of cubes is 1 then player 1 wins
- if(remaining == 1){
- cout<< firstplayer_Name <<" wins"<<endl;
- break;
- }
- //If number of cubes is 0 or less than 0 then player 2 wins
- if( remaining <= 0){
- cout<< secondplayer_Name <<" wins"<<endl;
- break;
- }
- }
- do{
- cout<<"Do you want to play another round (yes or no): ";
- cin>>play_Again;
- if(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
- if(play_Again == "yes" or play_Again == "Yes"){
- continue;
- }
- if(play_Again == "no" or play_Again == "No"){
- loop++;
- }
- }
- }
- //Code run for player v computer
- if(choice==2){
- count = 1;
- remaining = 21;
- cout<<"Enter player 1 name: ";
- getline(cin, firstplayer_Name);//The first get line command doesnt work so this is important
- getline(cin, firstplayer_Name);
- cout<<"Number of cubes = "<<remaining<<endl;
- randomized_Amount = (rand() % 4) + 1;;
- //Code for if random start = 1 or 3
- //Player 1 goes first
- if(randomized_Amount == 1 or randomized_Amount == 3){
- while(count == 1){
- //Loop ends when valid input is entered
- do{
- cout<<firstplayer_Name<<" enter number of cubes you want to take out(1-3): ";
- cin>>take_Amount;
- if(take_Amount< 1 or take_Amount > 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while(take_Amount < 1 or take_Amount > 3 );
- remaining -= take_Amount;
- cout<<remaining<<endl;
- if(remaining == 1){
- cout<<firstplayer_Name<<" wins"<<endl;
- break;
- }
- //If number of cubes is 0 or less than 0 then computer wins
- if(remaining <= 0){
- cout<<"Computer wins"<<endl;
- break;
- }
- //Code for computer
- //Computer takes out the certain amount cubes so that the two turns equals 4
- take_Amount = 4 - take_Amount;
- remaining -= take_Amount;
- cout<<"Computer takes out "<< take_Amount <<" cubes."<<endl;
- cout<< remaining <<endl;
- //If number of cubes is 1 then computer wins
- if(remaining == 1){
- cout<<"Computer wins"<<endl;
- break;
- }
- //If number of cubes is 0 or less than 0 then user 1 wins
- if(remaining <= 0){
- cout<<firstplayer_Name<<" wins"<<endl;
- break;
- }
- }
- do{
- cout<<"Do you want to play another round(yes or no): ";
- cin>> play_Again ;
- if(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
- if(play_Again == "yes" or play_Again == "Yes"){
- continue;
- }
- if(play_Again == "no" or play_Again == "No"){
- loop++;
- }
- }
- //Code for if random start = 2 or 4
- //Computer goes first
- if(randomized_Amount == 2 or randomized_Amount == 4){
- while(count == 1){
- //Number of sticks is over 4 then computer takes out a random number between 1 and 3
- if(remaining>4){
- take_Amount = (rand() % 3) + 1;
- }
- //Number of sticks is equal to 4 then it takes out 3
- if(remaining==4){
- take_Amount=3;
- }
- if(remaining ==3){
- take_Amount=2;
- }
- //Number of sticks is equal to 2 then it takes out 1
- if(remaining ==2){
- take_Amount =1;
- }
- remaining -=take_Amount;
- cout<<"Computer takes out "<<take_Amount<<" cubes"<<endl;
- cout<< remaining <<endl;
- //If number of sticks is 1 then computer wins
- if(remaining == 1){
- cout<<"Computer wins"<<endl;
- count = 2;
- break;
- }
- //If number of sticks is 0 or less than 0 then Player 1 wins
- if(remaining <= 0){
- cout<< firstplayer_Name <<" wins"<<endl;
- count = 2;
- break;
- }
- //User 1 inputs number of sticks to take out
- //Loop ends when valid input is entered
- do{
- cout<< firstplayer_Name <<" enter number of cubes you want to take out(1-3): ";
- cin>>take_Amount;
- if(take_Amount < 1 or take_Amount > 3 ){
- cout<<"Invalid input since it is not inbetween 1-3."<<endl;
- }
- }while( take_Amount < 1 or take_Amount > 3 );
- remaining -= take_Amount;
- cout<< remaining <<endl;
- //If number of cubes is 1 then user 1 wins
- if(remaining == 1){
- cout<< firstplayer_Name <<" wins"<<endl;
- count = 2;
- break;
- }
- //If number of cubes is 0 or less than 0 then computer wins
- if( remaining <= 0){
- cout<<"Computer wins"<<endl;
- count = 2;
- break;
- }
- }
- do{
- cout<<"Do you want to play another round(yes or no): ";
- cin>> play_Again;
- if(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no"){
- cout<<"Invalid input since you did not enter yes or no";
- }
- }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
- if(play_Again == "yes" or play_Again == "Yes"){
- continue;
- }
- if(play_Again == "no" or play_Again == "No"){
- loop++;
- }
- }
- }
- //Code for rules of game
- if(choice==3){
- cout<<"Rules of 21 cubes"<<endl;
- cout<<"User must take out between 1-3 sticks from the total."<<endl;
- cout<<"Last person to take out cubes loses the game. "<<endl;
- }
- else{
- cout<<"Invalid input since you did not enter 1, 2, or 3. "<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement