| 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>
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 score_index;
printf("nn");
printf("t#####################n");
printf("t# THE GUESSING GAME #n");
printf("t#####################n");
printf("nWelcome to the Random Number Guessing Game!n");
do {
if (iTOTAL)
temp_storage = scores;
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);
}
printf("Please start by picking a range of values: nn");
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);
srand( time( NULL ) );
iCOUNTER = 0;
int iR_NUMBER = ( iMIN + ( rand() % ( iRANGE + 1 ) ) );
printf("nnCan you guess the number I am thinking of?");
do {
iCOUNTER = iCOUNTER + 1;
printf("nYour Guess: ");
scanf( " %d", &iCHOICE );
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
} while( iCHOICE != iR_NUMBER );
iSCORES = iSCORES + iSCORE;
printf( "The answer was %d.", iR_NUMBER );
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." );
printf("nWould you like to play again? Y/N? n > ");
scanf(" %c", &NEWGAME);
scores[iTOTAL - 1] = iSCORE;
} while (NEWGAME == 'y' || NEWGAME == 'Y');
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;
} 复制代码
下面代码是关于C语言设计的简单的随机数字游戏的代码。
转载于:https://juejin.im/post/5c4fc82c6fb9a049a97a1a5e