C语言设计的简单的随机数字游戏的代码

下面代码是关于C语言设计的简单的随机数字游戏的代码。

|                                 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;
}                         复制代码
                                                                                                                    


转载于:https://juejin.im/post/5c4fc82c6fb9a049a97a1a5e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值