想找人玩猜拳游戏,没人玩,那就做个简单的人机猜拳系统。
第一步写头文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
下面用到
rand函数要#include <stdlib.h>;
srand函数要#include <time.h>;
sleep函数要#include <unistd.h>;(函数不知道在哪个库的时候没关系,大多数软件编译完会提醒,没提醒就只能自己搜了。
第二步写主函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
//内容
return 0;
}
第三步声明两个整型变量 ch 和 sh
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
int ch, sh;
return 0;
}
分别用于存储用户的选择和电脑的选择。(名字可以根据自己的喜好,我这就ch和sh)
第四步初始化随机数生成器
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int ch, sh;
return 0;
}
使用当前时间作为种子,确保每次运行程序时生成的随机数序列不同。
第五步生成电脑的随机选择
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int ch, sh;
sh = rand() % 3 + 1;
return 0;
}
生成一个0到2之间的随机数,然后加1,使得 sh 的值在1到3之间,分别代表剪刀、石头、布。
第六步打印提示信息,让用户输入他们的选择并读取用户输入的整数值
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int ch, sh;
sh = rand() % 3 + 1;
sleep(1);
printf("请输入选项(1.剪刀 2.石头 3.布):\n");
scanf("%d", &ch);
return 0;
}
将其存储在变量 ch 中。加个延时一秒(也可不加)。
第七步使用 switch 语句根据电脑的选择打印电脑选择的结果。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int ch, sh;
sh = rand() % 3 + 1;
sleep(1);
printf("请输入选项(1.剪刀 2.石头 3.布):\n");
scanf("%d", &ch);
switch(sh)
{
case 1:
printf("电脑选择剪刀\n");
break;
case 2:
printf("电脑选择石头\n");
break;
case 3:
printf("电脑选择布\n");
break;
}
return 0;
}
第八步判断用户和电脑的选择。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int ch, sh;
sh = rand() % 3 + 1;
sleep(1);
printf("请输入选项(1.剪刀 2.石头 3.布):\n");
scanf("%d", &ch);
switch(sh)
{
case 1:
printf("电脑选择剪刀\n");
break;
case 2:
printf("电脑选择石头\n");
break;
case 3:
printf("电脑选择布\n");
break;
}
if(ch == sh)
{
printf("平局\n");
}
else
{
if((ch == 1 && sh == 3) || (ch == 2 && sh == 1) || (ch == 3 && sh == 2))
{
printf("你赢了\n");
}
else
{
printf("你输了\n");
}
}
return 0;
}
-
如果相同,即玩家和电脑选择了相同的选项,则执行if(ch == sh):这个条件判断玩家的选择(ch)是否和电脑的选择(sh)相同。printf("平局\n");输出“平局”。 -
else:如果玩家的选择和电脑的选择不相同,即进入这个分支。if((ch == 1 && sh == 3) || (ch == 2 && sh == 1) || (ch == 3 && sh == 2)):这个条件判断玩家是否赢了。使用了逻辑或(||)来检查三种赢的情况:ch == 3 && sh == 2:玩家选择了布(3),电脑选择了石头(2),布赢石头。ch == 2 && sh == 1:玩家选择了石头(2),电脑选择了剪刀(1),石头赢剪刀。ch == 1 && sh == 3:玩家选择了剪刀(1),电脑选择了布(3),剪刀赢布。- 如果上述任何一个条件为真,即玩家赢了,执行
printf("你赢了\n");输出“你赢了”。 else:如果上述所有赢的条件都不满足,执行printf("你输了\n");输出“你输了”。
第九步加个while循环和退出条件,即完整代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
srand(time(NULL)); // 初始化随机数生成器
while(1)
{
int ch, sh;
sh = rand() % 3 + 1;
sleep(1); // 延时1秒
printf("请输入选项(1.剪刀 2.石头 3.布):\n");
scanf("%d", &ch);
switch(sh)
{
case 1:
printf("电脑选择剪刀\n");
break;
case 2:
printf("电脑选择石头\n");
break;
case 3:
printf("电脑选择布\n");
break;
}
if(ch == sh)
{
printf("平局\n");
}
else
{
if((ch == 1 && sh == 3) || (ch == 2 && sh == 1) || (ch == 3 && sh == 2))
{
printf("你赢了\n");
}
else
{
printf("你输了\n");
}
}
printf("按任意键继续,输入0退出游戏:\n");
scanf("%d", &ch);
if(ch == 0)
break;
}
return 0;
}
再次读取用户输入,如果用户输入0, break 语句会跳出循环,结束游戏。
人生能有几回搏,努力提升自己。
本人(大学生)学习较浅,如有错误请在评论区评论,我虚心接受。

6979

被折叠的 条评论
为什么被折叠?



