C语言实现数字炸弹小游戏

2022-07-27,,,

本文实例为大家分享了c语言实现数字炸弹小游戏的具体代码,供大家参考,具体内容如下

使用的是c语言

# 内容
#include<stdio.h>
#include<stdlib.h>
int main(){
 //变量
 char c_therequest = 1;//开始界面的字符
 int in_deltar = 1,in_enterednumber,in_sig;//游戏中输入数字及其判定所需要的数字
 int in_givennum = 0,in_an = 0,in_bn = 0;//出的数字,和数字区间
 int in_mid1,in_mid2;//用来交换an、bn的值以满足an<bn的中间量
 int in_distantan,in_distantbn;//an与bn离num的距离
 int in_temporaryansaver,in_temporarybnsaver;//临时用于比较的an、bn储存器
 
 //开始界面
 printf("if you just happen to open the game and do not have the intention to play it.\n");
 printf("you can enter a q to quit or you can enter an s to start the game now!\n");
 scanf("%c",&c_therequest);
 getchar();
 while (c_therequest != 's' && c_therequest != 'q'){
 printf("please do not enter a irrelevant letter.\n");
 scanf("%c",&c_therequest);
 getchar();
 }
 if(c_therequest == 'q'){
 printf("so sad then.hope you can play with me next time.\n");
 getchar();
 }

 //游戏
 if(c_therequest == 's'){
 
 //输入“数字炸弹”
 system("cls");
 printf("(^ o ^) # *** now let\'s play! *** # (^ o ^)\nattention!you can only enter numbers in the following steps.\nenter to start.\n");
 getchar();
 system("cls");
 printf("game : the number boom!(another life.)\n");
 printf("rule:\n");
 printf("the first player should enter a number.then he should give to numbers and guarantee the number above is between them.\n");
 printf("the rest should guess and enter the numbers.and the one who enter the exact number the first player entered\n");
 printf("is supported to be the winner!\n");
 printf("please enter a number to start the game.(you is ought not to expose it to other players.the number should bigger than 0.)\n");
 printf("__________\b\b\b\b\b\b\b\b\b\b\b");
 while(scanf("%d",&in_givennum) != 1 || in_givennum < 0){
  system("cls");
  printf("please enter a number which is bigger than 0.\n");
  printf("num:___________\b\b\b\b\b\b\b\b\b\b\b");
  getchar();
 }
 system("cls");
 //判定是否为数字

 //输入“数字炸弹”所在的区间
 printf("and where is the number?please enter two numbers,and ensure that the number above is between them.\n");
 printf("additionally,there should be at least 100 numbers between the two numbers you will enter.\n");
 do{
  printf("num = %d\n",in_givennum);
  printf("a:__________\b\b\b\b\b\b\b\b\b\b");
  while(scanf("%d",&in_an)!=1){
  printf("please enter a number which is bigger or smaller than the \"num\"!!!!\n");
  printf("a:__________\b\b\b\b\b\b\b\b\b\b");
  scanf("%d",&in_an);
  getchar();
  }
  printf("b:__________\b\b\b\b\b\b\b\b\b\b");
  while(scanf("%d",&in_bn)!=1){
  printf("please enter a number which is bigger or smaller than the \"num\"!!!!\n");
  printf("b:__________\b\b\b\b\b\b\b\b\b\b");
  scanf("%d",&in_bn);
  getchar();
  }//记录an和bn
  if(!(( in_an < in_givennum && in_bn > in_givennum ) || ( in_an > in_givennum && in_bn < in_givennum ))){
  system("cls");
  printf("your math is so poor.enter a again!\nthe \"num\" must be between the two numbers.\n");
  printf("enter to restart.\n");
  getchar();
  getchar();
  system("cls");
  }//区间如果错误就会报错
 }while(!(( in_an < in_givennum && in_bn > in_givennum ) || ( in_an > in_givennum && in_bn < in_givennum )));//判定区间是不是对的

 //排序
 in_mid1 = (in_an > in_bn)?in_bn : in_an;
 in_mid2 = (in_bn > in_an)?in_bn : in_an;
 in_an = in_mid1;
 in_bn = in_mid2;

 //开始猜数字
 system("cls");
 printf("now the game starts!\n");
 printf("the number is somewhere between %d and %d\n",in_an,in_bn);
 printf("please enter the number.\n");
 printf("__________\b\b\b\b\b\b\b\b\b\b");
 while(in_deltar != 0){
  while(scanf("%d",&in_enterednumber)!=1){
  printf("please enter a number!!!!\n");
  scanf("%d",&in_enterednumber);
  getchar();
  }//判断是否为数字

  //差的运算
  in_deltar = ((in_givennum - in_enterednumber)<0)?in_enterednumber - in_givennum:in_givennum - in_enterednumber;
  in_sig = in_givennum - in_enterednumber;
  in_distantan = in_givennum - in_an;
  in_distantbn = in_bn - in_givennum;

  //判定差的大小
  if(in_deltar >= 1000){
  if(in_sig > 0)
  printf("too small!next!\n");
  else
  printf("too big!next!\n");
  }
  else if(in_deltar >= 100){
  if(in_sig > 0)
  printf("small.next!\n");
  else
  printf("big.next!\n");
  }
  else if(in_deltar >= 10){
  if(in_sig > 0)
  printf("a little small.next!\n");
  else
  printf("a little big.next!\n");
  }
  else if(in_deltar > 0){
  printf("almost there!next!!!\n");
  }

  //临时存储,以便后面判断所给数字是否满足条件
  in_temporaryansaver = in_an;
  in_temporarybnsaver = in_bn;

  if(( in_deltar < in_distantan && in_sig > 0 ) || ( in_deltar < in_distantbn && in_sig < 0 )){
  if(in_sig > 0)
  in_an = in_enterednumber;
  else
  in_bn = in_enterednumber;
  }//这是修改上下限
  if((in_temporaryansaver == in_an && in_temporarybnsaver == in_bn) && in_deltar){
  system("cls");
  printf("do not cheat!\nyou should play it again.\n");
  }//判定所猜的数字是否在区间内

  if(in_deltar == 0)
  break;//猜中
  printf("enter to continue.\n");
  getchar();
  getchar();
  system ("cls");
  printf("between %d and %d\n__________\b\b\b\b\b\b\b\b\b\b",in_an,in_bn);//区间修正
 }
 printf("you are the one !!!");
 getchar();
 getchar();
 }
}

总结

学习c的时候为了巩固所学知识而编得一个小游戏,内容全英文。

小编再为大家分享一段代码:

#define _crt_secure_no_warnings 1
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
void menu()
{
 printf("###########################\n");
 printf("### 1. play 0. exit ###\n");
 printf("###########################\n");
}
void game()
{
 //1.生成一个随机数
 int ret = 0;
 int guess = 0;
 //拿时间戳来设置随机数的生成起点  //时间戳——(当前计算机的时间-计算机的起始时间(1970.1.1.0时0分0秒))=(xxxx)秒
 //time_t
 //srand((unsigned int)time(null));
 ret=rand()%100+1; //生成随机数 0---0x7fff(32767)
 //printf("%d\n",ret);
 //2.猜数字
 while (1)
 {
 printf("请猜数字: ");
 scanf("%d", &guess);
 if (guess > ret)
 {
 printf("big\n");
 }
 else if (guess < ret)
 {
 printf("small\n");
 }
 else
 {
 printf("you are die\n");
 break;
 }
 }
 
}

int main()
{
 int input = 0;
 srand((unsigned int)time(null));
 do
 { 
 menu();
 printf("请输入>:");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();
 break;
 case 0:
 printf("退出游戏\n");
 break;
 default:
 printf("输入错误\n");
 break;
 }
 } while (input);
 return 0;
}

更多有趣的经典小游戏实现专题,分享给大家:

c++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《C语言实现数字炸弹小游戏.doc》

下载本文的Word格式文档,以方便收藏与打印。