VC++课程设计实验报告对抗游戏.doc

上传人:仙人指路1688 文档编号:2385676 上传时间:2023-02-17 格式:DOC 页数:19 大小:85KB
返回 下载 相关 举报
VC++课程设计实验报告对抗游戏.doc_第1页
第1页 / 共19页
VC++课程设计实验报告对抗游戏.doc_第2页
第2页 / 共19页
VC++课程设计实验报告对抗游戏.doc_第3页
第3页 / 共19页
VC++课程设计实验报告对抗游戏.doc_第4页
第4页 / 共19页
VC++课程设计实验报告对抗游戏.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《VC++课程设计实验报告对抗游戏.doc》由会员分享,可在线阅读,更多相关《VC++课程设计实验报告对抗游戏.doc(19页珍藏版)》请在三一办公上搜索。

1、 理工大学VC课程设计报告课 程:VC+课程设计系 别:计算机科学与技术学院班 级:学 号:姓 名:选题1名称:对抗游戏选题1难易级别:B选题2名称:工资管理选题2难易级别:A指导教师:目 录1.1 程序功能简介一个简单的纸牌对抗游戏,根据随机产生的数字决定双方的输赢。1.2 课程设计要求(1)当双方进行对抗时,游戏者可根据自身的牌点决定是否继续下注,例如拿到A,把握增大,则在计算机的牌点出来之前加大赌注,但不超过5点;(2)将双方牌点的比较扩大为两张牌点之和,若出现一组对子,则对子牌点大于任何单牌,若双方均为对子,则比较牌点之和。21程序具体实现原理总体方案增加变量plyrAddBet,co

2、mpAddBet用来存储玩家和计算机加注的值,值并判断值的大小是否符合要求,若玩家不加注则plyrAddBet,compAddBet赋值为0,并对原成员函数Increase(),Decrease()做一定修改。分别定义变量plyr1,plyr2 ,comp1,comp2用来储存玩家和计算机的两张牌点,而且定义成员函数Player1(),Player2(),Computer1(),Computer2()用来返回玩家和计算机的牌点,再定义成员函数Initialize1() ,Initialize2()随机产生玩家牌点plyr1,plyr2 和计算机的牌点comp1,comp2,再对主函数中的比较语

3、句做出修改。基本原理主函数中首先定义了War类的对象Play,然后分别调用Initialize1() ,Initialize2()随机产生玩家牌点plyr1,plyr2 和计算机的牌点comp1,comp2,调用函数Loops()计算出牌的次数,依次输出玩家的两张牌plyr1,plyr2,询问玩家是否继续加注,若继续加注就将值赋给plyrAddBett,若不继续加注则plyrAddBet赋值为0,然后再依次输出计算机的牌点comp1,comp2,由计算机自动判断是否加注,若加注则compAddBet赋值为3,若不加注则compAddBet赋值为0。再根据要求比较玩家和计算机的牌点大小,调用In

4、crease()或Decrease(),再调用CheckScore()返回玩家的分数,最后用if语句判断游戏是否继续进行。是否开始调用函数Initialize1() ,Initialize2()调用函数CheckScore()结束定义类的对象play用if判断是否循环或结束分别输出Play.Player1(),Play.Player2()是否加大赌注输入Play.plyrAddBet分别输出Play.Computer1()Play.Computer2()调用函数Increase() ,Decrease()调用函数Loops()由计算机自动判断是否加注2.2 程序各个功能说明以及课程设计要求的实

5、现程序结构#include #include #include #define MAX 13/牌的最大点为13class War int plyr1,plyr2 ,comp1,comp2;/plyr玩者的牌点, comp计算机的牌点 static int Score;/玩者的分数,当分数为0或50时游戏结束 static int Loop;/出牌次数,游戏结束时给出总的出牌次数public: static int plyrAddBet,compAddBet;/plyrAddBet,compAddBet分别为玩家和计算机的加大赌注 int Player1(),Player2();/返回玩者的牌点

6、 int Computer1(), Computer2();/返回计算机的牌点 void Initialize1(),Initialize2();/给出玩者和计算机的牌点 int CheckScore();/返回玩者分数 void GameOver(); War();/缺省构造函数,初始化对象 static int Increase();/分数增加 static int Decrease();/分数减少 static int Loops();/出牌次数增加;int War:Score = 26; /初始分数int War:Loop = 0;/比赛轮数int War:Increase()/增分,

7、每轮赢一分加大的赌注 Score=+Score+plyrAddBet+compAddBet; return Score;int War:Decrease()/减分,每轮输一分加大的赌注 Score=-Score-plyrAddBet+compAddBet; return Score;int War:Loops()/双方出牌一次,为一轮 Loop+; return Loop;War:War()/构造函数,初始双方牌点为0 plyr1,plyr2, comp1,comp2 = 0;int War:Player1()/返回玩者牌点 return plyr1;int War:Player2()/返回玩

8、者牌点 return plyr2;int War:Computer1()/返回计算机的牌点 return comp1;int War:Computer2()/返回计算机的牌点 return comp2;void War:Initialize1()/随机产生玩家牌点 /srand ( time (NULL) ); plyr1 = rand()%MAX+2;/牌点为2-14,14点为A plyr2 = rand()%MAX+2;/牌点为2-14,14点为Avoid War:Initialize2()/随机产生计算机牌点 /srand ( time (NULL) ); comp1 = rand()%

9、MAX+2;comp2 = rand()%MAX+2;int War:CheckScore()/返回玩者分数 return Score;void War:GameOver()/游戏结束时的相关信息 cout endl 游戏结束! 谢谢参与. endl; cout 你用了 War:Loops() 轮. endl;int War:plyrAddBet;int War:compAddBet;void main() War Play; int x; cout endl 准备开始! endl endl; Play.Initialize1();/随机产生玩家牌点 Play.Initialize2();Pl

10、ay.Loops();/轮次增加 cout 你的牌是一张; if(Play.Player1() = 11) cout J endl; else if(Play.Player1() = 12) cout Q endl; else if (Play.Player1() = 13) cout K endl; else if (Play.Player1() = 14) cout A endl; else cout Play.Player1() endl; cout 和一张; if(Play.Player2() = 11) cout J endl; else if(Play.Player2() = 12

11、) cout Q endl; else if (Play.Player2() = 13) cout K endl; else if (Play.Player2() = 14) cout A endl; else cout Play.Player2() endl; cout是否继续加注?加注请输入1到5,如不加注请输入0Play.plyrAddBet;if(Play.plyrAddBet5|Play.plyrAddBet0) cout加注错误,重新加注endl; cout是否继续加注?加注请输入1到5,如不加注请输入0Play.plyrAddBet; cout 计算机有一张; if(Play.C

12、omputer1() = 11) cout J endl; else if(Play.Computer1() = 12) cout Q endl; else if (Play.Computer1() = 13) cout K endl; else if (Play.Computer1() = 14) cout A endl; else cout Play.Computer1() endl; cout和一张;if(Play.Computer2() = 11) cout J endl; else if(Play.Computer2() = 12) cout Q endl; else if (Pla

13、y.Computer2() = 13) cout K endl; else if (Play.Computer2() = 14) cout A endl; else cout Play.Computer2() endl; if (Play.Player1()=Play.Player2()&Play.Computer1()!=Play.Computer2() /比较玩家与计算机的输赢 coutPlay.Computer1() cout你赢了; Play.Increase(); if( Play.Player1()Play.Computer1() cout你输了; Play.Decrease();

14、 cout 势均力敌! Play.Computer1()+Play.Computer2() ) cout你赢了; Play.Increase(); if (Play.Player1()+Play.Player2()Play.Computer1()+Play.Computer2() )cout你输了;Play.Decrease(); cout 势均力敌! endl;Play.CheckScore();/返回分数cout endl 你的分数是: Play.CheckScore() endl; if(Play.CheckScore()=52)Play.GameOver(); else cout en

15、dl 继续 1 退出 0 x;if (x = 1)main(); /若是递归调用主函数实现一:当双方进行对抗时,游戏者可根据自身的牌点决定是否继续下注,例如拿到A,把握增大,则在计算机的牌点出来之前加大赌注,但不超过5点。在类War中定义了一个变量AddBet,用来表示玩家加大的赌注。在成员函数Increase()和Decrease()中分别做出修改。具体如下:int War:Increase()/增分,每轮赢一分及加大的赌注 Score=+Score+AddBet+compAddBet; return Score;int War:Decrease()/减分,每轮输一分加大的赌注 Score=

16、-Score-AddBet-compAddBet; return Score;在主函数中也加入了一段输入语句:cout是否继续加注?加注请输入1到5,如不加注请输入0Play. plyrAddBet;if(Play. plyrAddBet5|Play. plyrAddBet0) cout加注错误,重新加注endl; cout是否继续加注?加注请输入1到5,如不加注请输入0Play. plyrAddBet;实现二:将双方牌点的比较扩大为两张牌点之和,若出现一组对子,则对子牌点大于任何单牌,若双方均为对子,则比较牌点之和。在类War中分别定义了函数Player1(),Player2()用来返回玩者

17、的牌点,Computer1(), Computer2()用来返回计算机的牌点,Initialize1(),Initialize2()用来随机产生玩者和计算机的牌点。在主函数中分别调用这些函数,具体修改如下: Play.Initialize1();/随机产生玩家牌点 Play.Initialize2();Play.Loops();/轮次增加 cout 你的牌是一张; if(Play.Player1() = 11) cout J endl; else if(Play.Player1() = 12) cout Q endl; else if (Play.Player1() = 13) cout K

18、endl; else if (Play.Player1() = 14) cout A endl; else cout Play.Player1() endl; cout 和一张; if(Play.Player2() = 11) cout J endl; else if(Play.Player2() = 12) cout Q endl; else if (Play.Player2() = 13) cout K endl; else if (Play.Player2() = 14) cout A endl; else cout Play.Player2() endl; cout是否继续加注?加注请

19、输入1到5,如不加注请输入0Play.plyrAddBet;if(Play.plyrAddBet5|Play.plyrAddBet0) cout加注错误,重新加注endl; cout是否继续加注?加注请输入1到5,如不加注请输入0Play.plyrAddBet; cout 计算机有一张; if(Play.Computer1() = 11) cout J endl; else if(Play.Computer1() = 12) cout Q endl; else if (Play.Computer1() = 13) cout K endl; else if (Play.Computer1() =

20、 14) cout A endl; else cout Play.Computer1() endl; cout和一张;if(Play.Computer2() = 11) cout J endl; else if(Play.Computer2() = 12) cout Q endl; else if (Play.Computer2() = 13) cout K endl; else if (Play.Computer2() = 14) cout A endl; else cout Play.Computer2() endl; if (Play.Player1()=Play.Player2()&P

21、lay.Computer1()!=Play.Computer2() /比较玩家与计算机的输赢 coutPlay.Computer1() cout你赢了; Play.Increase(); if( Play.Player1()Play.Computer1() cout你输了; Play.Decrease(); cout 势均力敌! Play.Computer1()+Play.Computer2() ) cout你赢了; Play.Increase(); if (Play.Player1()+Play.Player2()Play.Computer1()+Play.Computer2() )cout

22、你输了;Play.Decrease(); cout 势均力敌! Show();/数据的输出CSalary *GetData() return pData;/数据的得到friend class CList;/把链表类设为友元类;CNode:CNode(CNode &node)/重载构造函数pData=node.pData;pNext=node.pNext;void CNode:InputData(CSalary *pSal)/输入数据pData=pSal;pNext=0;链表类:class CList/链表类protected:CNode *pHead;/链表头指针public:CList()

23、pHead=0;/构造函数CList() DeleteList();/析构函数 void AddNode(CNode *pnode);/在首部添加结点void DeleteNode(CNode *);/删除一个指定的结点,返回该节点的指针CNode *LookUp(CSalary&);/查找姓名void ShowList();/打印整个链表void DeleteList();/删除整个链表CNode *GetpHead();/返回链表头指针CNode *GetNextNode(CNode *);/返回链表指定结点的下一个结点,void Insert(CNode *);/按工资顺序插入一个节点C

24、Node *GetLastNode();/返回链表尾节点;封装类:class CRecord/定义一个类,此类封装所有在主函数内调用的函数CList list;public:void ReadFromFile();/读取文件void SaveToFile();/保存记录到文件里void AddRecord();/添加记录void ShowRecord();/显示记录void LookUpRecord();/根据姓名查询void EditRecord();/根据姓名编辑void DeleteRecord();/根据姓名删除void change(char*);/大小写转换;主函数:void ma

25、in()CRecord Record;Record.ReadFromFile();cout* * * * * * * * * * 工资管理系统 * * * * * * * * * *n;cout主菜单:endl;cout1.添加工资记录endl;cout2.显示工资记录endl;cout3.根据姓名查询工资数据endl;cout4.根据姓名删除工资数据endl;cout5.根据姓名修改工资数据endl;cout6.存储文件endl;cout0.退出系统endl;cout* * * * * * * * * * * * * * * * * * * * * * *n;while(1) cout请选择

26、要进行操作的代号:num;switch(num)case 1:Record.AddRecord();break;/添加工资记录case 2:Record.ShowRecord();break;/显示工资记录case 3:Record.LookUpRecord();break;/根据姓名查询工资数据case 4:Record.DeleteRecord();break;/根据姓名删除工资数据case 5:Record.EditRecord();break;/根据姓名修改工资数据case 6:Record.SaveToFile();break;/存储文件 case 0:exit(-1);break;

27、/退出系统default:break;实现改进一:进行了类的封装,将主要的操作函数都封装在了类中程序中,定义了三个类,数据类,结点类,链表类。还定义一个类,把主要操作函数包含其中。具体实现: class CRecord/定义一个类,此类封装所有在主函数内调用的函数CList list;public:void ReadFromFile();/读取文件void SaveToFile();/保存记录到文件里void AddRecord();/添加记录void ShowRecord();/显示记录void LookUpRecord();/根据姓名查询void EditRecord();/根据姓名编辑v

28、oid DeleteRecord();/根据姓名删除void change(char*);/大小写转换;改进二:要求显示、修改、删除数据时大小写通用只要修改大写字母就行,把大写转化成小写字母程序就能实现大小写通用了。具体实现void CRecord:change(char* name)/大小写转换int v=0;while(namev!=0)if (namev=A & namevpNext=0;return;if(pHead-pData-GetSal()pnode-pData-GetSal()pnode-pNext=pHead;pHead=pnode;return;p=pHead;while(

29、p&p-pData-GetSal()pData-GetSal()q=p;p=p-pNext;q-pNext=pnode;pnode-pNext=p;改进四:修改、删除数据前增加提示信息,用户确认后才能进一步操作,否则操作取消。要实现确认操作,真正删除或者删除前增加一个输出输入再一个判断语句而已。具体实现:do p-ShowNode(); coutedit; if(edit=N|edit=n)break; else p-GetData()-EditData(); break; while(edit!=Y&edit!=y&edit!=N&edit!=n);改进五:增加程序的文件输入输出功能,在执行程序中首先将工资数据从文件中读出再进行管理,在程序结束时能将工资数据保存在原文件中。具体实现:void CRecord:ReadFromFile()/读取文件ifstream infile(data.txt);if(!infile)cout文件读取失败!namesalary)/文件数据的读取CSalary *psalary=new CSalary;/psalary-SetSalary(name,salary);/置姓名与工资CNode *pnode=new CNode;/pnode-InputData(psalary);/输入数据

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号