关于简单猜拳游戏的实现
#include <stdio.h>
#include <stdlib.h>
void change(int key)
{
if(key == 0)
printf("石头\n");
else if(key == 1)
printf("剪刀\n");
else
printf("布\n");
}
int main() {
int key;
int i;
int key1;
char key2;
for(i = 1; ; i++)
{
printf("请输入您要猜的拳:\n0 石头\n1 剪刀\n2 布\n");
scanf("%d",&key);
printf("您选择了");
change(key);
int computer =arc4random_uniform(3);
printf("电脑扔到了");
change(computer);
if (key != 0 && key != 1 && key != 2)
{
printf("请重新输入:\n");
continue;
}
else if (key == computer)
printf("平局!");
else if(key == computer + 1 || key == computer-2)
printf("电脑赢了");
else
printf("你赢了!");
printf("继续请按任意键,结束请按9:\n");
while (getchar()!='\n'); //关于按除了9以外任意键继续 总是无法接受回车;
scanf("%c",&key2);
if (key2 == '9')
return 0;
}
return 0;
}
|
|