c语言项目实战猜拳游戏
时间: 2024-02-02 14:09:25 浏览: 166
很抱歉,根据提供的引用内容,没有找到关于C语言实现猜拳游戏的具体示例代码或详细介绍。但是,我可以为您提供一个简单的C语言猜拳游戏的示例代码,供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int playerChoice, computerChoice;
int playerScore = 0, computerScore = 0;
int round = 1;
srand(time(0)); // 设置随机数种子
printf("欢迎来到猜拳游戏!\n");
while (round <= 3) {
printf("\n第 %d 轮\n", round);
printf("请选择:\n");
printf("1. 石头\n");
printf("2. 剪刀\n");
printf("3. 布\n");
printf("请输入您的选择(1-3):");
scanf("%d", &playerChoice);
if (playerChoice < 1 || playerChoice > 3) {
printf("无效的选择,请重新选择!\n");
continue;
}
computerChoice = rand() % 3 + 1;
printf("您选择了:");
switch (playerChoice) {
case 1:
printf("石头\n");
break;
case 2:
printf("剪刀\n");
break;
case 3:
printf("布\n");
break;
}
printf("电脑选择了:");
switch (computerChoice) {
case 1:
printf("石头\n");
break;
case 2:
printf("剪刀\n");
break;
case 3:
printf("布\n");
break;
}
if (playerChoice == computerChoice) {
printf("本轮平局!\n");
} else if ((playerChoice == 1 && computerChoice == 2) ||
(playerChoice == 2 && computerChoice == 3) ||
(playerChoice == 3 && computerChoice == 1)) {
printf("您赢了本轮!\n");
playerScore++;
} else {
printf("电脑赢了本轮!\n");
computerScore++;
}
round++;
}
printf("\n游戏结束!\n");
printf("您的得分:%d\n", playerScore);
printf("电脑的得分:%d\n", computerScore);
if (playerScore > computerScore) {
printf("恭喜您获胜!\n");
} else if (playerScore < computerScore) {
printf("很遗憾,电脑获胜!\n");
} else {
printf("本局平局!\n");
}
return 0;
}
```
阅读全文