帮我编个猜拳小游戏的代码
时间: 2023-03-29 11:04:21 浏览: 99
好的,以下是一个简单的猜拳小游戏的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int player, computer;
int win = , lose = , draw = ;
srand(time(NULL)); // 初始化随机数种子
printf("欢迎来到猜拳游戏!\n");
printf("请出拳:1-石头,2-剪刀,3-布,-结束游戏\n");
do {
printf("你的选择:");
scanf("%d", &player);
if (player == ) {
break;
}
if (player < 1 || player > 3) {
printf("无效的选择,请重新输入!\n");
continue;
}
computer = rand() % 3 + 1; // 生成电脑的选择
printf("电脑的选择:%d\n", computer);
if (player == computer) {
printf("平局!\n");
draw++;
} else if ((player == 1 && computer == 2) ||
(player == 2 && computer == 3) ||
(player == 3 && computer == 1)) {
printf("你赢了!\n");
win++;
} else {
printf("你输了!\n");
lose++;
}
printf("当前得分:胜%d,负%d,平%d\n", win, lose, draw);
} while (1);
printf("游戏结束,谢谢参与!\n");
return ;
}
阅读全文