|
-
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);
}
____________________________________________________
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
|