Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

number guessing game reverse!

Ask ALL your questions here

number guessing game reverse!

Postby Kopilatis on Wed Oct 28, 2009 5:01 pm

so i have this project that i have to make a game like the number guessing one with the difference that i put the number and the computer tries to guess. I have wrote most of it but my only problem is that i cant put limits into the computers guesses...take a look and any suggestion is usefull



Code: Select all
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int theNumber;
    int cmpGuess;
    char highLow;
   
   
    cout << "Enter your number from 1-100";
    cin >>theNumber;
   
    srand(time(0));
    cmpGuess = rand()%100+1;
   
    do
    {
      cout << "i guess its "<<cmpGuess<<"...is it higher or lower than that?(h/l)";
      cin >> highLow;
             if (highLow == 'l')
             {
                                    cmpGuess=rand()%cmpGuess+1;
                           
             }
          cout << endl;
             if (highLow == 'h')
             {
                                   cmpGuess=cmpGuess+rand()%(100-cmpGuess) +1;               
             }
          cout << endl;
    }
    while (cmpGuess!=theNumber);
   
   
    system ("PAUSE>>NULL");
   
    return 0;
}         
Kopilatis
 
Posts: 13
Joined: Fri May 29, 2009 7:47 pm

Re: number guessing game reverse!

Postby vEjEsE on Wed Oct 28, 2009 5:21 pm

Hah, never thought someone will put the computer to guess xD

And I don't think that computers are made for that, but it looks like one of those questions that need several head smashing in the wall to get over it.

Just so to make the game look more ... realistic. The computer knows what the number typed in is, so you can do the "higher" and "lower" with if statements, and adjust it every time the computer picks a random number. Hmm, I somehow forgot what I was talking about :))

So, yes, that's a good idea, it just seems so ... FATAL CRASH.

Later Edit:
Uh, what's that "PAUSE<<NULL" in system() function? Never saw that before. It works fine with just "PAUSE" ... no?

And another edit:
Sorry, wasan't paying attention to the main question, that 'computer guessing' kind of bluescreened me.
You could make an integer (int nCount = 0) and at the end of the while loop increse it, then put inside
while ( (cmpGuess!=theNumber) && (nCount < 5));

Hope this helps somewhat.
Does a universe exist if there is no life to acknowledge it's existence?
User avatar
vEjEsE
 
Posts: 48
Joined: Mon Aug 03, 2009 12:48 am

Re: number guessing game reverse!

Postby Kopilatis on Wed Oct 28, 2009 5:37 pm

thanks it helps alot :)
Kopilatis
 
Posts: 13
Joined: Fri May 29, 2009 7:47 pm

Re: number guessing game reverse!

Postby noobgrammer on Wed Oct 28, 2009 5:59 pm

I wrote one of those before but mine was done in a split second
I just had my rand range get reassigned to what number was guessed if it wasn't right
all I did is type in a number and let the computer do the rest
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: number guessing game reverse!

Postby Kopilatis on Wed Oct 28, 2009 6:07 pm

how do u set a rand range ? thats what i m looking for :P
Kopilatis
 
Posts: 13
Joined: Fri May 29, 2009 7:47 pm

Re: number guessing game reverse!

Postby vEjEsE on Wed Oct 28, 2009 6:23 pm

You could make a funtion that return a random number between two numbers you want.

Wait, know that I think of it, it's hard to do. Bluescreend again.

Here's a link about the random numbers, you could implement it random-number-generator-t18.html
Does a universe exist if there is no life to acknowledge it's existence?
User avatar
vEjEsE
 
Posts: 48
Joined: Mon Aug 03, 2009 12:48 am

Re: number guessing game reverse!

Postby noobgrammer on Wed Oct 28, 2009 6:29 pm

Code: Select all
number = rand() % 100 + 1;


the first number 100 is how many random numbers you have the 1 makes it so you have a range from 1 - 100 and not 0 - 99
so the 1 would be your low range

here was the code I did for mine you can use it to help understand what I'm saying

Code: Select all
#include <iostream>
#include <limits>
#include <ctime>

using std::cout;
using std::cin;

int main()
{   
   srand((unsigned)time(NULL));
   
   int number;
   int guess;
   int counter = 1;
   int newmod;
   int newlow = 0;
   int newhigh = 100;

   while ((cout<< "Enter a number between 1 and 100\n\n") &&!(cin>> number) || (number > 100 || number <= 0))
   {
      cout<< "Please enter a number between 1 & 100 nothing else\n\n";
      cin.clear();
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   }
   
   guess = rand() % newhigh + newlow;
do
{
   if (guess > number)
   {
      cout<< guess << "\t<<<< To high\n";
      ++counter;
      newhigh = guess;
      newmod = newhigh - newlow;
      ++newmod;
      guess = rand() % newmod + newlow;
   }
      else if (guess < number)
      {
         cout<< guess <<"\t>>>> Too low\n";
         
         ++counter;
         newlow = guess;
         newmod = newhigh - newlow;
         ++newmod;
         guess = rand() % newmod + newlow;
      }
}while (guess != number);

      cout<< "You win with " << counter << " guess the number you guessed was " << number <<"\n\n";
}
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: number guessing game reverse!

Postby Vevix on Wed Oct 28, 2009 6:45 pm

vEjEsE wrote:You could make a funtion that return a random number between two numbers you want.

Wait, know that I think of it, it's hard to do. Bluescreend again.

Here's a link about the random numbers, you could implement it random-number-generator-t18.html


Something seriously wrong with your OS if you're blue-screening from a simple usermode application like this.
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. -- Bjarne Stroustrup
User avatar
Vevix
 
Posts: 142
Joined: Sat Oct 10, 2009 9:24 pm
Location: kernel32.dll

Re: number guessing game reverse!

Postby Kopilatis on Thu Oct 29, 2009 2:13 am

tnaks guys .got it :D
Kopilatis
 
Posts: 13
Joined: Fri May 29, 2009 7:47 pm


Return to QA

Who is online

Users browsing this forum: No registered users and 0 guests