C语言随机数猜数字游戏源码
C高级语言程序设计猜数字游戏
![C高级语言程序设计猜数字游戏](https://img.taocdn.com/s3/m/f8be3f20b4daa58da0114aa9.png)
《高级语言程序设计》课程设计说明书设计题目:猜数字游戏目录1.游戏说明、、、、、、、、、、、、、、、、、、、、、、、、、、12.程序的模块组成、、、、、、、、、、、、、、、、、、、、23.程序流程、、、、、、、、、、、、、、、、、、、、、、、、、、24.程序源清单、、、、、、、、、、、、、、、、、、、、、、、、25.课程设计总结、、、、、、、、、、、、、、、、、、、、、、、19课题:猜数字游戏说明:每次随机产生5个数字,提供10次机会,若猜中进入下一次猜数,每猜中一次奖励100分,可中途退出猜数游戏,可以累计分数,和产生积分榜基本功能:1.用户注册2.用户积分查看游戏流程设计思路:1设计思路:1.程序的模块组成:主函数、、、、、、、、、、、、int mian创立结构体、、、、、、、、struct record定义数组、、、、、、、、、、char name[ ]清除文本模式窗口、、system("cls")2.程序流程:分别用void定义开始界面display,操作函数sys(int a),记录文件recwrite(),浏览界面vdisplay(),读取文件recread() ,开始游戏newgame(),判断并增加记录addrec()。
在用int mian主函数调用。
srand( (unsigned)time( NULL ),以时间为种子,产生随机数,更具有代表性,这样避免了随机出显的数字服从正太分布。
3程序源清单:#include <stdlib.h>#include <stdio.h>#include <time.h>#include<windows.h>#include<string.h> 2struct record{char name[10];int sorce;}rec;int num; //输入的数字void display(); //开始界面void xuanze(); //选择函数void sys(int a); //操作函数void zhuce(); //用户注册void newgame(); //新游戏void vdisplay(); //浏览记录界面int qushu(); //取数函数int tishi(int aa); //提示函数void addrec(); //判断并且增加记录void recread(); //读取记录文件void recwrite(); //写入记录文件void main() 3 {display();}void display() //开始界面{system("cls"); //清屏printf("============================================\n"); printf(" ================数字竞猜游戏================\n");printf("============================================\n"); printf("\n");printf(" 1: 新游戏\n");printf("\n");printf(" 2: 注册用户\n"); printf("\n");printf(" 3: 用户积分查看\n");printf("\n"); 4 printf(" 4: 退出\n");printf("\n");printf("============================================\n"); xuanze();}void xuanze() //选择函数{int a;printf("请输入你所选择的操作数:\n");scanf("%d",&a);if(a==1||a==2||a==3||a==4){sys(a);}//选择相应的操作数执行函数else{printf("错误的操作数!\n");;printf("\n");printf("请按任意键继续...\n");getchar(); 5 getchar();display();}}void sys(int a) //操作函数{switch(a){case 1:newgame();break;case 2:zhuce();break;case 3:vdisplay();break;case 4:exit(0);break; //退出}}void zhuce(){system("cls");printf("请输入你的名字\n");scanf("%s",&); 6 recwrite(); //写入printf("恭喜!注册成功!\n");printf("请按任意键继续...\n");getchar();getchar();display();}void recwrite() //写入记录文件{FILE *fp;fp=fopen("E:\\rec.rec","wb");if(fp==NULL)printf("写入失败!\n");fwrite(&rec,sizeof(struct record),1,fp);fclose(fp);}void vdisplay() //浏览记录界面{ 7 system("cls"); //清屏printf("============================================\n"); printf(" ================数字竞猜游戏================\n");printf(" ====================积分====================\n");recread();printf("\n");printf(" 姓名:%s : 分数%d\n",,rec.sorce); printf("\n");printf("请按任意键继续...\n");getchar();getchar();display();}void recread() //读取记录文件{FILE *fp; 8 L: fp=fopen("E:\\rec.rec","rb");if(fp==NULL){recwrite();goto L; //初始化}if(fread(&rec,sizeof(record),1,fp)==1){}else{printf("文件读取失败!");}fclose(fp);}void newgame() //新游戏{recread();system("cls"); //清屏9 printf("\n");if (==0){ printf(" 请先注册用户\n");printf("请按任意键继续...\n");getchar();getchar();vdisplay();}/////////////////////////////////////////////////////////////////////////////int snum; //输入的数字int sn=10; //机会次数num=qushu(); //取数L: system("cls"); //清屏printf("============================================\n");printf("============================================\n"); printf(" ================数字竞猜游戏================\n");printf("============================================\n"); printf("\n");printf("===========================================\n"); printf(" 姓名:%s : 分数%d\n",,rec.sorce); printf("\n");printf(" 按1: 终止游戏,显示答案\n");printf("============================================\n"); printf("\n");printf("请输入你猜想的五个数字(格式为99999样式)你还有%d 次机会:\n",sn);scanf("%d",&snum);if(snum<100000 && snum>10000 || snum==1){if(snum==1){printf("你放弃了本局游戏!该五个数字是: %d\n",num); 11 printf("\n");printf("请按任意键继续...\n");getchar();getchar();display() ;}elseint flag=tishi(snum);if(flag!=0 && sn!=0){sn--;if(sn==0){printf("真是遗憾!十次机会没有了!没有关系!下次继续!\n"); printf("\n");printf("请按任意键继续...\n");getchar();getchar();newgame() ; 12 }printf("请按任意键继续猜...\n");getchar();getchar();goto L;}elseif(sn!=0)addrec();else{printf("请按任意键继续...\n");getchar();getchar();newgame();}}}}else{ 13 printf("错误的操作数!\n");;printf("\n");printf("请按任意键继续...\n");getchar();getchar();goto L;}int tishi(int aa) //提示函数{ int a,b,c,d,e;int af,bf,cf,df,ef;if(aa==num){printf("恭喜!你猜对了!五个数就是:%d\n",num);return(0);14 }else{ a=num/10000;b=num/1000%10;c=num/100%100%10; //获取输入的四个数字d=num/10%1000%100%10;e=num%10000%1000%100%10;af=aa/10000;bf=aa/1000%10;cf=aa/100%100%10; //获取输入的四个数字df=aa/10%1000%100%10;ef=aa%10000%1000%100%10;if(af>a)printf("\n第一个数应该比%d 小一点!\n",af);if(af==a)printf("恭喜!第一个数字对了!应该是: %d\n",af); //数字比较if(af<a)printf("第一个数应该比%d 大一点!\n",af);if(bf>b)printf("第二个数应该比%d 小一点!\n",bf);if(bf==b)printf("恭喜!第二个数字对了!就是: %d\n",bf); //数字比较if(bf<b)printf("第二个数应该比%d 大一点!\n",bf); 15if(cf>c)printf("第三个数应该比%d 小一点!\n",cf);if(cf==c)printf("恭喜!第三个数字对了!就是: %d\n",cf); //数字比较if(cf<c)printf("第三个数应该比%d 大一点!\n",cf);if(df>d)printf("第四个数应该比%d 小一点!\n",df);if(df==d)printf("恭喜!第四个数字对了!就是: %d\n",df); //数字比较if(df<d)printf("第四个数应该比%d 大一点!\n\n",df);if(ef>e)printf("第五个数应该比%d 小一点!\n",ef);if(ef==e)printf("恭喜!第五个数字对了!就是: %d\n",ef); //数字比较if(ef<e)printf("第五个数应该比%d 大一点!\n\n",ef);return(1);}}16void addrec() //判断并且增加记录{int a; //标志位a=100;recread();rec.sorce= rec.sorce+a;recwrite();printf("请按任意键继续...\n");getchar();getchar();newgame();}int qushu( ) //取数函数{int a=0;int i=0;17srand( (unsigned)time( NULL ) ); //随机数种子设置while(i==0){i=rand(); //这样做就是为了增加随机性i=i%100;}L: for(int j=0;j<i;j++){a=a+rand(); //判断随机数是否是五位数如果不是继续取数}a=(a/i+rand())%100000;if(a<10000)goto L;return(a);}18 3.课程设计总结这次的课程设计总算是告一段落,虽然源程序不是自己亲自全部完成,但是毕竟也花费了自己的很多精力,从开始的到网上查找资料,然后自己一点点的改正,调试,中间的好多问题自己都没有办法解决,也看不懂,也让自己知道光靠书本的那点知识是无法解决的,书本的知识毕竟是有限的。
c猜数游戏源代码含登录等
![c猜数游戏源代码含登录等](https://img.taocdn.com/s3/m/2ba19dfd2dc58bd63186bceb19e8b8f67d1cef50.png)
c猜数游戏源代码含登录等预览说明:预览图片所展示的格式为文档的源格式展示,下载源文件没有水印,内容可编辑和复制c++猜数游戏源代码(含登录等)/*c++猜数游戏游戏规则:用户有7/6/5次机会猜测0~99的随机数,猜中+10/20/40分,没猜中-10/20/40分。
用户名、密码、分数被保存在文件中。
制作者:LH*/#include <iostream>#include <fstream>#include <string>#include <ctime>using namespace std;class Users{public:Users(); //构造函数,默认分数为0,用户个数为0,首次使用时建立一个文件void zpm(); //主屏幕private:void choose(); //选择void logo();//主图void dl(); //登录void zc(); //注册void yx_e(); //游戏_简单void yx_m(); //游戏_中等void yx_d(); //游戏_困难void gxsj(); //更新数据void choose_level();//选择级别string name; //当前用户名string mima; //当前密码string rmima; //确认密码int score; //当前分数int num; //计数int n; //用户的个数int score0[50]; //用于查找、比较。
下同。
string name0[50];string mima0[50];};Users::Users(){score=0;num=0;ifstream infile("user.dat");if(!infile) //如果打开失败,即文件不存在{ofstream outfile("user.dat"); //建立此文件outfile<<0<<'\'; //默认用户个数为0 outfile.close();}infile.close();}void Users::zpm(){logo();cout<<"游戏规则:\1.您需要有您的用户名和密码才能登录,如果您是新用户,请注册。
自制猜数字游戏
![自制猜数字游戏](https://img.taocdn.com/s3/m/7b6c6f12227916888486d7c5.png)
自制简易猜数字游戏该小程序是c语言编写,在codeblocks下编译运行完成.主要是对大一下学期学习谭浩强c语言这门课程做一个小小的总结(结合在网上学习到的一些知识),同时为将来要学c语言的各位童鞋们提供一个小小的参考资料, 小弟目前能力有限……大神勿喷哦……转载请注明出处哦。
应该有人会发现这篇文章吧。
以下是代码及说明。
#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <windows.h>#include <time.h>#define MAX__X 19#define MAX__Y 40int diff=6;//定义全局变量。
游戏默认猜的次数void gotoxy(int x,int y)//坐标定位{COORD loc;loc.X = y;loc.Y = x;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);}void title(){gotoxy(1,4);puts("The simple number guessing game");gotoxy(2,9);printf("made by 王镛(tom)");}void InitGame()//调整界面大小及颜色{system("mode con cols=42 lines=20");system("color F0");}void drow_wall()//画出边界图案{int i;for(i = 0; i <= MAX__Y; i++){gotoxy(0,i);printf("O");gotoxy(MAX__X,i);printf("O");if(i <= MAX__X){gotoxy(i,0);printf("X");gotoxy(i,MAX__Y);printf("X");}}}int first()//第一屏图案{gotoxy(2,7);printf("============================");gotoxy(3,10);printf("1.start ");gotoxy(5,10);printf("2.Change the difficulty ");gotoxy(7,10);printf("Please choose [ ] ");gotoxy(9,7);printf("============================");gotoxy(11,8);printf("The player have six guesses.");gotoxy(7,25);}int change()//改变难度界面{system("cls");gotoxy(2,7);printf("============================");gotoxy(5,9);printf("Please input a number : ");gotoxy(8,7);printf("============================");gotoxy(9,7);printf("example:");gotoxy(11,7);printf(" 3 means you have 3 guesses.");gotoxy(5,32);scanf("%d",&diff);//此处用以改变全局变量的值即游戏难度}int compare()//自定义比较函数,输入值与系统随机产生值作比较{int a,b,c;srand( (unsigned)time( NULL ) );//系统随机产生一个两位数字及下一行b=rand()%(100-10+1)+10; //产生的数赋给变量bfor(c=0;c<diff;c++) //用c作循环控制变量,游戏做几次比较{gotoxy(7,4);printf("Two bit integer:");gotoxy(10,4);printf("prompt:");gotoxy(17,4);printf("注意:输入除数字以外的键程序将退出");gotoxy(7,21);scanf("%d",&a);if(a>b){gotoxy(12,5);puts("The number is smaller than yours.");}else if(a<b){gotoxy(12,5);puts("The number is bigger than yours.");}else{system("cls");gotoxy(2,7);printf("============================");gotoxy(4,16);printf("YOU WON!!! ");gotoxy(5,13);printf("CONGRATULATION ! ");gotoxy(7,7);printf("============================");Sleep(3000);break;//如果在所限次数之内猜中,则跳出循环继续运行下面的语句。
C语言猜数游戏代码
![C语言猜数游戏代码](https://img.taocdn.com/s3/m/34dbcd6e0b1c59eef9c7b406.png)
#include <stdio.h>#include <math.h>#include <stdlib.h>#include <time.h>int i;long int rn();void fun();void gc();int input(long int);void grade(int);int welcome();int compare(long int,long int);void choise(int *,int);void main(){system("cls");printf("\n\n\n\t\t* * * * * * * * welcome * * * * * * * *");printf("\n\n\n");printf("\t\t\tmaker: xiachao xiaoyuyan");printf("\n\n\n\t\t\tuse enter to be continue");getch();fun();}void fun(){int b,s,g;long int a;system("cls");s=welcome();if(s==1){system("cls");a=rn();b=input(a);grade(b);gc();}if(s==2){system("cls");printf("\n\n\n\n\n");printf("\t---------------------------------------------------------\n");printf("\t|game rules: |\n");printf("\t||\n");printf("\t|input your number(10000--99999),it will compare to the |\n");printf("\t||\n");printf("\t|system's number(10000--99999),and the number doesn't have|\n");printf("\t||\n");printf("\t|same digit. |\n");printf("\t||\n");printf("\t|if your number as same as system,your win,else try again.|\n");printf("\t||\n");printf("\t ---------------------------------------------------------");gc();}if(s==3){printf("\n\n\t\t\tThanks for your using.\n");printf("\t\t\t\tBye Bye!");getch();exit(0);}}long int rn(){int b,d;long int c;unsigned long int a;int array[5];time_t t;time(&t);srand(t);loop1:a=10000+rand() % 90000;c=a;for(i=0;i<5;i++){array[i]=c%10;c=c/10;}choise(array,5);b=array[0];for(i=1;i<5;i++){if(b==array[i])goto loop1;elseb=array[i];}return a;}void choise(int *num,int n){int j,k;for(i=1;i<n;i++){k=num[i];j=i-1;while((j>=0)&&(num[j]>k)){num[j+1]=num[j];j--;}num[j+1]=k;}}int compare(long int a,long int b) {int c;if(a>b){printf("\t\t\tneed greater!");printf("( >%ld )\n",b);c=0;}if(a<b){printf("\t\t\tneed less!");printf("( <%ld )\n",b);c=0;}if(a==b){printf("\t\t\tIt's true!\n");c=1;}return c;}int input(long int a){int c=0;long int b;printf("\n\n\n\n\n");printf("\t\t\tplease input your number:\n");printf("\t\t\tinput 0 back to main menu.");loop2:printf("\n\t\t\ttimes %d:",c+1);scanf("%ld",&b);c++;if(c>67){printf("\t\tyou are lose!");gc();}if(b==0)fun();elseif(compare(a,b)!=1) goto loop2;return c;}void grade(int num){int grade=0;printf("\t\t\tyou use %d times\n",num);if(num<4)grade=100-10*(num-1);else if(num<8)grade=80-5*(num-3);else if(num<68)grade=60-(num-7);printf("\t\t\tyour grade is %d",grade);switch(num){case 1:printf("\tyou are so lucky!!!");break;case 2:case 3:case 4:printf("\tlucky!");break;case 5:case 6:case 7:case 8:case 9:printf("\tgreat!");break;case 10:case 11:case 12:case 13:case 14:case 15:case 16:printf("\tgood!");break;case 17:case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 25: printf("\tjust so so.");break;default:printf("\tmaybe you can better.");}}int welcome(){int s;printf("\n\n\n\n");printf("\t\t********************************************\n");printf("\t\t* *\n");printf("\t\t* welcome paly the game! *\n");printf("\t\t* *\n");printf("\t\t* input 1: start the game *\n");printf("\t\t* input 2: help information *\n");printf("\t\t* input 3: quit the game *\n");printf("\t\t********************************************\n");printf("\t\tplease select: ");scanf("%d",&s);return s;}void gc(){int a;printf("\n\n\t\tinput 1: quit the game;\tinput 2: back to menu.\n");printf("\n\t\tplease select : ");scanf("%d",&a);if(a==1)exit(0);if(a==2){system("cls");fun();}if(a!=1&&a!=2){printf("\t\tinput error!check your input!\n\n");gc();}}。
C#基础:猜数字小游戏
![C#基础:猜数字小游戏](https://img.taocdn.com/s3/m/31ffec1c4b7302768e9951e79b89680203d86b48.png)
C#基础:猜数字⼩游戏系统随机⽣成0-100之间的数字,玩家有3次机会,每次猜错系统都会进⾏提⽰,3次都错就失败。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp1{class Program{#region规则//系统随机⽣成0-100之间的数字,玩家有3次机会,每次猜错系统都会进⾏提⽰,3次都错就失败#endregionstatic void Main(string[] args){Random random = new Random();//实例化⼀个随机数⽣成器while (true)//写⼀个循环,让他玩完⼀局后进⾏下⼀局{int n = random.Next(0, 101);//定义⼀个int变量,来接收每⼀局随机⽣成的变量,我们这⾥传递的两个都是int参数,那么他最⼤的值不会返回,只会返回最⼤值减⼀ Console.WriteLine("游戏开始,请猜出您认为最有可能的数字:");int count = 0;//这个变量代表他已经猜了多少次了while (count<3)//当次数⼩于3,我们就来接收他的数据{int input = int.Parse(Console.ReadLine());if (input == n)//如果猜对了,就跳出这个循环{count = 3;//⼤于或等于3,下⼀次就不会继续执⾏了Console.WriteLine("恭喜你,答对了,挑战成功");}else if (input > n){count += 1;//猜错了,可能机会已经够3次了,所以我们要他每次加等于1,⽤if循环再判断。
C语言实现猜数字小游戏
![C语言实现猜数字小游戏](https://img.taocdn.com/s3/m/9bb49b25ef06eff9aef8941ea76e58fafab045d4.png)
C语⾔实现猜数字⼩游戏本⽂实例为⼤家分享了C语⾔猜数字的具体代码,供⼤家参考,具体内容如下⼀、描述猜数字游戏。
⼆、程序使⽤srand((unsigned)time(NULL)),产⽣随机数种⼦。
int random = rand() % 100 + 1,产⽣0~100之间的随机数。
应加上头⽂件#include<time.h>#include<stdio.h>#include<windows.h>#include<time.h>void menu(){printf("#######################\n");printf("# 1 Play #\n");printf("# 0 Exit #\n");printf("#######################\n");}void Play(){int m = 0;int random = rand() % 100 + 1;while (1){printf("请输⼊⼀个数字:\n");scanf_s("%d", &m);if (m == random){printf("你猜对了\n");break;}else if (m > random){printf("你猜⼤了,请重新输⼊!");}else{printf("你猜⼩了,请重新输⼊!");}}}int main(){srand((unsigned)time(NULL));int select = 0;do {menu();printf("请选择:");scanf_s("%d", &select);switch (select){case 1:Play();break;case 0:printf("ByeBye!\n");break;default:printf("输⼊错误,请重新输⼊!\n");break;}} while (select);system("pause");return 0;}三、运⾏结果以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
C语言——猜数字游戏代码
![C语言——猜数字游戏代码](https://img.taocdn.com/s3/m/b83732a2970590c69ec3d5bbfd0a79563c1ed46b.png)
C语言——猜数字游戏代码#include#include#includeint x[4],y[4];//x是计算机产生的数字,y是玩家输入的数字void begin(void);//初始页void start(void);//开始游戏void creat_num(void);//创建数字int judgenum(int *n);//判断输入数字是否正确int judgeA(int *a,int *b);//获取Aint judgeB(int *a,int *b);//获取Bvoid win(void);//赢游戏void lose(void);//输游戏/************************************************************** ***********/int main() /*主函数*/{char PRESS; /*按键变量*/int i,A,B,outwhile=0; /*outwhlie为跳出循环标志*/begin(); /*显示初始页面和游戏说明*/do{scanf("%c",&PRESS); /*获取按键*/fflush(stdin); /*清除缓存*/switch(PRESS){case 'e':case 'E':exit(0);break; /*退出*/case 's':case 'S': /*是开始键*/start(); /*开始新游戏*/for(i=1;i<=8;i++) /*循环8次*/{y[0]=y[1]=y[2]=y[3]=-1;/*输入前使y置于-1*/printf("\n请输入第%d个四位数,各位之间以空格隔开:",i); do{scanf("%d%d%d%d",&y[0],&y[1],&y[2],&y[3]);fflush(stdin);/*清除缓存*/if(judgenum(y)==0) /*判断输入是否合法*/printf("输入数字不合法,请重新输入:");}while(!judgenum(y));A=judgeA(x,y); /*获取A*/B=judgeB(x,y); /*获取B*/if(A==4)break; /*如果A等于4,跳出循环*/printf("%dA%dB\n",A,B);printf("继续输入(c)看答案(a)离开(e)\n");outwhile=0; /*置零*/while(outwhile==0){scanf("%c",&PRESS);fflush(stdin); /*清除缓存*/switch(PRESS){case 'c':case 'C':outwhile=1;break;/*继续输入*/case 'a':case 'A':outwhile=1;goto out;break;/*看答案,认输*/ case 'e':case 'E':outwhile=1;exit(0);break;/*退出游戏*/default :printf("输入错误,请重新输入:");break;/*输入错误*/}}}out:if(A==4) win(); /*A等于4时赢*/else lose(); /*A不等于4时输*/break;default :printf("输入错误,请重新输入:");break; /*输入错误*/ }}while(1);return 0;}/*主函数结束*//************************************************************** *****************/void begin(void)//初始页和帮助{printf("\n\n\n\n\n");printf("******************************************************* *************\n");printf(" 猜数字游戏\n");printf("游戏帮助:\n");printf(" 每局游戏,计算机将随机产生一个四位整数,这个整数各个位上的数字都\n");printf("不重复,且首位可以为0。
用C++编程语言编写猜数字游戏示例
![用C++编程语言编写猜数字游戏示例](https://img.taocdn.com/s3/m/a362c34e1611cc7931b765ce05087632311274fa.png)
用C++编程语言编写猜数字游戏示例文章标题:用C++编程语言编写猜数字游戏示例介绍内容:猜数字游戏是一种常见且富有乐趣的游戏,编程语言可以帮助我们实现一个交互式的猜数字游戏程序。
本文将介绍如何使用C++编程语言编写一个简单而有趣的猜数字游戏示例。
首先,我们需要包含C++标准库中的iostream和cstdlib头文件,以便使用输入输出和随机数生成的函数。
接下来,我们可以定义一个范围内的随机数作为答案,然后让玩家进行数字的猜测。
下面是一个示例代码,用于实现一个猜数字游戏:```cpp#include <iostream>#include <cstdlib>#include <ctime>int main() {srand(time(0)); // 根据当前时间设置随机种子int answer = rand() % 100 + 1; // 生成1到100之间的随机数int guess;int attempts = 0;std::cout << "欢迎来到猜数字游戏!\n";std::cout << "我已经想好了一个1到100之间的数字,你来猜猜看吧!\n";do {std::cout << "请输入你的猜测:";std::cin >> guess;if (guess < answer) {std::cout << "猜小了,请再试一次。
\n";} else if (guess > answer) {std::cout << "猜大了,请再试一次。
\n";}attempts++;} while (guess != answer);std::cout << "恭喜你猜对了!答案是" << answer << "。
C语言编程猜数游戏代码
![C语言编程猜数游戏代码](https://img.taocdn.com/s3/m/e981c536492fb4daa58da0116c175f0e7cd119a1.png)
一 实现思路1、游戏模块(1)计算机给出四位随机数,要求数字不重复(2)用户从键盘输入四位数,要求与计算机给出的四位数数字和位数均相同(3)每猜一次,提示信息(x ,y ),x 表示位置和数字均相同的个数,y 表示数字相同,位置 不同的个数。
同时,给出得分「基础分1000分,猜错一次-100,猜对+20」(4)h 键作为帮助键,给出更为准确的信息,具体到某个数字是否正确,每使用一次帮助键-100分;k 键作为答案给出键,得分归零,并结束游戏(5)猜对答案后若选择继续游戏将会以你的得分为基础分进行第二轮游戏2、记录模块(1)每次游戏开始之前,将得分最高的前三名显示出来(2)每次游戏结束后,将前三名分值存盘二 流程图三设计细节(1)头文件的说明stdio.h 包含标准输入输出函数定义的头文件stdlib.h 包含最常用系统函数定义的头文件time.h 包含日期和时间函数定义的头文件conio.h包含通过控制台进行数据输入和数据输出函数定义的头文件string.h包含用字符数组函数定义的头文件(2)自定义函数的说明void start():绘制游戏的进入界面intdifferentnumber():判断电脑生成的四位数字是否一样 voidmakenumber():电脑随机产生四位数并存到一个一维数组中voidinputnumber():用户输入一个四位数并把它们分开存入一个数组中void judge():比较两个数组并给出相关提示voidhelph():帮助键hvoid helpk():帮助键kint game():游戏主流程,详见流程图void reclod():记录函数四系统调试出现的问题1、进入judge函数时,没有对初始的*x和*y重新赋值为0,导致x,y的值在做累加,实数对数值偏大。
2、if-else 语句没有在if后加大括号,只执行到if后一条,第二条则不在if语句的管辖范围内,导致if语句出错3、当两个scanf函数连接较密时,应在第二的scanf里加入空格,以清除缓冲区的内容,以便下一次存储数据(如:scanf(” %c”,&ch)4、文件和源代码没有保存在同一地址下,导致打开文件失败5、显示的内容没有看清楚就被清屏函数清屏,在清屏函数前加一个getch()即可五测试数据及测试结果分析图 1 说明游戏规则按任意键开始游戏图 2 输入一个数字给出提示信息和注解图3 按h键获得帮助图4 按k键正确输入密码后可得到答案同时分数归0图5 按k键输入错误密码无法获得答案图 6 猜对后显示分数图7 将成绩与排行榜比较并显示输入y继续游戏输入n结束游戏六总结c语言对于刚接触的学生,会感到很陌生。
c猜数游戏代码
![c猜数游戏代码](https://img.taocdn.com/s3/m/0ad8af33ccbff121dd368390.png)
c#猜数游戏代码using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace 猜数字{public partial class Form1 : Form{public Form1(){InitializeComponent();}private static bool _beginflag;private static int _guesstimes = 1;public int[] correctnumber = new int[4];public void BeginButton_Click(object sender, EventArgs e) {_guesstimes = 0;#region 生成随机数Random rand = new Random();correctnumber[0] = rand.Next(0, 9);correctnumber[1] = rand.Next(0, 9);correctnumber[2] = rand.Next(0, 9);correctnumber[3] = rand.Next(0, 9);while (correctnumber[0] == correctnumber[1] || correctnumber[0] == correctnumber[2] ||correctnumber[0] == correctnumber[3]){correctnumber[0] = rand.Next(0, 9);}while (correctnumber[1] == correctnumber[0] || correctnumber[1] == correctnumber[2] ||correctnumber[1] == correctnumber[3]){correctnumber[1] = rand.Next(0, 9);}while (correctnumber[2] == correctnumber[0] || correctnumber[2] == correctnumber[1] ||correctnumber[2] == correctnumber[3]){correctnumber[2] = rand.Next(0, 9);}while (correctnumber[3] == correctnumber[1] || correctnumber[3] == correctnumber[2] ||correctnumber[3] == correctnumber[0]){correctnumber[3] = rand.Next(0, 9);}#endregiontextBox5.Text += "开始游戏..." + "\r\n"; textBox5.Text += "提示信息..." + "\r\n";for (int i = 0; i < correctnumber.Length; i++){textBox6.Text += correctnumber[i].ToString();}_beginflag = true;BeginButton.Enabled = false;}private void GuessButton_Click(object sender, EventArgs e){int allcorrectcount = 0;int numbercorrectcount = 0;if (_beginflag == false){MessageBox.Show("游戏尚未开始!");}else if(textBox1.Text == textBox2.Text ||textBox1.Text == textBox3.Text ||textBox1.Text == textBox4.Text ||textBox2.Text == textBox3.Text ||textBox2.Text == textBox4.Text ||textBox3.Text == textBox4.Text ){MessageBox.Show("请输入不重复的四个数字且不能为空!");textBox1.Text = "";textBox2.Text = "";textBox3.Text = "";textBox4.Text = "";}else if (textBox1.Text == "" ||textBox2.Text == "" ||textBox3.Text == "" ||textBox4.Text == ""){MessageBox.Show("各个数字均不能为空!");textBox1.Text = "";textBox2.Text = "";textBox3.Text = "";textBox4.Text = "";}else{if (textBox1.Text == correctnumber[0].ToString() && textBox2.Text == correctnumber[1].ToString() && textBox3.Text == correctnumber[2].ToString() && textBox4.Text == correctnumber[3].ToString()){MessageBox.Show("恭喜你猜对了!");_guesstimes = 0;}for (int i = 0; i < 4; i++){if (textBox1.Text == correctnumber[i].ToString()){if (i == 0){allcorrectcount += 1;}else{numbercorrectcount += 1;}}}for (int i = 0; i < 4; i++){if (textBox2.Text == correctnumber[i].ToString()){if (i == 1){allcorrectcount += 1;}else{numbercorrectcount += 1;}}for (int i = 0; i < 4; i++){if (textBox3.Text == correctnumber[i].ToString()){if (i == 2){allcorrectcount += 1;}else{numbercorrectcount += 1;}}}for (int i = 0; i < 4; i++){if (textBox4.Text == correctnumber[i].ToString()){if (i == 3){allcorrectcount += 1;}else{numbercorrectcount += 1;}}}textBox5.Text += "本次有" + allcorrectcount + "个数字和位置都正确,有" + numbercorrectcount + "个数字正确,位置不正确。
C语言代码实现猜数字游戏
![C语言代码实现猜数字游戏](https://img.taocdn.com/s3/m/abca3abcdc3383c4bb4cf7ec4afe04a1b071b0ea.png)
C语⾔代码实现猜数字游戏⽬录前⾔:⼀:猜数字游戏基本介绍&对程序预期⼆:程序设计思路1.总体思路:2.菜单部分函数:3.游戏部分函数4.主函数部分5.代码主体三:总结前⾔:本⽂主要讲解以c语⾔编写猜数字游戏,⽬的是介绍C语⾔中的循环和分⽀的具体⽤法。
⼀:猜数字游戏基本介绍&对程序预期.猜数字游戏,顾名思义,就是系统随机给出⼀个数字,玩家对这个数字进⾏猜测,如果玩家猜测的数字⼤于给出的数字,则系统会提⽰猜⼤了,如果玩家猜测的数字⼩于给出的数字,则系统会提⽰猜⼩了,这样逐步缩⼩猜测范围,直到玩家猜对为⽌。
2.程序预期:猜数字结束后,玩家如果猜对了的话可以继续游戏,也可以退出游戏⼆:程序设计思路1.总体思路:⾸先,实现游戏功能必须要有⼀个菜单来提⽰玩家如何进⾏操作。
其次,为了代码具有可读性,可以将游戏部分函数和菜单部分函数作为两个⼦函数,然后在主函数调⽤他们。
然后我们为了实现玩家可以多次游戏可以使⽤循环。
综上所述,我们应该设计两个⼦函数:菜单函数和游戏函数。
⼀个主函数2.菜单部分函数:菜单部分函数void menu(){printf("***********************************************\n");printf("*************************1play*****************\n");printf("*************************0exit*****************\n");printf("***********************************************\n");}菜单函数设计相对简单,只是为了提⽰玩家如何操作3.游戏部分函数void game(){srand((unsigned int)time(NULL));int ret = rand() % 100 + 1;while (1){printf("猜数字\n");int guess = 0;scanf("%d", &guess);if (guess > ret){printf("猜⼤了\n");}else if (guess < ret){printf("猜⼩了\n");}else{printf("猜对了\n");break;}}}①在游戏函数部分,⾸先要⽣成随机数,⽣成随机数的⽅法如下:C语⾔中提供了⽣成随机数的函数rand函数rand函数会返回⼀个0到RAND-MAX的随机整数,使⽤这个函数需要包含头⽂件<stdlib.h>但是rand函数每次⽣成的随机数相同,所以只⽤rand函数不够,所以要使⽤srand()函数增加随机性后⼀句中说明在使⽤rand函数前要定义⼀个srand()函数如画线句所⽰,srand函数的作⽤就是⼀个随机数的⽣成的种⼦。
自己做的C语言猜数字
![自己做的C语言猜数字](https://img.taocdn.com/s3/m/d860b8c46137ee06eff91874.png)
}
}
printf("有 %d个数数值和位置都相同!有 %d个数数值相同,但位置不同。\n",record,current);
if(record==count)
{
score=(k+1)*100/try_time;
PaintScoreBang();
}
break;
case 3:
PaintHelp();
break;
case 4:
InputCount();
break;
for(i=0;i<jifenbanggeshu;i++)
{
memset(his_name[i],0,name);
}
try_time=10;
count=4;
flag=0;
restart :
PaintMenu();
system("pause");
goto reNewGame;
}
}
}
void suijishu(int* ran,int count)
{
int i,j;
i=0;
while (i<count)
}
else
{
printf("**************************操作菜单**************************\n");
printf("\n");
printf("\n");
printf("\n");
case 0:
c语言入门猜数字游戏(解释每行代码)
![c语言入门猜数字游戏(解释每行代码)](https://img.taocdn.com/s3/m/b29e8765e55c3b3567ec102de2bd960591c6d971.png)
c语言入门猜数字游戏(解释每行代码)这是一个“猜数字”的小游戏,玩家需要猜出随机生成的数字。
#include <stdio.h> // 1. 引入标准输入输出库#include <stdlib.h> // 2. 引入标准库#include <time.h> // 3. 引入时间相关库int main() { // 4. 主函数开始int num,guess; // 5. 定义两个整型变量: num和guesssrand(time(NULL)); // 6. 初始化随机数种子num = rand() % 100 + 1; // 7. 生成1到100之间的随机整数printf("请猜一个1~100之间的整数:\n"); // 8. 提示玩家输入do { // 9. 进入循环体scanf("%d", &guess); // 10. 接收玩家的输入if (guess > num) { // 11. 判断玩家猜的数是否太大printf("你猜的数字太大了!\n继续猜:\n");} else if (guess < num) { // 12. 判断玩家猜的数是否太小printf("你猜的数字太小了!\n继续猜:\n");}} while (guess != num); // 13. 如果猜错了就继续循环printf("恭喜你,猜对了!\n"); // 14. 玩家猜对了,输出结果return 0; // 15. 返回0表示程序结束}各行代码解释如下:1. 引入`stdio.h`库,提供了输入和输出的函数和符号常量。
2. 引入`stdlib.h`库,提供一些标准函数功能,如内存分配等。
3. 引入`time.h`库,提供了处理时间的函数。
4. `int main()`是程序的起点,主函数开始执行。
C++实现简单猜数字小游戏
![C++实现简单猜数字小游戏](https://img.taocdn.com/s3/m/6e55f5b0294ac850ad02de80d4d8d15abe2300f5.png)
C++实现简单猜数字⼩游戏本⽂实例为⼤家分享了C++实现简单猜数字⼩游戏的具体代码,供⼤家参考,具体内容如下⼀、随机数本⽂采⽤time(0)作为srand()函数的种⼦⽣成随机数,time(0)为1970年1⽉1⽇0时0分0秒到此时的秒数。
本⽂随机数范围控制在0~100,可根据⾃⼰需求进⾏更改。
⼆、次数本⽂代码中times代表次数,可根据⾃⼰需求进⾏更改。
三、胜负条件数字猜对即为胜利,次数耗尽前仍未猜中即为失败,结束后继续游玩请输⼊1,退出游戏输⼊其他任意字符。
(本⽂设置游戏在输⼊正确范围的数字后才会开始,输⼊错误范围的数字仍会减少次数)四、代码#include<iostream>#include<cstdlib>#include<ctime>using namespace std;int main(){int num1 = 0;int num2 = 0;int num3 = 1;int times = 7;while (num3 == 1){times = 7;srand((unsigned int)time(NULL));num1 = rand() % 100;cout << "游戏开始,请输⼊你的猜测结果,共"<<times<<"次机会,数字范围为0~100:" << endl;cin >> num2;while (num2 > 100 || num2 < 0){cout << "输⼊错误,请重新输⼊0~100的数字:" << endl;cin >> num2;}times -= 1;cout << "游戏正式开始"<< endl;while (1){if (times == 0){cout << "次数⽤尽,游戏失败" << endl;break;}if (num2 > num1){cout << "你猜测的数字过⼤,剩余次数:"<<times<<",请重新输⼊:" << endl;cin >> num2;times -= 1;}if (num2 < num1){cout << "你猜测的数字过⼩,剩余次数:" << times << ",请重新输⼊:" << endl;cin >> num2;times -= 1;}if (num2 == num1){cout << "猜对了,数字为" << num1 << endl;break;}if (num2 > 100 || num2 < 0){cout << "请输⼊正确范围的数字,剩余次数:" << times << ",请重新输⼊:" << endl; cin >> num2;times -= 1;}}cout << "继续游玩请输⼊1,退出游戏输⼊其他任意字符" << endl;cin >> num3;}system("pause");return 0;}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<stdio.h>
#include<time.h>//time()需要用到这个头文件
#include<stdlib.h>//rand()和srand()需要用到这个头文件
int main()
{
int answer,num;//声明“答案”和“用户输入的数”两个变量。
srand((unsigned)time(NULL));//用srand()函数设置随机数种子。参数是系统时间。
answer = rand()%101;//随机生成答案,范围是0-100。
while(1)//死循环。直到输入正确答案,跳出。
{
printf("%请输入数字:");
scanf("%d",&num);//从键盘接收数,存到num。
if(num == answer)//如果num和answer相等
{
printf("YOU ARE WINNER!\n");//输出获胜信息并跳出循环。
break;
}
else//执行到elseswer)//判断数值大于还是小于答案,并输出相应的提示信息。
printf("小了!\n");
else
printf("大了!\n");
}
}
return 0;
}