• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 1474次浏览

C语言设计的简单的随机数字游戏,猜数字的游戏,只能在windows下运行

/********************************************************************************************
|                                 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;
}
/** end of http://code.activestate.com/recipes/578088/ }}} */


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C语言设计的简单的随机数字游戏
喜欢 (0)
加载中……