-
help for a newb?
I'm a newbie to C. I need a bit of help developing a C program that will fulfil the following requirements:
- User interface to start a new game and quit the current game;
- User interface to read the questions and select the answers for guessing animal names;
- Algorithms to generate questions, make guesses and decide when the game has ended
- Records and displays the number of questions asked and the time elapsed for each game
The following code is an incremental step towards the final program. The algorithm tries to guess the users chosen integer between -100 and 400 in less than 20 guesses:
------------------------------------------------------------
Code:
/* This tells the compiler that the program will use source code defined in the standard C library. */
#include <stdio.h>
#include <string.h>
/* These are convenient macros.
The compiler replaces all instances of the macro name (all capitals) with the corresponding values */
#define MINIMUM_NUMBER -100
#define MAXIMUM_NUMBER 400
#define CONTINUE_KEY "g"
#define YES_KEY "y"
#define HIGH_KEY "+"
#define LOW_KEY "-"
#define BUFFER_SIZE 32
/* Start of the C program */
int main( void )
{
int guess = 0;
int counter = 0;
int minimum_value = MINIMUM_NUMBER;
int maximum_value = MAXIMUM_NUMBER + 1;
char buffer[ BUFFER_SIZE ];
do
{
printf( "\n" );
printf( "Pick a number between %d and %d. ", MINIMUM_NUMBER, MAXIMUM_NUMBER );
printf( "Press '%s' when you're ready and I will try to guess your number : ", CONTINUE_KEY );
scanf( "%s", buffer );
} while ( strncmp( buffer, CONTINUE_KEY, 1 ) != 0 );
do
{
counter++;
guess = minimum_value + ( ( maximum_value - minimum_value ) / 2 );
printf( "\n" );
printf( "Attempt %d: Is your number %d? ", counter, guess );
printf( "Type '%s' if this guess is correct, '%s' if too high or '%s' if too low : ", YES_KEY, HIGH_KEY, LOW_KEY );
scanf( "%s", buffer );
if ( strncmp( buffer, YES_KEY, 1 ) == 0 )
{
printf( "\n" );
printf( "It took %d attempts to guess your number, %d!", counter, guess );
printf( "\n" );
break;
}
if ( strncmp( buffer, HIGH_KEY, 1 ) == 0 )
maximum_value = guess;
else
minimum_value = guess;
} while ( 1 );
/* Unix convention is to return zero from main for normal program exit */
return 0;
}
------------------------------------------------------------
Firstly, I need to modify the code so that the program will stop guessing and exit after 20 guesses, displaying a message "cannot guess integer < 20 guesses".
Would something
Any help is much appreciated !
-
Already answered on tek-tips.
It's polite to actually wait a while to see if any given forum delivers, rather than trolling the net looking for places to post.
http://www.catb.org/~esr/faqs/smart-...ons.html#forum