PDA

View Full Version : [Game] Simple Number Guessing Input Driven Event Loop



Entertainer
05-19-2009, 05:01 AM
Intro

Gauging my wits the other night in trying to find something from my favourite 3D game programming book to share with you students, I spotted just the thing...

A Simple Number Guessing Input Driven Event Loop!

I will be adding a summery at the end of this post, for anyone who is interested in C++, yet not that knowledgeable of what is going on...


// INCLUDES ////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <graph.h>

// MAIN ////////////////////////////////////////////

void main(void)
{
int done = 0, // exit flag
number, // the random number
num_tries = 0, // number of tries
guess; // the players guess

unsigned int far *clock = (unsigned int far*) 0X0000046CL; // pointer to clock

// SECTION (1) /////////////////////////////////////

printf("\n I'm thinking of a number from 1 to 100.");
printf("\n Try and guess it! \n");

srand(*clock); // seed the random number generator within the time
number = 1 + rand() % 100; // choose a random number from 1 to 100


// SECTION (2) /////////////////////////////////////

while(!done)
{

// SECTION (3) /////////////////////////////////////

printf("\n What's your guess?");
scanf("%d", &guess);

// SECTION (4) /////////////////////////////////////

num_tries++;

if (guess > number)
printf("\n Too big! \n");

else

if (guess < number)
printf("\n Too Small! \n");

else
{
printf("\n You guessed the number in %d tries!!! \n", num_tries);
done = 1;

} // end else
} // end while
} // end main

Summery

Now, let's take a look at what each section of the code does.

Section (1) This is the initialization section of the program.
It seeds the random number generator with the current time,
selects a random number, and then prints out a message to the player.

Section (2) This is the entrance to the main event loop.

Section (3) Here's where the input driven event system waits for an event.
In this case, the event is a character string that represents an integer.
Note that the program will not proceed until the integer is entered.

Section (4) Once the input has been entered, this section is executed.
The number that the player enters is tested against the computer's selection,
and messages are displayed telling the player if the guess is too high or too low.

Have fun! :p

If you've any questions or feedback, you are more welcome to respond. ;)

FateFirst
05-19-2009, 09:30 AM
Codes a bit old isn't it? (but decent reference). Some of the headers are meant for 16-bit MS-DOS and thus won't work in your modern 32-bit compilers.



#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std; // not great but just slimming the example down :P

int main()
{
srand(time(0)); // seed random number generator

int theNumber = rand() % 100 + 1; // random number between 1 and 100
int tries = 0, guess;

cout << "\tWelcome to Guess My Number\n\n";

do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;

if (guess > theNumber)
cout << "Too high!\n\n";

if (guess < theNumber)
cout << "Too low!\n\n";

} while (guess != theNumber);

cout << "\nThat's it! You got it in " << tries << " guesses!\n";

return 0;
}

Entertainer
05-19-2009, 09:38 AM
lol yes I was using my favourite book of all time 'The Dark Art of 3D Game Programming' as I will soon be writing up guides on how low level interaction with the on-board gfx cards works.
I have plans...
In a way, I'm going to try and run a parallel open source route up through the ages using a variety of different coding techniques to assit those of us with little to no spending money in this current economic crisis - call it my duty to poor man's society (if you will). In the hope someone will care, that it will make a difference in some way, or that it will help students such as yourself gain a wider perspective of a certain subject matter...or not... Famous last words, I know.

This is what I'm trying to achieve anyway, in the hope maybe Tainer can get his own corner on the board? :p

FateFirst
05-19-2009, 09:43 AM
Its also a good testing base actually...get people to attempt a rewrite of classic code into todays standards.

+1 Vote for "Tainers 'Coding Corner' "