Welcome, guest | Sign In | My Account | Store | Cart

An old game I created freshman year of college. For use on Windows OS only. (The code contains a few Windows system calls.)

C, 171 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/********************************************************************************************
|                                 C O M M E N T S                                           |   
+-------------------------------------------------------------------------------------------+
| T I T L E:    random_number_gamev3.c							                            |
| A U T H O R:  Jacob A. Bridges                                                            |
| D A T E:      3.1.2011                                                                    |
|											                                                |
| This program will take a range of numbers from the user and randomly select one number.   |
|     Then it asks the user to guess the random number. Keeps track of times the user       |
|     tries to guess the number.							                                |
|   											                                            |
| What new in Version 3? The game keeps track of your average scores for every game then    | 				
|     displays them when the player stops playing the game. It also displays the average    |
|     of all your scores.								                                    |
|											                                                |
+-------------------------------------------------------------------------------------------+
********************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/////////////////////////////////////////////////////////////////////////////////////////////
   // Defining some Gloabl Variables
/////////////////////////////////////////////////////////////////////////////////////////////

int iR_NUMBER = 0;
int iCHOICE   = 0;
int iCOUNTER  = 0;
int iMIN      = 0;
int iMAX      = 0;
int iRANGE    = 0;
int iSCORE    = 0;
int iSCORES   = 0;
int iTOTAL    = 0;
char NEWGAME  = 0;


int main ()
{

	int *scores;
	int *temp_storage;
	int score_index;
/////////////////////////////////////////////////////////////////////////////////////////////
   // Header
/////////////////////////////////////////////////////////////////////////////////////////////

   printf("\n\n");
   printf("\t#####################\n");
   printf("\t# THE GUESSING GAME #\n");
   printf("\t#####################\n");
   printf("\nWelcome to the Random Number Guessing Game!\n");

/////////////////////////////////////////////////////////////////////////////////////////////
   // Beginning of the "PLAY AGAIN?" loop
/////////////////////////////////////////////////////////////////////////////////////////////

do {
	 if (iTOTAL)
		temp_storage = scores;
     // iTOTAL used to keep track of games played
     iTOTAL = iTOTAL + 1;
	 scores = calloc(iTOTAL, sizeof(int));
	 if (iTOTAL != 1)
	 {
		for (score_index = 0; score_index < iTOTAL - 1; score_index++)
			scores[score_index] = temp_storage[score_index];
		free(temp_storage);
	 }
/////////////////////////////////////////////////////////////////////////////////////////////
   // Setting a range of values
/////////////////////////////////////////////////////////////////////////////////////////////

     printf("Please start by picking a range of values: \n\n");
     printf("Highest value in range: ");
     scanf(" %d", &iMAX);
     printf("Lowest value in range: ");
     scanf(" %d", &iMIN);
     printf("\n > You chose the range to be from %d  to %d. < ", iMIN, iMAX);
     iRANGE = (iMAX - iMIN);

     // Seeding the random function
     srand( time( NULL ) );

     // Setting counter to ZERO
     iCOUNTER = 0;

/////////////////////////////////////////////////////////////////////////////////////////////
   // Selecting a random value from our range
/////////////////////////////////////////////////////////////////////////////////////////////

     int iR_NUMBER = ( iMIN + ( rand() % ( iRANGE + 1 ) ) );

/////////////////////////////////////////////////////////////////////////////////////////////
   // The WHILE loop
/////////////////////////////////////////////////////////////////////////////////////////////

     printf("\n\nCan you guess the number I am thinking of?");
     do {
  	  // Starting the counter
          iCOUNTER = iCOUNTER + 1;

	  // Asks the user for a number
	  printf("\nYour Guess: ");
  	  scanf( " %d", &iCHOICE );

	  // Compares the user's input to the random number
 	  if( iCHOICE < iR_NUMBER )
		  printf( "Your guess is too low, try again!" );
	  else if( iCHOICE > iR_NUMBER )
		  printf( "Your guess is too high, try again!" );
	  else
		  printf( "\n\n\t*YOU GUESSED IT!*\n\n" );
        } while( iCHOICE != iR_NUMBER );

/////////////////////////////////////////////////////////////////////////////////////////////
   // End of game sequence
/////////////////////////////////////////////////////////////////////////////////////////////

     // Calculates player's "GRADE"
     iSCORE = (int)(100 * ((float)(iRANGE - iCOUNTER) / iRANGE));
     //printf(" %d%%\n", iSCORE);

     // Logs your score
     iSCORES = iSCORES + iSCORE;
     
     // Tells the answer
     printf( "The answer was %d.", iR_NUMBER );

     // Gives the player's "score"
     printf( "\nIt took you %d guesses.", iCOUNTER );
     if( iSCORE == 100 )
  	  printf( " AMAZING GUESS!" );
     else if( iSCORE >= 90 )
	  printf( " Great job!" );
     else if( iSCORE >= 80 )
	  printf( " Nice Work." );
     else if( iSCORE >= 70 )
	  printf( " Try harder next time." );
     else if( iSCORE >= 60 )
	  printf( " You are bad at this game." );
     else 
	  printf( " EPIC FAIL." );

/////////////////////////////////////////////////////////////////////////////////////////////
   // Option to play again
/////////////////////////////////////////////////////////////////////////////////////////////

     printf("\nWould you like to play again?    Y/N? \n >  ");
     scanf(" %c", &NEWGAME);
	 
	 scores[iTOTAL - 1] = iSCORE;

     } while (NEWGAME == 'y' || NEWGAME == 'Y');
   
   // If player chooses to end game
   printf("\n+-----------------------------------------------------+");
   printf("\n|  You played %d time(s).", iTOTAL);
   printf("\n|  Your average score was %d%%\n", (iSCORES/iTOTAL));
   
   for (score_index = 0; score_index < iTOTAL; score_index++)
		printf("|  Score for game %d: %d%%\n", score_index + 1, scores[score_index]);
   free(scores);
   printf("+-----------------------------------------------------+");
   printf("\n");
   system("pause");

   
   return 0;
}

1 comment

Stephen Chappell 9 years, 7 months ago  # | flag

Amazing! So ActiveState has a cookbook for C? That is a really neat discovery.

Created by Jacob A. Bridges on Thu, 29 Mar 2012 (MIT)
C recipes (32)
Jacob A. Bridges's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks