C语言-----石头剪刀布小游戏控制胜率
时间: 2023-12-31 15:24:54 浏览: 207
以下是一个C语言实现的石头剪刀布小游戏,可以控制胜率的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int play, computer, winCount = 0, loseCount = 0, drawCount = 0;
char choice;
srand(time(NULL));
do {
printf("请选择:\n");
printf("1. 石头\n");
printf("2. 剪刀\n");
printf("3. 布\n");
printf("0. 退出\n");
scanf("%d", &play);
if (play == 0) {
break;
}
computer = rand() % 3 + 1;
printf("你选择了:");
switch (play) {
case 1:
printf("石头\n");
break;
case 2:
printf("剪刀\n");
break;
case 3:
printf("布\n");
break;
default:
printf("无效选择\n");
continue;
}
printf("电脑选择了:");
switch (computer) {
case 1:
printf("石头\n");
break;
case 2:
printf("剪刀\n");
break;
case 3:
printf("布\n");
break;
}
if (play == computer) {
printf("平局!\n");
drawCount++;
} else if ((play == 1 && computer == 2) || (play == 2 && computer == 3) || (play == 3 && computer == 1)) {
printf("你赢了!\n");
winCount++;
} else {
printf("你输了!\n");
loseCount++;
}
printf("胜利次数:%d,失败次数:%d,平局次数:%d\n", winCount, loseCount, drawCount);
printf("是否继续游戏?(y/n) ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
return 0;
}
```
这个程序使用了随机数生成电脑的选择,然后根据玩家和电脑的选择进行比较,判断胜负或平局,并统计胜利、失败和平局的次数。你可以通过多次运行程序来控制胜率。
阅读全文