Heloo im trying to make this program function the player has to win two rounds of dice 21 in order to win if he loses two rounds they lose. This is the code I have im trying to make it function but Im having trouble with the for loop.
The player has two roll 21 to win the round.
If they fail to get 21 they lose the round
If the player decides not to roll and finds out the next number would have cost him to lose they win the round.
The player has to win 2 out of 3 rounds to win the game. The trouble Im having is trying to make the foor loop work.
#include<iostream>
#include<cstdlib>
#include<time.h>
usingnamespace std;
int getRandomNum( int lowRange, int highrange);
int main()
{
char choice=' ';
char keep=' ';
srand( static_cast<int>( time(NULL) ) );
///////////////////////////// Start of the code
cout<<"**************** Welcome to the Dice 21 game! ****************"<<endl;
cout<<endl;
cout<<"You must win the majority of rounds to win the game. In each";
cout<<"round you will roll one 10-sided die as many times as you like.";
cout<<"Each time you roll, the number on the die will be added to your";
cout<<"total score. Your goal is to get a score as close as possible to";
cout<<"21 without going over 21.";
cout<<endl;
cout<<endl;
cout<<"At the end of a round, you will be able to look ahead to see";
cout<<" what the next roll would have been. You win the round if the";
cout<<" next roll would have caused you to go bust. Otherwise you lose";
cout<<" the round"<<endl;
cout<<endl;
cout<<"********** Round 1 *********"<<endl;
int rollValue= 0;
int total_turn= 0;
int value1= 0;
int value2= 0;
int tries= 0;
int loss= 0;
int round= 0;
for(round=0; round>=2; round++)
{
do
{
if(rollValue==1)
{
cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
cout<<"Enter 'y' if you would like the value to be 11"<<endl;
cout<<"Enter any other character if you would like to keep the value 1"<<endl;
cin>> keep;
if(keep=='y')
{
value1= 11;
cout<<"You rolled:"<<value1<<endl;
total_turn= total_turn + value1;
cout<<"Your roll total is:"<<total_turn<<endl;
cout<<endl;
}
else
{
value2= 1;
cout<<"You rolled:"<<value2<<endl;
total_turn= total_turn+ value2;
cout<<"Your roll total is :"<< total_turn<<endl;
cout<<endl;
}
}
cout<<"Rolling..."<<endl;
rollValue = rand() % 10 + 1;
cout<<"You rolled a :"<<rollValue<<endl;
total_turn= total_turn + rollValue;
cout<<"Your roll tatal is :"<<total_turn<<endl;
cout<<endl;
cout<<"Do you want to roll again: (y/n)";
cin>> choice;
if(total_turn==21)
{
cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
round++;
break;
}
elseif(total_turn>21)
{
cout<<"Sorry you lose you rolled over 21"<<endl;
break;
}
if(choice=='y')
{
if(rollValue==1)
{
cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
cout<<"Enter 'y' if you would like the value to be 11"<<endl;
cout<<"Enter any other character if you would like to keep the value 1"<<endl;
cin>> keep;
if(keep=='y')
{
value1= 11;
cout<<"You rolled:"<<value1<<endl;
total_turn= total_turn + value1;
cout<<"Your roll total is:"<<total_turn<<endl;
cout<<endl;
}
else
{
value2= 1;
cout<<"You rolled:"<<value2<<endl;
total_turn= total_turn+ value2;
cout<<"Your roll total is"<< total_turn<<endl;
cout<<endl;
}
}
cout<<"Rolling..."<<endl;
rollValue = rand() % 10 + 1;
cout<<"You rolled a :"<<rollValue<<endl;
total_turn= total_turn + rollValue;
cout<<"Your roll tatal is :"<<total_turn<<endl;
if(total_turn==21)
{
cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
break;
}
elseif(total_turn>21)
{
cout<<"Sorry you lose you rolled over 21"<<endl;
break;
}
cout<<endl;
cout<<"Do you want to roll again: (y/n)";
cin>> choice;
}
elseif(choice=='n')
{
cout<<"Your roll total is:"<<total_turn<<endl;
rollValue = rand() % 10 + 1;
cout<<"Look ahead to the next roll..."<<endl;
cout<<"Your next roll would have been:"<<rollValue<<endl;
total_turn=total_turn+rollValue;
if(total_turn>21)
{
cout<<"Good choice you would have one BUST, if you have rolled again"<<endl;
round++;
break;
}
elseif(total_turn<21)
{
cout<<"BUMMER- you could have rolled again"<<endl;
break;
}
}
else
{
cout<<"invalid input"<<endl;
break;
}
}while(total_turn<= 21);
}
system("pause");
return 0;
}
As it stands your program will never ever ever ever.... go in to the for loop because you are imposing the condition that it go in to the loop only if round is greater than 1. The post made by long will allow it to go in to the for loop.
If you want to play the game 3 times then put what long said. Also you need to reset the value of total_turn to 0 every new game. So put total_turn = 0 in the for loop just before the while loop and start fixing the code from there.
EDIT:There are other problems with the program for instance the player doesn't want to roll the dice yet you still add the roll value to total_turn. Instead you should add in the code int temp = 0 after line 162 and in line 167 have temp = total_turn + rollValue and check temp rather than total_turn.
EDIT2:"how would i update it to increase the round count in the for loop"
The code for(round=0; round<3; round++) means "start with round = 0 then increment round by 1 every time it goes in to the for loop until round is greater than 2". So round increases each time because you have "round++" there.
thanks Krahl. but I only have to play 3 rounds of dice 21 and only 3. You have to win 2 out of three rounds in order to win the game. If you lose 2 out of 3 you lose the game.
The player wins if the game if they win 2 rounds and roll 21 or decide to not roll and find out the next number would make them lose or go bust.
The player loses the game if they lose 2 rounds if they roll greater than 21 or decide not to roll and find out if they could have rolled again.
this is my main problem and I know it's in the for loop. The thing is i dont know what to put in the for loop the rounds won, or rounds lost, or how many rounds.
Wow that is a complicated game. Ok I think I understand. However you still need to do the corrections we have suggested. In addition to these corrections you need to have a variable to record the wins.
So the win conditions are.
1- win 2 games and roll 21
or
2- win 2 games and choose not to roll and find out if you had rolled you would have bust.