- UID
- 2233
- 精华
- 积分
- 40
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
本帖最后由 龙魂 于 2017-2-10 17:19 编辑
大家好,我是新人龙魂,才学C语言,自己花了1天写的小游戏<珠玑妙算>
经0xAA55推荐,来这里和大家分享我的程序和源码
由于使用了system();所以杀毒软件可能会报毒
游戏和源码下载地址:点击下载
源码如下,注释的很清楚,希望能帮到一些人,当然也请各位大神多多指教
(源码已更新为1.3)
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <windows.h>
- //函数菜单
- void menu ()
- {
- //修改控制台标题
- SetConsoleTitle("珠玑妙算 (建议使用窗口最大化游玩) 0.龙魂");
- printf ("\t\t\t┏━━━━━━━━┓\n");
- printf ("\t\t\t┃ 珠玑妙算 ┃\n");
- printf ("\t\t\t┗━━━━━━━━┛\n");
- printf ("\t\t\t\t ---by龙魂\n\n");
- printf ("\t\t\t\t1.单人游戏\n\n");
- printf ("\t\t\t\t2.双人游戏\n\n");
- printf ("\t\t\t\t3.规则\n\n");
- printf ("\t\t\t\t4.关于\n\n");
- printf ("\t\t\t\t5.退出\n\n");
- }
- main ()
- {
- //变量:双人游戏玩家A设置答案
- int num;
- //变量:电脑生成(双人模式下玩家设定)的随机数
- int number[4];
- //变量:玩家输入的整体答案
- int ans;
- //变量:玩家输入的答案
- int answer[4];
- //变量:菜单选择
- int choise;
- //变量:防止玩家输入非数字字符
- int illegal;
- //变量:循环
- int ia,ib,ic;
- //变量:防止反复遍历
- int count1[4] = {0,0,0,0},count2[4] = {0,0,0,0};
- //变量:提示
- int hint1 = 0,hint2 = 0;
- //打印菜单
- menu ();
- //以时间作为随机数种子
- srand (time(0));
- //游戏主体
- for (;;)
- {
- //防止玩家输错
- do
- {
- printf ("请选择:");
- //清除缓冲区
- fflush(stdin);
- illegal = scanf ("%d",&choise);
- printf ("\n");
- if (choise < 0 || choise > 5 || illegal == 0)
- {
- printf ("选择错误,请重新选择!\n\n");
- }
- }while (choise < 0 || choise > 5 || illegal == 0);
- //彩蛋
- if (choise == 0)
- {
- //清屏
- system ("cls");
- //修改控制台标题
- SetConsoleTitle("珠玑妙算 ---by龙魂");
- printf ("\t\t\t┏━━━━━━┓\n");
- printf ("\t\t\t┃ 彩蛋 ┃\n");
- printf ("\t\t\t┗━━━━━━┛\n");
- printf ("111111111111111111111111111111111111111\n");
- printf ("11111111 1111111111111111111 11111111\n");
- printf ("11111111 1 1111111111111111 11111111\n");
- printf ("11111111 1 111111111111111 111 1111\n");
- printf ("11111111 11 1111111 1 1111\n");
- printf ("11111111 111 1111111 1111\n");
- printf ("11111111 111 1111111111 11 1 1111\n");
- printf ("11 111111 1111\n");
- printf ("11 1 11111111 1111\n");
- printf ("1111111 1 11 11111 1 11 1 1 1111\n");
- printf ("1111111 1 1 111111 111 1111\n");
- printf ("1111111 1 111111 1 1111\n");
- printf ("111111 11 1111111 1 1 1 1111\n");
- printf ("111111 11 1111111 1 1 1111\n");
- printf ("11111 1 11111 11 1 1 1 11\n");
- printf ("1111 111111 111111111 11 11\n");
- printf ("1111 11 11111 11111111 111 1111 1\n");
- printf ("111 11111 1111 111111 111 11 1\n");
- printf ("11 111111 11111 11111 1\n");
- printf ("1 11111111111111111111 11111111111111\n");
- printf ("111111111111111111111111111111111111111\n");
- //暂停屏幕
- system ("pause");
- //清屏
- system ("cls");
- //打印菜单
- menu ();
- }
- //单人游戏
- if (choise == 1)
- {
- //清屏
- system ("cls");
- //修改控制台标题
- SetConsoleTitle("珠玑妙算 ---by龙魂");
- //随机生成4个1-8的数字组成答案
- number[0] = rand () % 7 + 1;
- number[1] = rand () % 7 + 1;
- number[2] = rand () % 7 + 1;
- number[3] = rand () % 7 + 1;
- printf ("\t\t\t┏━━━━━━┓\n");
- printf ("\t\t\t┃ 单人游戏 ┃\n");
- printf ("\t\t\t┗━━━━━━┛\n");
- //循环10次,玩家的10次机会
- for (ia = 1;ia <= 10;ia++)
- {
- //防止玩家输错
- do
- {
- //防止玩家输入的数字不够4位
- do
- {
- printf ("请输入第%d答案:\n\n",ia);
- //清除缓冲区
- fflush(stdin);
- illegal = scanf ("%d",&ans);
- if (ans < 1111 || ans > 8888 || illegal == 0)
- {
- printf ("\n");
- printf ("你输入的答案有误,请重新输入\n\n");
- }
- }while (ans < 1111 || ans > 8888 || illegal == 0);
- //将答案分解为4个单独数字
- answer[0] = ans / 1000;
- answer[1] = ans / 100 % 10;
- answer[2] = ans / 10 % 10;
- answer[3] = ans % 10;
- //玩家输入错误提示
- if (answer[0] < 1 || answer[0] > 8)
- {
- printf ("你输入的第一个数字有误,请重新输入本次答案\n\n");
- }
- if (answer[1] < 1 || answer[1] > 8)
- {
- printf ("你输入的第二个数字有误,请重新输入本次答案\n\n");
- }
- if (answer[2] < 1 || answer[2] > 8)
- {
- printf ("你输入的第三个数字有误,请重新输入本次答案\n\n");
- }
- if (answer[3] < 1 || answer[3] > 8)
- {
- printf ("你输入的第四个数字有误,请重新输入本次答案\n\n");
- }
- }while (answer[0] < 1 || answer[0] > 8 || answer[1] < 1 || answer[1] > 8 || answer[2] < 1 || answer[2] > 8 || answer[3] < 1 || answer[3] > 8);
- printf ("\n");
- //判断答案
- //判断提示(1)的个数
- for (ib = 0;ib <= 3;ib++)
- {
- if (answer[ib] == number[ib])
- {
- //控制反复遍历
- count1[ib]++;
- count2[ib]++;
- //提示(1)加1
- hint1++;
- }
- }
- //判断提示(2)的个数
- for (ib = 0;ib <= 3;ib++)
- {
- for (ic = 0;ic <= 3;ic++)
- {
- if (ib != ic && answer[ib] == number[ic] && count1[ib] == 0 && count2[ic] == 0)
- {
- //控制反复遍历
- count1[ib]++;
- count2[ic]++;
- //提示(2)加1
- hint2++;
- }
- }
- }
- //打印提示(1)
- for (ib = 1;ib <= 4;ib++)
- {
- if (ib <= hint1)
- {
- printf ("●");
- }
- }
- //打印提示(2)
- for (ib = 1;ib <= 4;ib++)
- {
- if (ib <= hint2)
- {
- printf ("○");
- }
- }
- printf ("\n\n");
- //成功
- if (hint1 == 4)
- {
- printf ("恭喜使用了%d次机会获得成功\n\n",ia);
- //暂停屏幕
- system ("pause");
- //清屏
- system ("cls");
- //打印菜单
- menu ();
- //跳出单人游戏循环
- hint1 = 0;
- hint2 = 0;
- for (ib = 0;ib <= 3;ib++)
- {
- count1[ib] = 0;
- count2[ib] = 0;
- }
- break;
- }
- //失败
- if (ia == 10 && hint1 != 4)
- {
- printf ("你的机会用完喽,胜败乃兵家常事,请重新来过吧\n\n");
- printf ("谜底:%d %d %d %d\n\n",number[0],number[1],number[2],number[3]);
- //暂停屏幕
- system ("pause");
- //清屏
- system ("cls");
- //打印菜单
- menu ();
- //跳出单人游戏循环
- hint1 = 0;
- hint2 = 0;
- for (ib = 0;ib <= 3;ib++)
- {
- count1[ib] = 0;
- count2[ib] = 0;
- }
- break;
-
- }
- //变量恢复初始值
- hint1 = 0;
- hint2 = 0;
- for (ib = 0;ib <= 3;ib++)
- {
- count1[ib] = 0;
- count2[ib] = 0;
- }
- }
- }
- //双人游戏
- if (choise == 2)
- {
- //清屏
- system ("cls");
- //修改控制台标题
- SetConsoleTitle("珠玑妙算 ---by龙魂");
- printf ("\t\t\t┏━━━━━━┓\n");
- printf ("\t\t\t┃ 双人游戏 ┃\n");
- printf ("\t\t\t┗━━━━━━┛\n");
- printf ("请玩家A先蒙上玩家B的眼睛(滑稽)\n\n");
- printf ("请玩家A输入四个由1-8的数字作为的谜底(数字可重复):\n\n");
- //防止玩家输错
- do
- {
- //防止玩家输入的数字不够4位
- do
- {
- //清除缓冲区
- fflush(stdin);
- illegal = scanf ("%d",&num);
- if (num < 1111 || num > 8888 || illegal == 0)
- {
- printf ("\n");
- printf ("你输入的答案有误,请重新输入\n\n");
- }
- }while (num < 1111 || num > 8888 || illegal == 0);
- //将答案分解为4个单独数字
- number[0] = num / 1000;
- number[1] = num / 100 % 10;
- number[2] = num / 10 % 10;
- number[3] = num % 10;
- //玩家输入错误提示
- if (number[0] < 1 || number[0] > 8)
- {
- printf ("你输入的第一个数字有误,请重新输入本次答案\n\n");
- }
- if (number[1] < 1 || number[1] > 8)
- {
- printf ("你输入的第二个数字有误,请重新输入本次答案\n\n");
- }
- if (number[2] < 1 || number[2] > 8)
- {
- printf ("你输入的第三个数字有误,请重新输入本次答案\n\n");
- }
- if (number[3] < 1 || number[3] > 8)
- {
- printf ("你输入的第四个数字有误,请重新输入本次答案\n\n");
- }
- }while (number[0] < 1 || number[0] > 8 || number[1] < 1 || number[1] > 8 || number[2] < 1 || number[2] > 8 || number[3] < 1 || number[3] > 8);
- printf ("\n");
- printf ("玩家A你输入的数字是%d %d %d %d\n\n",number[0],number[1],number[2],number[3]);
- printf ("请玩家A按下任意键后摘下玩家B头上的胖次,让他开始解题\n\n");
- //屏幕暂停
- system ("pause");
- //清屏
- system ("cls");
- printf ("\t\t\t┏━━━━━━┓\n");
- printf ("\t\t\t┃ 双人游戏 ┃\n");
- printf ("\t\t\t┗━━━━━━┛\n");
- //循环10次,玩家的10次机会
- for (ia = 1;ia <= 10;ia++)
- {
- //防止玩家输错
- do
- {
- //防止玩家输入的数字不够4位
- do
- {
- printf ("请输入第%d答案:\n\n",ia);
- //清除缓冲区
- fflush(stdin);
- illegal = scanf ("%d",&ans);
- if (ans < 1111 || ans > 8888 || illegal == 0)
- {
- printf ("\n");
- printf ("你输入的答案有误,请重新输入\n\n");
- }
- }while (ans < 1111 || ans > 8888 || illegal == 0);
- //将答案分解为4个单独数字
- answer[0] = ans / 1000;
- answer[1] = ans / 100 % 10;
- answer[2] = ans / 10 % 10;
- answer[3] = ans % 10;
- //玩家输入错误提示
- if (answer[0] < 1 || answer[0] > 8)
- {
- printf ("你输入的第一个数字有误,请重新输入本次答案\n\n");
- }
- if (answer[1] < 1 || answer[1] > 8)
- {
- printf ("你输入的第二个数字有误,请重新输入本次答案\n\n");
- }
- if (answer[2] < 1 || answer[2] > 8)
- {
- printf ("你输入的第三个数字有误,请重新输入本次答案\n\n");
- }
- if (answer[3] < 1 || answer[3] > 8)
- {
- printf ("你输入的第四个数字有误,请重新输入本次答案\n\n");
- }
- }while (answer[0] < 1 || answer[0] > 8 || answer[1] < 1 || answer[1] > 8 || answer[2] < 1 || answer[2] > 8 || answer[3] < 1 || answer[3] > 8);
- printf ("\n");
- //判断答案
- //判断提示(1)的个数
- for (ib = 0;ib <= 3;ib++)
- {
- if (answer[ib] == number[ib])
- {
- //控制反复遍历
- count1[ib]++;
- count2[ib]++;
- //提示(1)加1
- hint1++;
- }
- }
- //判断提示(2)的个数
- for (ib = 0;ib <= 3;ib++)
- {
- for (ic = 0;ic <= 3;ic++)
- {
- if (ib != ic && answer[ib] == number[ic] && count1[ib] == 0 && count2[ic] == 0)
- {
- //控制反复遍历
- count1[ib]++;
- count2[ic]++;
- //提示(2)加1
- hint2++;
- }
- }
- }
- //打印提示(1)
- for (ib = 1;ib <= 4;ib++)
- {
- if (ib <= hint1)
- {
- printf ("●");
- }
- }
- //打印提示(2)
- for (ib = 1;ib <= 4;ib++)
- {
- if (ib <= hint2)
- {
- printf ("○");
- }
- }
- printf ("\n\n");
- //成功
- if (hint1 == 4)
- {
- printf ("恭喜使用了%d次机会获得成功\n\n",ia);
- //暂停屏幕
- system ("pause");
- //清屏
- system ("cls");
- //打印菜单
- menu ();
- //跳出双人游戏循环
- //变量恢复初始值
- hint1 = 0;
- hint2 = 0;
- for (ib = 0;ib <= 3;ib++)
- {
- count1[ib] = 0;
- count2[ib] = 0;
- }
- break;
- }
- //失败
- if (ia == 10 && hint1 != 4)
- {
- printf ("你的机会用完喽,胜败乃兵家常事,请重新来过吧\n\n");
- printf ("谜底:%d %d %d %d\n\n",number[0],number[1],number[2],number[3]);
- //暂停屏幕
- system ("pause");
- //清屏
- system ("cls");
- //打印菜单
- menu ();
- //跳出双人游戏循环
- //变量恢复初始值
- hint1 = 0;
- hint2 = 0;
- for (ib = 0;ib <= 3;ib++)
- {
- count1[ib] = 0;
- count2[ib] = 0;
- }
- break;
- }
- //变量恢复初始值
- hint1 = 0;
- hint2 = 0;
- for (ib = 0;ib <= 3;ib++)
- {
- count1[ib] = 0;
- count2[ib] = 0;
- }
- }
- }
- //规则
- if (choise == 3)
- {
- //清屏
- system ("cls");
- //修改控制台标题
- SetConsoleTitle("珠玑妙算 ---by龙魂");
- printf ("\t\t\t┏━━━━━━┓\n");
- printf ("\t\t\t┃ 游戏规则 ┃\n");
- printf ("\t\t\t┗━━━━━━┛\n");
- printf ("\t介绍\n\n");
- printf ("\tMastermind(珠玑妙算)是一种可供两名玩家使用的密码破译棋盘游戏\n");
- printf ("\t在1970年由Mordecai Meirowitz发明,他是一位以色列邮政和电信专家\n");
- printf ("\t但游戏类似早期一种利用铅笔和纸进行的游戏,游戏名为“公牛和母牛”\n\n");
- printf ("\t一、单人游戏规则\n\n");
- printf ("\t1.游戏开始后电脑会随机生成四个由1-8的数字作为谜底(数字可重复)\n\n");
- printf ("\t2.玩家有10次机会,每次机会都可以输入四个由1-8数字组成答案,电脑会\n");
- printf ("\t 根据玩家输入的答案进行提示,玩家通过提示推理出谜底\n\n");
- printf ("\t3.提示分为●和○两种\n");
- printf ("\t(1).●表示玩家的答案中有一个数字和位置都正确\n");
- printf ("\t(2).○表示玩家的答案中有一个数字正确,位置不正确\n\n");
- printf ("\t如:●●○\n");
- printf ("\t表示输入的答案中有两个数字和位置都正确,有一个数字正确,位置不正确\n");
- printf ("\t,还有一个数字错误\n\n");
- printf ("\t二、双人游戏规则\n\n");
- printf ("\t1.双人游戏谜底由两名玩家中其中一名玩家设置,另一名玩家解题\n");
- printf ("\t2.其他规则和单人游戏相同\n\n");
- printf ("\t<此程序有个小彩蛋哦>\n\n");
- //屏幕暂停
- system ("pause");
- //清屏
- system ("cls");
- //打印菜单
- menu ();
- }
- //关于
- if (choise == 4)
- {
- //清屏
- system ("cls");
- //修改防止台标题
- SetConsoleTitle("珠玑妙算 ---by龙魂");
- printf ("\t\t\t┏━━━━━━┓\n");
- printf ("\t\t\t┃ 关于 ┃\n");
- printf ("\t\t\t┗━━━━━━┛\n");
- printf ("\t\t\t作者:龙魂\n\n");
- printf ("\t\t\tQQ:1017611703\n\n");
- printf ("\t\t\t新浪微博:@Minecraft-龙魂\n");
- //暂停屏幕
- system ("pause");
- //清屏
- system ("cls");
- //打印菜单
- menu ();
- }
- //退出
- if (choise == 5)
- {
- //清屏
- system ("cls");
- //跳出循环
- break;
- }
- }
- //退出语
- printf ("\t\t\t┏━━━━━━━━━━━━━┓\n");
- printf ("\t\t\t┃感谢你的使用,祝你生活愉快┃\n");
- printf ("\t\t\t┗━━━━━━━━━━━━━┛\n");
- //暂停屏幕
- system ("pause");
- }
复制代码 |
|