Sign in to follow this  
DaemonMagus

Guess the Number Game

Recommended Posts

I made a guess the number game and I'd like you guys to see if there is anything you would change. As far as I know it works properly. If you know how to put the code into that little box with the highlightng and everything please tell me. Edit: Thanks for the responses, I put it into the code box.
// Guess the Number - Program generates random number
// and user tries to guess what it is

#include <iostream>
#include <stdlib.h>
#include <time.h>

bool gameloop();  // main game loop
void debug();  // prints values of variables for debugging

int guess, lives, num; // guess for what rand num is, lives left, random number
char input; // input for if user wants to play again
bool win;  // checks to see if user won

int main()
{
    srand( (unsigned)time( NULL ) );
    
    while (gameloop() == true);               
    
    return 0;
}    

bool gameloop()
{    
    num = int((rand()/(double)(RAND_MAX+1)) * 6) + 1;
    win = 0;
    lives = 3;
    bool playagain;
    
    while (lives != 0)
    {
        debug();
        std::cout << "Guess number: ";
        std::cin >> guess;
        std::cout << "\n";
        
        // checks if the users guess equals the random number
        // if not tells if its too high or too low and decrements lives
        if (guess == num)
        {
            win = 1;
            break;
        }
        else
        {
            std::cout << "Wrong number\n";
            lives -= 1;
            
            if (guess < num)
                std::cout << "Too low.\n";
            else
                std::cout << "Too high.\n";
        }
        
    }
    if (win == 1)
        std::cout << "You win!\n";
    else
        std::cout << "You lose.\n";
    
    std::cout << "Would you like to play again? (y/n)\n";
    std::cin >> input;
    if (input == 'y')
        playagain = true;
    else
        playagain = false;
    return playagain;
}  

void debug()
{
    std::cout << num << "\n" << win << "\n" << lives << "\n";
}


Share this post


Link to post
Share on other sites
Looks neat; as far as a I can tell [grin]
To put it in a source box, do this:
[ source lange="cpp" ]
***source in here***
[ /source ]

Without the spaces around after and before the [ ]

EDIT: Well; if you wrote a funky one there, how about one I wrote specially for those who don't want to compile the source? Guess the number game in a link!


If you want the source;

javascript:var r=Math.floor(Math.random()*10)+1;var g='Enter your guess here.';window.alert('Welcome to Ben\'s awesome guess the number game!');while(g!=r){g=prompt('Guess the number!\nIt\'s between 1 and 10',g);if(g==r){window.alert('Well done! You win a cake!');window.location='http://www.gerryscakes.com/cafe-mocha-cake.html'}else if(g<r){window.alert('Too low!');}else if(g>r){window.alert('Too high!');}}


Share this post


Link to post
Share on other sites
Its just a stylistic thing, but I would take the break out of the if statement and change the while to a do while, with conditions while (lives > 0 && win == 0). This makes the exit conditions clearer for anyone who is reading your code; they know the loop will be executed at least once, because its a do while, and they can see all the exit conditions in one place.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this