-
November 19th, 2008, 07:32 PM
#1
Loop Problem with game of 21
Ok, Here is my problem I made a program that is a simple game of 21, but when I run it all I get is an infinite loop that reads...
You are over 21. You loose!
Any suggestions would be greatly appreciated...
Here is my code...
_____________________________________
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
//prototypes...
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);
void main(){
char keepPlaying = 'n'; //loop control variable
do {
play21();
//keep playing?
cout << "Do you want to play anouther hand (y/n)?";
cin >> keepPlaying;
} while(keepPlaying == 'Y' || keepPlaying == 'y');
}
void play21(void){
//play one hand of 21.
//randomize the cards.
srand((int) time(0));
// deal the cards.
int person = dealCards(2, "Your Cards:");
cout << " = " << person << endl;
int house = dealCards(2, "Computers Cards:");
cout << " = " << house << endl;
// Ask if human wants a hit and keep hitting...
hit(person);
cout << endl;
//Determine if computer takes a hit.
while ((house < person) && (house <= 21) && (person <= 21)) {
house += dealCards(1, "The Computer takes a card ");
cout << endl;
}
//show who won....
determineWinner(person, house);
}
void determineWinner(int humanScore, int houseScore) {
while ((humanScore <= 21) && (humanScore < 0)) //Compare the scores.
if (humanScore == 21)
{
cout << "You have 21. You win!" << endl;
}
else if ((humanScore < 21) && (humanScore > houseScore))
{
cout << "You have the closer hand to 21. You win!" << endl;
}
//outcomes: human wins, computer wins, tie.
}
int dealCards(int numberOfCards, string message){
int sumOfCards = 0;
for (int a = 0; a <= numberOfCards; a++) //This function deals the cards.
{
//Random();
return sumOfCards;
}
}
void hit(int &playerScore){
char anotherCard = 'n';
//int cardCount = 0;
int cardTotal = 0;
cardTotal = playerScore;
cout << "Would you like another card?";
{while ((anotherCard == 'Y' || 'y'))
if ((cardTotal > 0 ) && (cardTotal <= 21))
{
//cardCount += 1;
//cardTotal += Random();
cout << " " << cardTotal << endl;
cout << "Would you like another card?";
cin >> anotherCard;
}
else
{
cout << "You are over 21. You loose!" << endl;
}
}
}
int Random(int lowerLimit, int upperLimit) {
//returns a random number within the boundary.
return 1 + rand() % (upperLimit - lowerLimit + 1);
}
____________________________________________________
-
November 19th, 2008, 08:10 PM
#2
Re: Loop Problem with game of 21
1) Try to use code tags. They help others in reading your code.
2) infinite loops can only appear where there are looping like functions. For example
Code:
for(int a = 5; a > 2; a++)
will loop forever because it will never meet the breaking conditions (a >2)
Do you see any loops in your code in which the breaking condition is never met?
-
November 19th, 2008, 08:13 PM
#3
Re: Loop Problem with game of 21
Originally Posted by jakedasnake
1) Try to use code tags. They help others in reading your code.
2) infinite loops can only appear where there are looping like functions. For example
Code:
for(int a = 5; a > 2; a++)
will loop forever because it will never meet the breaking conditions (a >2)
Do you see any loops in your code in which the breaking condition is never met?
Sort of. It's not a "breaking condition", it's continue looping while the expression is true. In this case, a will be > 2 until it overflows, so it will loop for a long time.
-
November 19th, 2008, 08:24 PM
#4
Re: Loop Problem with game of 21
Thanks for the correction. I wasn't thinking about nomenclature and I didn't think about the overflow later on. I typically stop it after it re-iterates my code about 10 times more than it should and call it infinite!
Decipil, what I should have said was functions such as FOR and WHILE continue to loop while a certain expression is true (in my previous example the expression was a > 2). Your textbook should be able to show you how to structure both types of loops and also show how and where the expression goes to control the loop
-
November 19th, 2008, 09:05 PM
#5
Re: Loop Problem with game of 21
Originally Posted by Decipil
You are over 21. You loose!
One thing -- the word is lose, not loose.
"Loose" is the opposite of "tight". "Lose" is the opposite of "win".
Regards,
Paul McKenzie
-
November 19th, 2008, 10:00 PM
#6
Re: Loop Problem with game of 21
Your code is almost impossible to read without tags and indentation, but I believe this line is causing your problem.
while ((anotherCard == 'Y' || 'y'))
-
November 19th, 2008, 10:19 PM
#7
Re: Loop Problem with game of 21
Sorry about the indentions, when I posted it I thought it showed them, but what might be the problem with that piece of code, I have been working on for a while now and can't seem to figure out what is wrong with it.
-
November 19th, 2008, 10:35 PM
#8
Re: Loop Problem with game of 21
Originally Posted by Decipil
Sorry about the indentions, when I posted it I thought it showed them, but what might be the problem with that piece of code, I have been working on for a while now and can't seem to figure out what is wrong with it.
In C++, anything that evaluates to non-zero is true. 'y' is not zero, therefor that expression is always true.
You really meant
while (anotherCard == 'Y' || anotherCard == 'y')
I don't know if that's your only problem, but it's certainly one of them.
To maintain indentations put [ code ] before your code and [ /code ] after, but don't include the spaces before and after the brackets.
-
November 19th, 2008, 11:21 PM
#9
Re: Loop Problem with game of 21
Sweet deal, thanks a load I really appreciate it, that solved the problem with the loop, now all I got to do is figure out how to get the random numbers to work instead of just showing zeros.
Thank Again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|