收藏 分销(赏)

猜数字游戏的分析与实现课案doc资料.doc

上传人:w****g 文档编号:4134906 上传时间:2024-07-31 格式:DOC 页数:20 大小:253KB 下载积分:10 金币
下载 相关 举报
猜数字游戏的分析与实现课案doc资料.doc_第1页
第1页 / 共20页
猜数字游戏的分析与实现课案doc资料.doc_第2页
第2页 / 共20页


点击查看更多>>
资源描述
合肥工业大学计算机与信息学院 课程设计报告 设计题目: 猜数字及生命游戏 学生姓名: 李挺 专 业: 电子信息工程 班 级: 13级1班 学 号: 2013211732 指导教师: 于磊 完成日期: 2014年7月9日 目录 一 游戏说明 二 总体设计 三 程序代码 四 运行结果 五 附录文档 一 游戏说明(猜数字) 1使用说明 (1)开始功能键选择 (2)系统将提交的数与它自动产生的数进行比较,输出结果“*A*B”形式。其中A代表数字正确相应位置也正确,B代表数字正确但位置不正确。如:1A3B表示游戏者有1个数字的位置正确且数值也正确,除此以外,游戏者还猜对了3个数字,但位置不对,即游戏者已经猜出4位数据,但有3个数据位置不对 2程序说明书 (1)程序的功能说明:猜数字游戏是一个运行在Windows平台下的小型游戏软件,依据猜数字游戏的基本规则,给用户进行成绩排行,并可以随着玩家的游戏记录进行更新。游戏用户可以选择开始游、排行榜查看、帮助等主要功能。当用户选择开始游戏时,系统会让用户逐次猜数字,用户每次猜数字后系统会给出本次猜数字的结果提示,如位置和数字都正确的数字个数、数字正确但位置不正确的数字个数。如果用户猜数字的次数超过10次,则提示用户游戏失败,如果在10次之内猜出4个数字和正确的顺序,则提示用户猜数字成功,并到排行榜中进行比较,如果排名在前一名,则计入排行榜中,以便其他用户查看。在排行榜查看功能中,用户可以查看当前的游戏排行,如有新的记录产生,则插入到排行榜中,并把排行榜中位次最末的记录挤出排行榜。在帮助模块,用户可以查看游戏规则,当熟悉规则后可以退出帮助模块。 3 软件可以实现的基本功能 (1)游戏区:玩家可以在游戏区逐次猜数字,系统给出玩家每次猜数字后的提示,包括数值、位置均正确的数字个数和数值正确但位置不正确的数字个数。 (2)查看排行榜:玩家可以查看当前排行榜中的排名顺序,从而确定自己的游戏目标。排行榜的查看按一定顺序显示给用户,便于查看。 (5)帮助:玩家通过帮助功能了解游戏规则和排行榜设置规则。 (4)清除排行榜:玩家可以选择清除排行榜功能,使得排行榜上的玩家姓名和玩家游戏记录从系统中清除,只留下系统默认的玩家名(player)和系统默认记录10次。 二 总体设计 1 要求 (1)要求使用多文件方式实现设计; (2)要求在各个文件内实现结构化设计; (3)每个模块作为一个单独的文件。 2 文件及函数组成 源文件 函数名或其他成分 功能 record.h ASK 宏定义 结构声明 结构声明 库函数及函数原型声明 record.h int choose_level 设置游戏等级 void show_explain 输出游戏说明 record.h bool check_num 检查数据 void show_tiptop 输出最佳排行 record.h void new_game 开始游戏中心 void get_num 获取系统随机数字 Game_in_out 获取游戏者数据和输出结果 Int time 时间控制 Game_result 游戏结果处理输出 record.h int main 主函数 void display() 选择菜单 int choose_menu 处理菜单 三.程序代码 1 主体游戏程序 using namespace std; void rand_num( int[] ); void get_num(int[],int); bool check_num(int[]); int guess_num(int); void goal(int,int,int); void choose_menu(); void new_game(); void show_tiptop(); void show_explain(); void clean_data(); void display(); int choose_level(); void main(){ choose_menu(); } #include<iostream> #include<stdio.h> #include<string.h> #include<fstream> #include<iomanip> #include<stdlib.h> #include<time.h> using namespace std; #pragma warning(disable:4996) struct data{ int goal; char name[20]; }; fstream iofile; void rand_num(int b[]){ int temp; int temp2; int c[10] = { 0 }; for (int i = 0; i < 10; i++) c[i] = i; srand((unsigned)time(0)); for (int j = 0; j < 4; j++){ temp = rand() % (10 - j) + j; temp2 = c[temp]; c[temp] = c[j]; c[j] = temp2; b[j] = c[j]; } for (int k = 0; k < 4; k++) cout << b[k] << " "; cout << endl; } void get_num(int b[],int k) { cout << "\t\t\t请输入你猜的数字,还剩下"<<k<< "机会\n"; cout << "\t\t\t\t"; for (int i = 0; i < 4; i++) cin >> b[i]; } bool check_num(int b[]) { for (int i = 0; i < 4; i++){ if (b[i]<0 || b[i]>9){ cout << "\t\t\t输入错误,请重新输入!\n"; return false; } } return true; } int choose_level(){ int x; cout << "\n\n\n\n"; cout << "\t****************************************************************\n"; cout << "\t****************************************************************\n"; cout << "\t\t\t1.高级\t2.中级\t3.低级\n"; cout << "\t\t\t\t"; cin >> x; while (x > 2 && x < 1){ cout << "\t\t\t输入有误,重新输入!\n"; cout << "\t\t\t\t"; cin >> x; } if (x == 1)return 1; if (x == 2)return 2; if (x == 3)return 3; } int guess_num(int level){ system("cls"); int A ;int B ; int a[4] = { 0 }; int b[4] = { 0 }; rand_num(a); int times = 0; for (int k = level+7; k > 0; k--){ get_num(b,k); while (!check_num(b)){ k--; get_num(b,k); }; A = B = 0; for (int i = 0; i < 4; i++){ if (a[i] == b[i])A++; for (int j = 0; j < 4; j++){ if (i != j&&a[i] == b[j])B++; } } cout << "\t\t\t\t"<<A << " A " << B << " B\n"; times ++; if (A == 4)return times; } for (int k = 0; k < 4; k++) cout << b[k]<<""; cout << endl<<""; return 0; } void goal(int times, int time,int level) {//计算分数并保存 int goal; char name[20]; data temp,temp2; iofile.open("e:\\dataf.dat", ios::in | ios::app | ios::binary); if (!iofile){ cerr << "没有游戏最高分\n"; abort(); } goal = 500 - time * 0.002 - times * 10-level*10; cout << "\t\t\t您的得分是:" << goal<<"\t"<<"时间是"<<time/1000<<"s\n"; iofile.seekg(0, ios::beg);//(level - 1)*sizeof(temp) iofile.read((char*)&temp, sizeof(temp)); //cout << temp.name; if (temp.goal > goal)cout <<"\t\t\t最高分"<< temp.goal<<"\t"<<temp.name; if (temp.goal < goal){ cout << "\t\t\t请输入您的姓名:"; cin >> name; temp.goal = goal; strcpy(temp.name,name); //iofile<<temp[0].goal<<temp[0].name<<endl; //iofile.flush(); iofile.write((char*)&temp,sizeof(temp)); //iofile.read((char*)&temp, sizeof(temp)); cout << "\t\t\t 保存成功\n"; } iofile.close(); } void new_game(){ //开始新游戏 system("cls"); clock_t start, finish; int a; int b; int level; int time; level=choose_level(); start = clock(); a=guess_num(level); finish = clock(); time = (int)(finish - start); goal(a, time,level); cout << "\t\t\t 是否继续?\n" << endl; cout << "\t\t\t1.继续 2.返回" << endl; cout << "\t\t\t\t"; cin >> b; if (b == 1)new_game(); } void show_tiptop(){ int x; data data1[3] = { 500, "ting" ,400,"qian",300,"07161208"}; iofile.open("e:\\dataf.dat",ios::in|ios::app|ios::binary); if (!iofile){ cerr << "没有游戏最高分\n"; abort(); } for (int i=0; i < 3; i++) iofile.write((char*)&data1[i],sizeof(data1[i])); iofile.flush(); cout << "\t\t\t1.高级\t2.中级\t3.低级\t4.返回\n"; cout << "\t\t\t\t "; cin >> x; while (x != 4){ data datal[1]; if (x<1 || x>4)cout << "\t输入有误,请重新输入!\n"; switch (x){ case 1: iofile.seekg((x - 1)*sizeof(datal[x - 1]), ios::beg); iofile.read((char*)&datal[0],sizeof(datal[0])); cout << "\t\t\t 高级:" << datal[0].goal << " " << datal[0].name << endl; break; case 2: iofile.seekg((x - 1)*sizeof(data1[x - 1]), ios::beg); iofile.read((char*)&datal[0],sizeof(datal[0])); cout << "\t\t\t 中级:" << datal[0].goal << " " << datal[0].name << endl; break; case 3: iofile.seekg((x - 1)*sizeof(data1[x - 1]), ios::beg); iofile.read((char*)&datal[0],sizeof(datal[0])); cout << "\t\t\t 低级:" << datal[0].goal << " " << datal[0].name << endl; break; } cout << "\t\t\t\t "; cin >> x; } iofile.close(); } void show_explain(){//查看游戏说明 system("cls"); int a; cout<<"\t\t\t\t游戏说明\n"; cout << "\t****************************************************************\n"; cout << "\t****************************************************************\n"; cout << "\t①:游戏开始,系统随机产生一位不重复的N位数字.N的大小与等级有关\n"; cout << "\t\t初级:N=3\t中级:N=4\t高级:N=5\t\n"; cout << "\t②:游戏者输入所猜的不重复数字,并按回车提交,提交的数据位数应与\n"; cout << "\tN的大小一致,当数据位数大于N时,只取前N位;\n"; cout << "\t③:系统将提交的数与它自动产生的数进行比较,输出结果“*A*B”形式.\n"; cout << "\t其中A代表数字正确相应位置也正确,B代表数字正确但位置不正确.如:\n"; cout << "\t1A3B表示有1个数字的位置正确且数值也正确,除此以外,还猜对了3个数\n"; cout << "\t字但位置不对.即已经猜出4位数据,但有3个数据位置不对!\n"; cout << "\t④:游戏者有限定次数的猜测机会,在规定次数内完成,则游戏成功.\n"; cout << "\t否则,游戏失败.其中.猜测次数与等级有关\n"; cout << "\t\t初级:10次\t中级:9次\t高级:8次。\n"; cout << "\t⑤:按完成游戏所用的时间和次数计算游戏者所得分数,游戏等级越高,\n"; cout << "\t猜测所用的次数越少.得分越高!若游戏者得分比系统已经保存的分数\n"; cout << "\t的高,将提示要求输入游戏者信息.并且保存在最佳排行之中.\n"; cout << "\t⑥:游戏者可以自行设置等级!\n"; cout << "\t**********************************************************\n"; cout << "\t**********************************************************\n"; cout << "\t1.返回主菜单\n" << endl; cin >> a; } void clean_data(){ data temp,temp2; iofile.open("dataf.dat", ios::in | ios::app | ios::binary); temp.goal=0; strcpy(temp.name, "nothing"); for (int i = 0; i < 3; i++) iofile.write((char*)&temp, sizeof(temp)); iofile.read((char*)&temp2, sizeof(temp2)); cout << temp2.name << "\t" << temp2.goal; iofile.close(); } void display(){ system("cls"); cout <<"\n\n\n\n"; cout << "\t\t\t\t 主菜单\n"; cout <<"\t****************************************************************\n"; cout <<"\t****************************************************************\n"; cout <<"\t\t\t\t1.新游戏\n\n" ; cout <<"\t\t\t\t2.查看最高分\n\n"; cout <<"\t\t\t\t3.游戏说明\n\n"; cout <<"\t\t\t\t4.清空所有数据\n\n"; cout <<"\t\t\t\t5.退出游戏\n\n"; cout << "\t****************************************************************\n"; cout << "\t****************************************************************\n"; cout << setw(10) << " " << endl; } //清空所有数据 void choose_menu(){ int x; display(); cout << "\t\t\t\t"; cin >> x; while (x != 5){ switch (x){ case 1: new_game(); break; case 2: show_tiptop(); break; case 3: show_explain(); break; case 4: clean_data(); break; } display(); cout << "\t\t\t\t"; cin >> x; } } 四 运行结果 一 游戏说明(生命游戏) 而每一个格子都可以看成是一个生命体,每个生命都有生和死两种状态,如果该格子生就显示蓝色,死则显示白色。每一个格子旁边都有邻居格子存在,如果我们把3*3的9个格子构成的正方形看成一个基本单位的话,那么这个正方形中心的格子的邻居就是它旁边的8个格子。 每个格子的生死遵循下面的原则: 1) 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变) 。 2) 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变; 3) 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变设定图像中每个像素的初始状态后依据上述的游戏规则演绎生命的变化,由于初始状态和迭代次数不同,将会得到令人叹服的优美图案)。 三.程序代码 #include<iostream> using namespace std; #include<windows.h> #include<conio.h> struct Cell { bool live; int others; }; void main() { Cell cell[40][40]; for (int i = 0; i<40; i++) for (int j = 0; j<40; j++) { cell[i][j].live = true; cell[i][j].others = 0; } while (1) { for (int i = 0; i<40; i++) for (int j = 0; j<40; j++) { cell[i][j].others = 0; } for (int i = 0; i<40; i++) { for (int j = 0; j<40; j++) { if (cell[i][j].live) cout << "$ "; else cout << "- "; } cout << endl; } for (int i = 0; i<40; i++) for (int j = 0; j<40; j++) { if ((i - 1) >= 0 && (j - 1) >= 0 && cell[i - 1][j - 1].live) cell[i][j].others++; if ((i - 1) >= 0 && cell[i - 1][j].live) cell[i][j].others++; if ((i - 1) >= 0 && (j + 1)<40 && cell[i - 1][j + 1].live) cell[i][j].others++; if ((j - 1) >= 0 && cell[i][j - 1].live) cell[i][j].others++; if ((j + 1)<40 && cell[i][j + 1].live) cell[i][j].others++; if ((i + 1)<40 && (j - 1) >= 0 && cell[i + 1][j - 1].live) cell[i][j].others++; if ((i + 1)<40 && cell[i + 1][j].live) cell[i][j].others++; if ((i + 1)<40 && (j + 1)<40 && cell[i + 1][j + 1].live) cell[i][j].others++; switch (cell[i][j].others) { case 2:break; case 3:cell[i][j].live = true; break; default:cell[i][j].live = false; break; } } Sleep(1000); system("cls"); } } 四 运行结果 五 附录文档 1 游戏规则 (1)游戏软件随机产生4个数字,但不显示,给用户提供输入提示符,让用户猜4个数字的值和位置顺序。如果数字值和位置顺序均正确,则猜数字成功。 (2)玩家有10次猜数字的机会,如果在10次之内仍末猜出正确的数字值和数字位置顺序,则提示用户游戏失败。 (3) 一旦玩家在10次的次数限制内猜出正确的数字值和位置顺序,则赢得游戏。 (4)当玩家赢得游戏后,会与排行榜中的前五名玩家的游戏记录比较,如果排名能够进入前五名,则更新排行榜,使当前玩家成为入榜选手。 (5)记分规则:猜数字所用次数即为分数,所以分数越少证明玩家水平越高。 我们可以把计算机中的宇宙想象成是一堆方格子构成的封闭空间,尺寸为N的空间就有N*N个格子。而每一个格子都可以看成是一个生命体,每个生命都有生和死两种状态,如果该格子生就显示蓝色,死则显示白色。每一个格子旁边都有邻居格子存在,如果我们把3*3的9个格子构成的正方形看成一个基本单位的话,那么这个正方形中心的格子的邻居就是它旁边的8个格子。 每个格子的生死遵循下面的原则: 1) 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变) 。 2) 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变; 3) 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变设定图像中每个像素的初始状态后依据上述的游戏规则演绎生命的变化,由于初始状态和迭代次数不同,将会得到令人叹服的优美图案)。 2.心得体会 一直以为用C++语言编写一个程序 是一个十分困难的事情。以前也曾认为过 C++语言不是很实用,但是通过学习 , 也充分认识到了C++语言编程中的乐趣,也懂得了如何应用, 我可以更努力 ,去客服一个个困难。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服