CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2008
    Posts
    13

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

    ____________________________________________________

  2. #2
    Join Date
    Sep 2008
    Location
    Boulder, CO
    Posts
    16

    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?

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Loop Problem with game of 21

    Quote Originally Posted by jakedasnake View Post
    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.

  4. #4
    Join Date
    Sep 2008
    Location
    Boulder, CO
    Posts
    16

    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Loop Problem with game of 21

    Quote Originally Posted by Decipil View Post
    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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'))

  7. #7
    Join Date
    Nov 2008
    Posts
    13

    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.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Loop Problem with game of 21

    Quote Originally Posted by Decipil View Post
    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.

  9. #9
    Join Date
    Nov 2008
    Posts
    13

    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
  •  





Click Here to Expand Forum to Full Width

Featured