Guides:C/C Crash Course/Game Example - Guess the Number

From CoderGuide

Jump to: navigation, search

Back to TOC


Game Example - Guess the Number

So, now we know about loops, if/else statements, and the basics of entering in information. We can now have some fun and make our first game!

To make things interesting, we're going to use a pseudo-random (kinda random, but not truly) number generator. We're also going to need to set the random number seed to something unique so we don't get the same numbers each time our program runs. For the unique seed, we're going to use a unique number.. like... hmm... what changes every time a program runs... hmm.. The time! So we're going to set our random number seed based on the current time.

So, we're going to be using a few new functions we haven't seen before, and that we're not going to discuss much further in this course, but you're welcome to look them up: rand(), srand(). We'll be looking at atoi() and time() later, but they're simple enough. time(), by it's self, isn't too useful, besides returning the number of seconds since the Epoch date for that system, typically (00:00:00 UTC, January 1, 1970). atoi() converts a string of text to a number.

Okay, here is the game code:

  1. /*******
  2.  numguess.c -- A Number Guessing Game
  3.  
  4.  *******/
  5.  
  6. #include <stdio.h>
  7. /*srand() and rand() are defined in stdlib.h */
  8. #include <stdlib.h>
  9. /* time.h is needed by time()*/
  10. #include <time.h>
  11.  
  12. int main(){
  13.  	int number, guess, guesses;
  14.  
  15. 	char s[80];
  16.  
  17. 	/* Get the random number seed ready*/
  18. 	srand( time(NULL) );/*set the seed*/
  19.  
  20.  
  21. 	printf("\n**** Guess My Number Between 0 and 1000! ****\n\n");
  22.  
  23. 	printf("\nEnter negative number for a guess to quit\n");
  24.  
  25.  
  26. 	/*keep looping while the guess is positive.*/
  27. 	do{	
  28. 		/*next, lest get the number to guess*/
  29.  
  30. 		printf("Hmmm.. let me get my number...");
  31.  
  32. 		/*Don't want the number to be over 1000, so let's
  33. 		  get the remainder after dividing it by 1000 by
  34. 		  using modulo operator '%' */
  35. 		number=rand()%1000;
  36.  
  37. 		printf("Got it! Lets go!\n\n\n");
  38.  
  39. 		/*this is a for loop. We're going to have it keep
  40. 		  track of guesses for us. If the guess is equal to
  41. 		  the number we picked, then perfect! Also, if the
  42. 		  number is negative, we what the loop to end too.
  43. 		  (Remember, negative number quits?)
  44. 		 */
  45.  
  46.  
  47. 		/*** Lets start guessing...***/
  48.  
  49. 		for(guesses=0;guess!=number & guess>=0;guesses++){
  50. 			printf("Guess:");
  51. 			gets(s); /*get a number*/
  52.  
  53. 			guess=atoi(s); /*convert string to a number*/
  54.  
  55. 			if(guess>number) printf("**Too High!\n");
  56. 			if(guess<number) printf("**Too Low!\n");
  57. 			if(guess<0) printf("Quitter! It was %d\n\n",number);
  58. 			if(guess==number)
  59. 				printf("Wow, you guessed it!\n");
  60. 		}/*end for(guesses....*/
  61.  		/*** end guessing loop ***/
  62.  
  63. 		/*Ah, looks like they quit or guessed the number.
  64. 		  Let's let them know how they did*/
  65.  
  66. 		printf("You made %d guesses before you ", guesses);
  67.  
  68. 		if(guess==number)printf("won. \n");
  69. 		else printf("gave up and quit... and lost. Haha! I Win!\n");
  70.  
  71. 		/*Say funny things depending on how badly you lost or
  72. 		  by how much you won*/
  73.  
  74. 		if(guesses>=20) {
  75. 			printf("Wow, %d guesses? You soooo loose!\n",
  76. 			   guesses);
  77. 		} else if(guesses>10) {
  78. 			printf("You Lose!\n");
  79. 		} else if(guess>=0){
  80.  
  81. 			printf("\n**** You won! *****\n");
  82. 			if(guesses>=8)printf("Lucky stiff.\n\n");
  83. 			if(guesses<=5)
  84. 			printf("%d guesses? Wow you're good!\n",guesses);
  85.  
  86. 		}/*end if(guess>=...*/
  87.  
  88. 	}while( !(guess<0) ); /*loop while NOT less than zero */
  89.  
  90.  
  91. }/*** end main() ***/
Personal tools