课程设计24点小游戏_第1页
课程设计24点小游戏_第2页
课程设计24点小游戏_第3页
课程设计24点小游戏_第4页
课程设计24点小游戏_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课 程 设 计 报 告 课程名称:面向对象程序设计C+ 设计题目: 24点小游戏 专 业:计算机科学与技术 姓 名: 学 号: 指导教师:李 晓 虹2016 年 1 月 4 日课程设计报告模块内容要求:(1) 系统需求分析:基本简单小游戏通过c+实现(2) 总体设计:1. 系统自动给出4个数字(1-k,其中J,Q,K按一点算)2. 由玩家自主计算,当给出数字不能算出24点时,按任意键继续; 3. 由玩家自行决定是否继续游戏; 4. 在任何情况下都可以按Esc键结束游戏; (3)系统调试:程序实现代码:1. 主函数:2. #include stdio.h3. #include conio.h4. #include stdlib.h5. #include time.h6. #include math.h7. #include string.h8. /*9. 从一副扑克牌中,任取4张。10. 2-10 按其点数计算(为了表示方便10用T表示),J,Q,K,A 统一按 1 计算11. 要求通过加减乘除四则运算得到数字 24。12. 本程序可以随机抽取纸牌,并用试探法求解。13. */14. void GivePuzzle(char* buf)15. 16. int i;17. char card = A,2,3,4,5,6,7,8,9,T,J,Q,K;18. for(i=0;i4;i+)19. 20. bufi=cardrand()%13;21. 22. 23.24. void shuffle(char * buf)25. 26. int i,k;27. char t;28. for(i=0; i=0 & c0.0001)71. return op1 / op2;72. else73. return 100000;74. 75. return 0;76. 77. void MakeAnswer(char* answer, int type, char* question, int* oper)78. 79. int i;80. 81. char p43;82.83. for(i=0; i4; i+)84. 85. if( questioni=T)86. strcpy(pi, 10);87. else88. sprintf(pi, %c, questioni);89. 90. switch(type)91. 92. case 0:93. sprintf(answer, %s %c (%s %c (%s %c %s), 94. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);95. break;96. case 1:97. sprintf(answer, %s %c (%s %c %s) %c %s), 98. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);99. break;100. case 2:101. sprintf(answer, (%s %c %s) %c (%s %c %s), 102. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);103. break;104. case 3:105. sprintf(answer, (%s %c %s) %c %s) %c %s, 106. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);107. break;108. case 4:109. sprintf(answer, (%s %c (%s %c %s) %c %s, 110. p0, GetOper(oper0), p1, GetOper(oper1), p2, GetOper(oper2), p3);111. break;112. 113. 114.115.116. int TestResolve(char* question, int* oper, char* answer)117. 118. / 等待考生完成119. int type = 0;120. double a,b,c;121. double cc = 0.00001;122.123. for(type=0;type5;type+)124. 125. if(type=0)126. 127. c=MyCalcu(double)GetCardValue(question2),(double)GetCardValue(question3),oper2);128. b=MyCalcu(double)GetCardValue(question1),c,oper1);129. a=MyCalcu(double)GetCardValue(question0),b,oper0);130. /printf(n%fn,a);131. if(fabs(a-24.00)cc)132. break;133. else continue;134. 135. else if(type=1)136. 137. b=MyCalcu(double)GetCardValue(question1),(double)GetCardValue(question2),oper1);138. c=MyCalcu(b,(double)GetCardValue(question3),oper2);139. a=MyCalcu(double)GetCardValue(question0),c,oper0);140. /printf(n%fn,a);141. if(fabs(a-24.00)cc)142. break;143. else continue;144. 145. else if(type=2)146. 147. 148. 149. a=MyCalcu(double)GetCardValue(question0),(double)GetCardValue(question1),oper0);150. c=MyCalcu(double)GetCardValue(question2),(double)GetCardValue(question3),oper2);151. b=MyCalcu(a,c,oper1);152. /printf(n%fn,b);153. if(fabs(b-24.00)cc)154. break;155. else continue;156. 157. else if(type=3)158. 159. a=MyCalcu(double)GetCardValue(question0),(double)GetCardValue(question1),oper0);160. b=MyCalcu(a,(double)GetCardValue(question2),oper1);161. c=MyCalcu(b,(double)GetCardValue(question3),oper2);162. /printf(n%fn,c);163. if(fabs(c-24.00)cc)164. break;165. else continue;166. 167. else if(type=4)168. 169. b=MyCalcu(double)GetCardValue(question1),(double)GetCardValue(question2),oper1);170. a=MyCalcu(double)GetCardValue(question0),b,oper0);171. c=MyCalcu(a,(double)GetCardValue(question3),oper2);172. /printf(n%fn,c);173. if(fabs(c-24.00)cc)174. break;175. else continue;176. 177. 178. /*switch(type)179. 180. case 0:181. MakeAnswer(answer,0,question,oper);182. return true;183. /printf(参考:%sn, answer);184. /break;185. case 1:186. MakeAnswer(answer,1,question,oper);187. return true;188. /printf(参考:%sn, answer);189. /break;190. case 2:191. MakeAnswer(answer,2,question,oper);192. return true;193. /printf(参考:%sn, answer);194. /break;195. case 3:196. MakeAnswer(answer,3,question,oper);197. return true;198. /printf(参考:%sn, answer);199. /break;200. case 4:201. MakeAnswer(answer,4,question,oper);202. return true;203. /printf(参考:%sn, answer);204. /break;205. 206. 207.208. return false;*/209.210. if(type5)211. 212. MakeAnswer(answer,type,question,oper);213. return 1;214. 215. else return 0;216. 217.218.219. /*220. 采用随机试探法:就是通过随机数字产生 加减乘除的 组合,通过大量的测试来命中的解法221. 提示:222. 1. 需要考虑用括号控制计算次序的问题 比如:( 10 - 4 ) * ( 3 + A ), 实际上计算次序的数目是有限的:223. A*(B*(c*D)224. A*(B*C)*D)225. (A*B)*(C*D)226. (A*B)*C)*D227. (A*(B*C)*D228. 2. 需要考虑计算结果为分数的情况:( 3 + (3 / 7) ) * 7229. 3. 题目中牌的位置可以任意交换230. */231. int TryResolve(char *question, char *answer)232. 233. /int oper3; / 存储运算符,0:加法 1:减法 2:乘法 3:除法234. int i,j;235. int oper3;236. for(i = 0; i 1000 * 1000; i+)237. 238. / 打乱纸牌顺序239. shuffle(question);240. / 随机产生运算符241. for(j=0; j3; j+)242. operj = rand() % 4;243.244. if( TestResolve(question,oper,answer)245.246. return 1;247. 248.249. return 0;250. 251.252.253. int main(int argc, char* argv)254. 255. char buf_question4; / 题目256. char buf_answer30; / 解答257.258. int i;259. / 初始化随机种子260. srand( (unsigned)time( NULL ) );261. 262. printf(*n);263. printf(速算24游戏n);264. printf(A J Q K 均按1计算,其它按牌点计算n);265. printf(目标是:通过四则运算组合出结果:24n);266. printf(*nn);267. for(;)268. 269. GivePuzzle(buf_question); / 出题270. printf(题目:);271. for(i=0; i4; i+)272. 273. if( buf_questioni = T )/初始化buf1;274. printf(10 );275. else276. printf(%c , buf_questioni);277. 278.279. printf(n按任意键参考答案.n);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论