|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
就是我写了一个猜拳游戏 0 代表 拳头 1 代表 剪刀 2 代表 布
代码如下
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
int player, computer, n, result, win=0, lose=0, draw=0, T;
n = 1;
system("cls");
printf("let's play the finger-guessing game!\n");
srand(time(0));
while (n)
{
printf("input 0 mesns fist, 1 means shears and 2 means papper.\n");
scanf_s("%d", &player);
fflush(stdin);
computer = rand() % 3;
T = rand();
printf("the rand is %d and the computer is %d", T, computer);
system("pause");
result = player - computer;
if (result == 0)
{
printf("平局!\n");
Sleep(1000);
system("cls");
draw++;
}
else if (result == 2)
{
printf("winner is player!\n");
Sleep(1000);
system("cls");
win++;
}
else if (result == 1)
{
printf("输了!\n");
Sleep(1000);
system("cls");
lose++;
}
else if (result == -1)
{
printf("输了!\n");
Sleep(1000);
system("cls");
lose++;
}
else if (result == -2)
{
printf("winner is player!\n");
Sleep(1000);
system("cls");
win++;
}
else
{
printf("illegal argument!!!\n");
Sleep(1000);
system("cls");
}
printf("input 0 to leave and others play again\n");
scanf_s("%d", &n);
fflush(stdin);
}
printf("the result is The Player win %d times!\nThe Computer wins %d times!\nAnd ends in the draw %d times\n",win, lose, draw);
system("pause");
return 0;
}
统计结果是这样的
0 1 2
0 平 赢 输
3 8 9
1 输 平 赢
13 10 0
27 12 0
2 赢 输 平
3 6 9
输入1的时候一次没赢过....
本帖最后由 superbe 于 2019-10-13 22:51 编辑
代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结果是0,1,2,没问题。
|
|