+ Reply to Thread
Results 1 to 5 of 5

Thread: Guess The Number VS. Computer Opponent.

  1. #1
    Join Date
    Nov 2009
    Location
    I seem to be lost...
    Posts
    1,566
    Blog Entries
    1
    Rep Power
    22

    Guess The Number VS. Computer Opponent.

    OK I’m going to show you how to make a little game where you and the computer take turns guessing at a random number between 1-100. I’ll do my best to explain everything. :)

    OK the first thing we do is add this.
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    OK #include<iostream> includes the file to take user input and output it on the console window.

    #include<cstdlib> this includes file that deal with generating random numbers.

    #include<ctime> allows us to seed our random number or in other words allows us to make the number random.

    using namespace std; this lets us call any function in the std namespace without having to prefix it so std::cout can now be writhen cout which will save you lots of time.

    Next we add.
    Code:
    int main()
    {
    	srand(time(NULL));
    
    	char playagain = 'y';
    	while (playagain == 'y')
    	{
    }
    }
    In side Int main()s braces is where all of our code is executed by the complier.(for this tutorial.)

    srand(time(NULL)); This seeds our random number

    char playagain = 'y'; This sets playagain to a char value which is a character value then set it == or equal to ‘y’

    while (playagain == 'y')This is or main loop where the program is ran so while playagain is equal to ‘y’ execute whatever’s in these braces( this make sure the user selected y to play the game again.)
    {
    }

    Ok now we define our main variables in our while loop.
    Code:
    int comguessHigh  =  100;
    		int comguessLow  =  1;
    		int thenumber = rand() % 100 + 1;
    		int tries = 0, guess;
    int comguessHigh = 100; and int comguessLow = 1; Are the variables used to tell the computer not to guess higher or lower than the high or low guess.

    int thenumber = rand() % 100 + 1; this sets the variable thenumber to a random number between 1-100.

    Int tries = 0,guess; This sets the variable tries and guess equal to 0.

    Now comes the guessing the number part.
    Code:
    for(;;)
    		{
    			cout << "Enter guess: ";
    		    cin >> guess;
    			tries += 1;
    
    			if (guess < thenumber)
    			{
    				cout << "\nThat's to low.\n" << endl;
    			}
    			else if (guess > thenumber)
    			{
    				cout << "\nThat's to high.\n" << endl;
    			}
    			else
    			{
    				cout << "That's it! You guessed it in " << tries << " tries." << endl;
    				break;
    			}
    cout << "Enter guess: This asks the user to enter a number

    cin >> guess; This takes the user input and sets guesses value equal to the number entered.

    tries += 1; This sets tries plus 1 which will later be used to tell the user how many tries it took them to guess the number.

    if (guess < thenumber) if guess is smaller than thenumber tell the user it to low then move on to the next operation.(which will do in a minute)
    {
    cout << "\nThat's to low.\n" << endl;
    }
    else if (guess > thenumber) if guess is larger than thenumber tell the user it to high then move on to the next operation.(which will do in a minute)
    {
    cout << "\nThat's to high.\n" << endl;
    }
    else [If the users guess is not to high or low tell the user how many tries it took to guess it in then exit the for(;;) loop with the break; statement.]
    {
    cout << "That's it! You guessed it in " << tries << " tries." << endl;
    break;
    }

    OK so now the player can guess at the number but now we need to make the computer take a guess.
    Code:
    int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow;
    			cout << "Computer guess's: " << comguess << endl;
    
    			if (comguess < thenumber)
    			{
    				cout << "\nThe computers guess was to low.\n" << endl;
    				comguessLow = comguess;
    			}
    			else if (comguess > thenumber)
    			{
    				cout << "\nThe computers guess was to high.\n" << endl;
    				comguessHigh = comguess;
    			}
    			else
    			{
    				cout << "\nSorry the computer guessed it." << endl;
    				break;
    			}
    int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow; This changes comguess which is the computers guess so it can only be lower than its highest guess and higher than its lowest guess.

    The rest of that code checks to see if the computers guess to high or low just like we did a minute ago when the player guessed.

    Now the last thing we have to do is when the player or computer guesses the number we need to ask if we want to play again.
    Code:
    }
    			
    		       cout << "\nDo you want to play again? (y/n)" << endl;
    		       cin >> playagain;
    	}
    	
    	           cout << "\nOk mabye next time." << endl;
    
    	cin.get();
    	return 0;
    }
    }
    cout << "\nDo you want to play again? (y/n)" << endl;
    cin >> playagain;
    }
    Asks the user if they want to play again if a letter is entered that is not y the while loop is not executed because again is not equal to y.

    cin.get(); This keeps the console window open intel the program is done running.

    return 0;This exits the program.

    I know that that migh be a little hard to tell where every thing go's so heres the whole code.
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    
    
    int main()
    {
    	srand(time(NULL));
    
    	char playagain = 'y';
    	while (playagain == 'y')
    	{
    
    		int comguessHigh = 100;
    		int comguessLow = 1;
    		int thenumber = rand() % 100 + 1;
    		int tries = 0, guess;
    
    		for(;;)
    		{
    			cout << "Enter guess: ";
    		    cin >> guess;
    			tries += 1;
    
    			if (guess < thenumber)
    			{
    				cout << "\nThat's to low.\n" << endl;
    			}
    			else if (guess > thenumber)
    			{
    				cout << "\nThat's to high.\n" << endl;
    			}
    			else
    			{
    				cout << "That's it! You guessed it in " << tries << " tries." << endl;
    				break;
    			}
    
    			int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow;
    			cout << "Computer guess's: " << comguess << endl;
    
    			if (comguess < thenumber)
    			{
    				cout << "\nThe computers guess was to low.\n" << endl;
    				comguessLow = comguess;
    			}
    			else if (comguess > thenumber)
    			{
    				cout << "\nThe computers guess was to high.\n" << endl;
    				comguessHigh = comguess;
    			}
    			else
    			{
    				cout << "\nSorry the computer guessed it." << endl;
    				break;
    			}
    	    
    		}
    			
    		       cout << "\nDo you want to play again? (y/n)" << endl;
    		       cin >> playagain;
    	}
    	
    	           cout << "\nOk mabye next time." << endl;
    
    	cin.get();
    	return 0;
    }
    Well there you have it hope you learned somthing if you have any questions or problems feel free to ask.(leave comments on how to make my next tut better :)
    Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
    Science is only an educated theory, which we cannot disprove.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Guess The Number VS. Computer Opponent.

    Looks good. If you want to make the computer a little better, you could have it pay attention to your guesses, and react to them as well, and post it's guesses to help the player. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Nov 2009
    Location
    I seem to be lost...
    Posts
    1,566
    Blog Entries
    1
    Rep Power
    22

    Re: Guess The Number VS. Computer Opponent.

    Thanks,Ya good Idea.
    Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
    Science is only an educated theory, which we cannot disprove.

  5. #4
    wiwbiz's Avatar
    wiwbiz is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    66
    Rep Power
    0

    Re: Guess The Number VS. Computer Opponent.

    I've made such a game, didn't interest me.

  6. #5
    Join Date
    Nov 2009
    Location
    I seem to be lost...
    Posts
    1,566
    Blog Entries
    1
    Rep Power
    22

    Re: Guess The Number VS. Computer Opponent.

    Why not?
    Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
    Science is only an educated theory, which we cannot disprove.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with Guess the number in C++
    By Azrian in forum Classes and Code Snippets
    Replies: 3
    Last Post: 04-05-2010, 11:47 PM
  2. Guess the number! - Python
    By Azrian in forum Classes and Code Snippets
    Replies: 6
    Last Post: 02-17-2010, 04:13 AM
  3. guess the number problem
    By CommittedC0der in forum C and C++
    Replies: 2
    Last Post: 11-14-2009, 08:28 AM
  4. Guess the number game
    By drdebcol in forum Classes and Code Snippets
    Replies: 1
    Last Post: 06-13-2009, 08:09 AM
  5. Guess a Number
    By 3n! in forum C and C++
    Replies: 2
    Last Post: 12-02-2007, 01:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts