Thread: My first c++ game

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    My first c++ game

    it's a simple game, you've got to guess whats the number thats been randomly generated. I cant help having the feeling that this code could be cleaner, I'd like suggestions from more experienced programmers.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    void game();
    string turnString(int n);
    string clue(int n, int randNumber);
    string comments(int turn);
    
    int playAgain;
    
    int main()
    {
        do
        {
            game();
    
            cout<<"\n\n\nPLAY AGAIN? (TYPE 1 FOR YES): ";
            cin>>playAgain;
            cout<<"\n\n\n";
    
        }
        while(playAgain == 1);
    }
    
    void game()
    {
    
    srand(time(0));
    int randNum = (rand() % 15) + 1;
    
    int chances = 5;
    int turn = 1;
    int input;
    
    cout<<"Random number has been generated (1 to 15),\nyou've got 5 chances to guess it. GOOD LUCK!"<<endl;
    
    cin.get();
    
    while(turn <= 5 && input != randNum)
    {
    
    cout<<"\nGuess Number "<<turn<<": ";
    cin>>input;
    
    cout<<clue(input,randNum)<<endl;
    
    turn++;
    
    cin.get();
    
    }
    
    turn--;
    
    if(input == randNum)
    {
        cout<<"\n\nCongratulations! You've guessed it!"<<endl;
        cin.get();
        cout<<"You've done it on the "<<turnString(turn)<<" turn.";
        cin.get();
        cout<<"\nComments: "<<comments(turn);
        cin.get();
    }
    else
    {
        cout<<"\n\nYou've run outta chances!\nThe random number was "<<randNum<<".\nTRY AGAIN, SUCKER!";
        cin.get();
        cin.get();
    }
    
    
    }
    
    string turnString(int n)
    {
        string stringReturn;
    
        switch(n)
        {
            case 1:
            stringReturn = "first";
            break;
    
            case 2:
            stringReturn = "second";
            break;
    
            case 3:
            stringReturn = "third";
            break;
    
            case 4:
            stringReturn = "fourth";
            break;
    
            case 5:
            stringReturn = "fifth";
            break;
        }
    
        return stringReturn;
    
    }
    
    string clue(int n,int randNumber)
    {
    
        string stringReturn;
    
        if(randNumber != n && randNumber > n)
        {
            stringReturn = "Try higher.";
        }
    
        if(randNumber != n && randNumber < n)
        {
            stringReturn = "Try lower.";
        }
    
        return stringReturn;
    
    }
    
    string comments(int turn)
    {
    
       string comments[6];
       comments[1]="FLAWLESS VICTORY!";
       comments[2]="Good job!";
       comments[3]="Hmmm... not bad.";
       comments[4]="You could've done better.";
       comments[5]="You saved your ass this time, pal.";
    
        return comments[turn];
    
    }
    Last edited by dhuan; 08-08-2010 at 01:23 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You made some attempt at indentation, and that is good, but it can still be improved, particularly for the main function. Note that the expression (randNumber != n && randNumber > n) can be simplified to (randNumber > n).

    Instead of always defining the array of strings for each call of your comments function, you could define a static array of const strings instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    main function? Surely you mean the game function since main is already properly indented.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Hi, i've made a few changes in my code, i've made it possible to play on different difficulty levels, but now i have a problem, here's my code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    int level;
    bool isLevelSet;
    
    void game();
    string turnString(int n);
    string clue(int n, int randNumber);
    string comments(int turn);
    
    int playAgain;
    
    int main()
    {
        do
        {
            game();
    
            cout<<"\n\n\nPLAY AGAIN? (TYPE 1 FOR YES): ";
            cin>>playAgain;
            cout<<"\n\n\n";
    
        }
        while(playAgain == 1);
    }
    
    void game()
    {
    
    if(isLevelSet == false){srand(time(0));};
    
    int randNum;
    int range[3];
    int chances = 5;
    int turn = 1;
    int input;
    
    cout<<"Random number will be generated.\nYou've got 5 chances to guess it."<<endl;
    
    if(isLevelSet == false)
    {
    
    
    cout<<"\n\nSELECT DIFFICULTY LEVEL: \n";
    cout<<"1. EASY (10 Numbers)\n2. NORMAL (15 Numbers)\n3. HARD (20 Numbers)\n";
    cout<<"Command: ";
    
    cin>>level;
    
    range[1] = 10;
    range[2] = 15;
    range[3] = 20;
    
    randNum = (rand() % range[level]) + 1;
    
    //cout<<"Range is "<<range[level];
    
    isLevelSet = true;
    
    }
    
    while(turn <= 5 && input != randNum)
    {
    
    cout<<"\nGuess Number "<<turn<<": ";
    cin>>input;
    
    cout<<clue(input,randNum)<<endl;
    
    turn++;
    
    cin.get();
    
    }
    
    turn--;
    
    if(input == randNum)
    {
        cout<<"\n\nCongratulations! You've guessed it!"<<endl;
        cin.get();
        cout<<"You've done it on the "<<turnString(turn)<<" turn.";
        cin.get();
        cout<<"\nComments: "<<comments(turn);
        cin.get();
    }
    else
    {
        cout<<"\n\nYou've run outta chances!\nThe random number was "<<randNum<<".\nTRY AGAIN, SUCKER!";
        cin.get();
        cin.get();
    }
    
    
    }
    
    string turnString(int n)
    {
        string stringReturn;
    
        switch(n)
        {
            case 1:
            stringReturn = "first";
            break;
    
            case 2:
            stringReturn = "second";
            break;
    
            case 3:
            stringReturn = "third";
            break;
    
            case 4:
            stringReturn = "fourth";
            break;
    
            case 5:
            stringReturn = "fifth";
            break;
        }
    
        return stringReturn;
    
    }
    
    string clue(int n,int randNumber)
    {
    
        string stringReturn;
    
        if(randNumber != n && randNumber > n)
        {
            stringReturn = "Try higher.";
        }
    
        if(randNumber != n && randNumber < n)
        {
            stringReturn = "Try lower.";
        }
    
        return stringReturn;
    
    }
    
    string comments(int turn)
    {
    
       string comments[6];
       comments[1]="FLAWLESS VICTORY!";
       comments[2]="Good job!";
       comments[3]="Hmmm... not bad.";
       comments[4]="You could've done better.";
       comments[5]="You saved your ass this time, pal.";
    
        return comments[turn];
    
    }
    the problem is, after you select the level, it sets the range int so it defines how far the random number can go, this is okay up until your first game, now, when you play it for the second time, the range goes immensely far.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, indentation. Your code is unreadable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    No code within a function will touch the left margin.
    Code:
    int main()
    {
    do_something();
    this_guy = that_guy;
    }
    Is much better read as
    Code:
    int main()
    {
       do_something();
       this_guy = that_guy;
    }
    I used three spaces.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Then here's the indented code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    int level;
    bool isLevelSet;
    
    void game();
    string turnString(int n);
    string clue(int n, int randNumber);
    string comments(int turn);
    
    int playAgain;
    
    int main()
    {
        do
        {
            game();
    
            cout<<"\n\n\nPLAY AGAIN? (TYPE 1 FOR YES): ";
            cin>>playAgain;
            cout<<"\n\n\n";
    
        }
        while(playAgain == 1);
    }
    
    void game()
    {
    
        if(isLevelSet == false){srand(time(0));};
    
        int randNum;
        int range[3];
        int chances = 5;
        int turn = 1;
        int input;
    
        cout<<"Random number will be generated.\nYou've got 5 chances to guess it."<<endl;
    
        if(isLevelSet == false)
        {
    
    
            cout<<"\n\nSELECT DIFFICULTY LEVEL: \n";
            cout<<"1. EASY (10 Numbers)\n2. NORMAL (15 Numbers)\n3. HARD (20 Numbers)\n";
            cout<<"Command: ";
    
            cin>>level;
    
            range[1] = 10;
            range[2] = 15;
            range[3] = 20;
    
            randNum = (rand() % range[level]) + 1;
    
            isLevelSet = true;
    
        }
    
        while(turn <= 5 && input != randNum)
        {
    
            cout<<"\nGuess Number "<<turn<<": ";
            cin>>input;
    
            cout<<clue(input,randNum)<<endl;
    
            turn++;
    
            cin.get();
    
        }
    
        turn--;
    
        if(input == randNum)
        {
            cout<<"\n\nCongratulations! You've guessed it!"<<endl;
            cin.get();
            cout<<"You've done it on the "<<turnString(turn)<<" turn.";
            cin.get();
            cout<<"\nComments: "<<comments(turn);
            cin.get();
        }
        else
        {
            cout<<"\n\nYou've run outta chances!\nThe random number was "<<randNum<<".\nTRY AGAIN, SUCKER!";
            cin.get();
            cin.get();
        }
    
    
    }
    
    string turnString(int n)
    {
        string stringReturn;
    
        switch(n)
        {
            case 1:
            stringReturn = "first";
            break;
    
            case 2:
            stringReturn = "second";
            break;
    
            case 3:
            stringReturn = "third";
            break;
    
            case 4:
            stringReturn = "fourth";
            break;
    
            case 5:
            stringReturn = "fifth";
            break;
        }
    
        return stringReturn;
    
    }
    
    string clue(int n,int randNumber)
    {
    
        string stringReturn;
    
        if(randNumber != n && randNumber > n)
        {
            stringReturn = "Try higher.";
        }
    
        if(randNumber != n && randNumber < n)
        {
            stringReturn = "Try lower.";
        }
    
        return stringReturn;
    
    }
    
    string comments(int turn)
    {
    
       string comments[6];
       comments[1]="FLAWLESS VICTORY!";
       comments[2]="Good job!";
       comments[3]="Hmmm... not bad.";
       comments[4]="You could've done better.";
       comments[5]="You saved your ass this time, pal.";
    
        return comments[turn];
    
    }
    I cant help having the feeling that this code could be cleaner, I'd like suggestions from more experienced programmers.

    Another problem is, after you select the level, it sets the range int so it defines how far the random number can go, this is okay up until your first game, now, when you play it for the second time, the range goes immensely far.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's how I would do it:
    1) Pick a random number. Clamp, if desired (but not necessary).
    2) Calculate log2(N) + 1. This is the maximum number of guesses needed to find any given N.
    3) Divide #2 by the difficulty level.
    4) Scale #3 by some factor (a bias) to increase the game's overall "level of ease", if desired.

    In code, something along these lines:

    Code:
    size_t maximum_guesses_allowed( size_t value, size_t level, size_t bias = 0 )
    {
    	return std::max
    	( 
    		size_t( ( log( value ) / log( 2 ) + 1 ) / ( level + 1 ) * ( bias + 1 ) ), 
    		size_t( 1 ) 
    	);
    }
    Note that the above assumes a 0-based difficulty rating.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  3. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  4. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM