C语言编程轰炸,C语言实现数字炸弹小游戏
本文實例為大家分享了c語言實現數字炸彈小游戲的具體代碼,供大家參考,具體內容如下
使用的是c語言
# 內容
#include
#include
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
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
#include
#include
#include
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语言编程轰炸,C语言实现数字炸弹小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 类的特殊成员反射异常处理
- 下一篇: SQL创建表格——手写代码