C++猜单词游戏

游戏说明

运行须知:

  请将压缩文件中的list.txt englishi.txt people.txt answer.txt放入D盘中的game文件夹中才能正常运行游戏,或者更改文件途径。

 

  输入1可以登录用户,输入2可以游客试玩,用户登录可以查看单词本,里面有本游戏所粗存的所有单词及意思,游客只能游戏,不能查看单词本,如果强行查看只有强行退出游戏重开。

 

  目前可以登录的四个用户是:

账号 密码

2001 2001

2002 2002

2005 2005

2333 4666

(注册功能暂未开启。。。。。。。QAQ)

 

游戏规则:

系统会在储存的单词中随机挑选一个单词并抹去其中一个字母。你需要猜测“*”部位的字母。在生词模式下,你可以将词汇加入生词本。在怀旧模式下,你所猜测的是你加入生词本的词。玩家登陆有福利可以查看词典。

 

代码简介:

有一个people类,配合文件一起进行登录操作,运用了文件存储单词,学习了使用随机数,用了数组储存文件中的内容,有菜单界面函数,游戏函数,登录函数,在代码中有注释。

 

思路:

   刚开始的时候按要求先写出了最基础的,猜单词,然后在猜单词的基础上开始回忆玩过的游戏,开始分游戏的模式,然后开始有用户登录和玩家试玩,看起来更像游戏,为了区分玩家和游客的区别又在玩家方面加了可以看单词本的功能。


 
 
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include<ctime>
  5. #include<conio.h>
  6. using namespace std;
  7. class people
  8. {
  9. public:
  10. int snum;
  11. int spassword;
  12. people * next;
  13. };
  14. people *head1= NULL;
  15. void menu(); //菜单函数
  16. void star(); //链接游戏的函数
  17. void instruction(); //游戏说明
  18. void startgame1(); //生词模式函数
  19. void startgame2(); //怀旧模式函数
  20. void denglu(); //玩家登陆函数
  21. void enter(); //没啥大用的链接其他函数的函数
  22. void danci(); //登录用户查看单词本的函数
  23. int main()
  24. {
  25. cout<< "玩家登陆请输入1,游客试玩请输入2,退出请输入0"<< endl;
  26. int n;
  27. while(n)
  28. {
  29. cout<< "请选择操作"<< endl;
  30. cin>>n;
  31. switch(n)
  32. {
  33. case 1:
  34. denglu();
  35. break;
  36. case 2:
  37. star();
  38. break;
  39. }
  40. }
  41. }
  42. void star()
  43. {
  44. int xuanxiang;
  45. menu(); //显示菜单
  46. while(xuanxiang) //选择操作
  47. {
  48. cout<< "请选择操作"<< endl;
  49. cin>>xuanxiang;
  50. system( "cls");
  51. switch(xuanxiang)
  52. {
  53. case 1:
  54. instruction(); //游戏说明
  55. menu() ;
  56. break;
  57. case 2:
  58. enter(); //游戏开始
  59. menu() ;
  60. break;
  61. }
  62. }
  63. }
  64. void star2()
  65. {
  66. int xuanxiang;
  67. menu(); //显示菜单
  68. while(xuanxiang) //选择操作
  69. {
  70. cout<< "请选择操作"<< endl;
  71. cin>>xuanxiang;
  72. system( "cls");
  73. switch(xuanxiang)
  74. {
  75. case 1:
  76. instruction(); //游戏说明
  77. menu() ;
  78. break;
  79. case 2:
  80. enter(); //游戏开始
  81. menu() ;
  82. break;
  83. case 3:
  84. danci();
  85. menu() ;
  86. break;
  87. }
  88. }
  89. }
  90. void denglu()
  91. {
  92. int m,n;
  93. int snum,spassword;
  94. ifstream read("d:\\game\\people.txt",ios::in);
  95. read.seekg( 0,ios::beg);
  96. cout<< "请输入账号"<< endl;
  97. cin>>m;
  98. cout<< "请输入密码"<< endl;
  99. cin>>n;
  100. while(read>>snum>>spassword)
  101. {
  102. if(snum==m&&spassword==n)
  103. star2();
  104. }
  105. read.close ();
  106. }
  107. void menu()
  108. {
  109. cout<< "***************欢迎来到猜词游戏**************"<< endl;
  110. cout<< endl;
  111. cout<< endl;
  112. cout<< "*****************请选择操作******************"<< endl;
  113. cout<< endl;
  114. cout<< "*****************0.退出游戏******************"<< endl;
  115. cout<< endl;
  116. cout<< "*****************1.游戏规则******************"<< endl;
  117. cout<< endl;
  118. cout<< "*****************2.进入游戏******************"<< endl;
  119. cout<< endl;
  120. cout<< "*****************3.查看词典(仅登陆用户使用)"<< endl;
  121. cout<< endl;
  122. }
  123. void instruction()//等到全部做完再写游戏说明
  124. {
  125. cout<< "游戏规则:"<< endl;
  126. cout<< "系统会在储存的单词中随机挑选一个单词并抹去其中一个字母。"<< endl;
  127. cout<< "你需要猜测“*”部位的字母。"<< endl;
  128. cout<< "在生词模式下,你可以将词汇加入生词本。"<< endl;
  129. cout<< "在怀旧模式下,你可以练习曾经错过的词。"<< endl;
  130. cout<< "玩家登陆有福利可以查看词典哦。"<< endl;
  131. }
  132. void enter()
  133. {
  134. cout<< "*****************1.生词模式******************"<< endl;
  135. cout<< endl;
  136. cout<< "*****************2.怀旧模式******************"<< endl;
  137. cout<< endl;
  138. cout<< "*****************0.退出游戏******************"<< endl;
  139. cout<< endl;
  140. int n;
  141. while(n)
  142. {
  143. cout<< "请选择模式,0退出"<< endl;
  144. cin>>n;
  145. system( "cls");
  146. switch(n)
  147. {
  148. case 1:
  149. startgame1();
  150. break;
  151. case 2:
  152. startgame2();
  153. break;
  154. }
  155. }
  156. }
  157. void startgame1()
  158. {
  159. cout<< "game star~~~~~~~~~~~你,准备好迎接挑战了吗?"<< endl;
  160. srand(( unsigned)time( 0));
  161. ifstream in;
  162. int ijudge;
  163. int iline= 0,irand1,irand2,irand3,isize;
  164. int i= 0;
  165. char s[ 100][ 100],*sline,s1,s2;
  166. //打开文件并将数据存到数组中
  167. in.open( "d:\\game\\answer.txt",ios::in);
  168. if(in)
  169. {
  170. while(!in.eof())
  171. {
  172. in>>s[iline];
  173. iline++;
  174. if(iline>= 100)
  175. break;
  176. }
  177. }
  178. else
  179. cout<< "file not found!"<< endl;
  180. //随机选一个单词
  181. irand1=rand()%iline; //随机选一个单词
  182. isize= strlen(s[irand1]); //得到该单词长度
  183. srand( int(time( 0)));
  184. irand2=rand()%isize; //随机选取某个字母位
  185. sline= new char[irand2];
  186. strcpy(sline,s[irand1]);
  187. for( i= 0;i<=isize;i++)
  188. {
  189. if(i==irand2)
  190. {
  191. s1=s[irand1][irand2];
  192. s[irand1][irand2]= '*';
  193. }
  194. }
  195. //开始游戏
  196. while( 1)
  197. {
  198. if(i== 0)
  199. {
  200. i++;
  201. }
  202. cout<< endl;
  203. cout<<s[irand1]<< endl;
  204. cout<< endl;
  205. cin>>s2;
  206. if(s1==s2)
  207. {
  208. cout<< "答对啦~~~~"<< endl;
  209. s[irand1][irand2]=s1;
  210. }
  211. else
  212. {
  213. cout<< "答错了,要加油哦QAQ"<< endl;
  214. s[irand1][irand2]=s1;
  215. }
  216. cout<< "******是否将该单词加入生词本******"<< endl;
  217. cout<< "*********1-是,2-否***************"<< endl;
  218. cin>>ijudge;
  219. if(ijudge== 1)
  220. {
  221. ofstream input("d:\\game\\list.txt",ios::app);
  222. input<<s[irand1]<< endl;
  223. input.close();
  224. }
  225. cout<< "是否继续?1--是,2--否"<< endl;
  226. cin>>ijudge;
  227. if(ijudge== 2)
  228. break;
  229. else
  230. {
  231. srand( int(time( 0)));
  232. irand1=rand()%iline; //随机选一个单词
  233. isize= strlen(s[irand1]); //得到该单词长度
  234. srand( int(time( 0)));
  235. irand2=rand()%isize; //随机选取某个字母位
  236. sline= new char[irand2];
  237. strcpy(sline,s[irand1]);
  238. for( int i= 0;i<=isize;i++)
  239. {
  240. if(i==irand2)
  241. {
  242. s1=s[irand1][irand2];
  243. s[irand1][irand2]= '*';
  244. }
  245. }
  246. }
  247. }
  248. in.close();
  249. }
  250. void startgame2()
  251. {
  252. cout<< "game star,温习巩固记忆更强"<< endl;
  253. srand(( unsigned)time( 0));
  254. ifstream in;
  255. int ijudge;
  256. int iline= 0,irand1,irand2,irand3,isize;
  257. int i= 0;
  258. char s[ 100][ 100],*sline,s1,s2;
  259. //打开文件并将数据存到数组中
  260. in.open( "d:\\game\\list.txt",ios::in);
  261. if(in)
  262. {
  263. while(!in.eof())
  264. {
  265. in>>s[iline];
  266. iline++;
  267. if(iline>= 100)
  268. break;
  269. }
  270. }
  271. else
  272. cout<< "file not found!"<< endl;
  273. //随机选一个单词
  274. irand1=rand()%iline; //随机选一个单词
  275. isize= strlen(s[irand1]); //得到该单词长度
  276. srand( int(time( 0)));
  277. irand2=rand()%isize; //随机选取某个字母位
  278. sline= new char[irand2];
  279. strcpy(sline,s[irand1]);
  280. for( i= 0;i<=isize;i++)
  281. {
  282. if(i==irand2)
  283. {
  284. s1=s[irand1][irand2];
  285. s[irand1][irand2]= '*';
  286. }
  287. }
  288. //开始游戏
  289. while( 1)
  290. {
  291. if(i== 0)
  292. {
  293. i++;
  294. }
  295. cout<< endl;
  296. cout<<s[irand1]<< endl;
  297. cout<< endl;
  298. cin>>s2;
  299. if(s1==s2)
  300. {
  301. cout<< "答对啦~~~~"<< endl;
  302. s[irand1][irand2]=s1;
  303. }
  304. else
  305. {
  306. cout<< "答错了,要加油哦QAQ"<< endl;
  307. s[irand1][irand2]=s1;
  308. }
  309. cout<< "是否继续?1--是,2--否"<< endl;
  310. cin>>ijudge;
  311. if(ijudge== 2)
  312. break;
  313. else
  314. {
  315. srand( int(time( 0)));
  316. irand1=rand()%iline; //随机选一个单词
  317. isize= strlen(s[irand1]); //得到该单词长度
  318. srand( int(time( 0)));
  319. irand2=rand()%isize; //随机选取某个字母位
  320. sline= new char[irand2];
  321. strcpy(sline,s[irand1]);
  322. for( int i= 0;i<=isize;i++)
  323. {
  324. if(i==irand2)
  325. {
  326. s1=s[irand1][irand2];
  327. s[irand1][irand2]= '*';
  328. }
  329. }
  330. }
  331. }
  332. in.close();
  333. }
  334. void danci()
  335. {
  336. char danci[ 100],zhongwen[ 100];
  337. ifstream instuf("d:\\game\\englishi.txt",ios::in);
  338. instuf.seekg( 0,ios::beg);
  339. if(!instuf)
  340. {
  341. cout<< "file could not be open."<< endl;
  342. abort();
  343. }
  344. while(instuf>>danci>>zhongwen)
  345. { cout<<danci<< " "<<zhongwen<< endl;}
  346. instuf.close();
  347. }


 
 
请自己选择路径添加相应的txt文件
 
 


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值