Hello,
I'm trying to code a simulation of a card game called Acey Ducey where the computer outputs two cards. Then the user chooses to bet money or not. Then, the computer outputs a third card. If the third card is between the first two cards then no money is lost for the user. Otherwise, the amount they bet is lost. I'm running the code, but it doesn't seem to work. When compiled the code keeps looping over and over again. The loops in my code have breaks, but the program seems to skip them! Please help...
#include<iostream>
#include<time.h>
usingnamespace std;
int main()
{
int money=100;
int card1,card2,card3;
char valid1[2]="n";
char valid2[2]="o";
int bet=0;
while(money!=0)
{
srand(time(0));
card1=rand() %13 +1;
card2=rand() %13 +1;
card3=rand() %13 +1;
while(valid1[2]=='n')
{
if(card1==card2 || card1==card3) // Check if cards are the same
{
srand(time(0));
card1=rand() %13 +1;
break;
}
elseif(card2==card1 || card2==card3)
{
srand(time(0));
card2=rand() %13 +1;
break;
}
elsebreak;
}
cout<<"Your cards are:"<<endl; // Display cards
if(card1==11)
{
cout<<"Jack"<<"\t"<<card2; // Checks for jacks, queens, or kings
}
elseif(card2==11)
{
cout<<"Jack"<<"\t"<<card1;
}
elseif(card1==12)
{
cout<<"Queen"<<"\t"<<card2;
}
elseif(card2==12)
{
cout<<"Queen"<<"\t"<<card1;
}
elseif(card1==13)
{
cout<<"King"<<"\t"<<card2;
}
elseif(card2==13)
{
cout<<"King"<<"\t"<<card1;
}
else
{
cout<<card1<<"\t"<<card2<<endl;
}
while(valid2[2]=='o')
{
cout<<"How much do you want to bet?"<<endl; // Asks for bet amount
cin>>bet;
if(bet>100)
{
cout<<"Bet is greater than $100!"<<endl<<endl; // Checks if bet is valid
continue;
}
elseif(bet<0)
{
cout<<"Bet is less than $0!"<<endl<<endl;
continue;
}
elseif(bet==0)
{
cout<<"CHICKEN!!!"<<endl;
break;
}
else
{
break;
}
}
if(card3>card1 && card3<card2)
{
cout<<"The next card, "<<card3<<", is between card 1 and card 2!"<<endl; // Outputs next card and how much money was lost if any
cout<<"You lost no money!"<<endl;
}
elseif(card3>card2 && card3<card1)
{
cout<<"The next card, "<<card3<<", is between card 1 and card 2!"<<endl;
cout<<"You lost no money!"<<endl;
}
else
{
money-=bet;
cout<<"Sorry but the next card isn't between the previous two cards!"<<endl;
cout<<"You lost "<<bet<<" dollars, leaving your total money as "<<money<<endl; // Loops to the beginning again
}
}
cout<<"I'm sorry but you lost all your money! Better luck next time!"<<endl; // Displays when money is 0
return 0;
}
I'm running the code, but it doesn't seem to work.
That's a very descriptive problem, let me get right on that... First of all I don't understand the premise of the game. Why would I play when the only two outcomes are to lose money and not lose money. How do you win? That aside I do see a few WTFs:
1 2 3 4 5 6 7 8 9 10
char valid1[2]="n"; //line 9
char valid2[2]="o";
//Why not just make these char's?
//Like this:
char valid1 = 'n',
valid2 = 'o';
//Or, if you want to use an array, like this:
char valid[2] = { 'n', 'o' };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
while( ... ) //line 18
{
...
srand(time(0)); //line 22
...
srand(time(0)); //line 28
...
}
//You are srand-ing inside a loop. You only need to seed the random number generator once!
//Just call it once before the loop starts. Like this:
srand( time(NULL) );
while( ... )
{
...
}
//That being said, in every case, your while-loop breaks. Why not just use an if-statement?
if(card1==11) //line 36
{
cout<<"Jack"<<"\t"<<card2;
}
elseif(card2==11)
{
cout<<"Jack"<<"\t"<<card1;
}
...
else //line 60
{
cout<<card1<<"\t"<<card2<<endl;
}
//This long else-if chain could most likely be replaced by a switch-statement.
//But first, it doesn't make sense that (card2==11) should depend on the previous (card1==11),
// so why is this an else-if at all?
//You most likely wanted something like:
switch(card1) {
case 11:
cout << "Jack";
break;
case 12:
cout << "Queen";
break;
case 13:
cout << "King";
break;
case 1: //You forgot this case in your code...
cout << "Ace";
break;
default:
cout << card1;
}
cout << "\t";
switch(card2) {
... //same as above
// (hint, hint... you could make this a function, or better yet, make card a class! ;)
}
cout << endl;
//There's an even faster yet messier way:
cout << (card1==1 ? "Ace" : card1==11 ? "Jack" : card1==12 ? "Queen" : card1==13 ? "King" : card1) << "\t";
cout << (card2==1 ? "Ace" : card2==11 ? "Jack" : card2==12 ? "Queen" : card2==13 ? "King" : card2) << endl;