石头剪刀布游戏c语言代码五局三胜
时间: 2023-08-26 07:07:13 浏览: 190
以下是石头剪刀布游戏的C语言代码,五局三胜:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int playerScore = 0, computerScore = 0;
int playerChoice, computerChoice;
int round = 1;
srand(time(NULL));
printf("欢迎来到石头剪刀布游戏!\n");
printf("游戏规则:玩家和电脑分别出石头、剪刀、布,进行猜拳比赛,先赢三局的人获胜。\n");
while (playerScore < 3 && computerScore < 3) {
printf("\n第%d局:\n", round);
printf("请输入您的选择(1-石头,2-剪刀,3-布):");
scanf("%d", &playerChoice);
if (playerChoice < 1 || playerChoice > 3) {
printf("输入无效,请重新输入!\n");
continue;
}
computerChoice = rand() % 3 + 1;
switch (playerChoice) {
case 1:
printf("您出了石头。");
switch (computerChoice) {
case 1:
printf("电脑出了石头。平局!\n");
break;
case 2:
printf("电脑出了剪刀。您赢了这局!\n");
playerScore++;
break;
case 3:
printf("电脑出了布。电脑赢了这局!\n");
computerScore++;
break;
}
break;
case 2:
printf("您出了剪刀。");
switch (computerChoice) {
case 1:
printf("电脑出了石头。电脑赢了这局!\n");
computerScore++;
break;
case 2:
printf("电脑出了剪刀。平局!\n");
break;
case 3:
printf("电脑出了布。您赢了这局!\n");
playerScore++;
break;
}
break;
case 3:
printf("您出了布。");
switch (computerChoice) {
case 1:
printf("电脑出了石头。您赢了这局!\n");
playerScore++;
break;
case 2:
printf("电脑出了剪刀。电脑赢了这局!\n");
computerScore++;
break;
case 3:
printf("电脑出了布。平局!\n");
break;
}
break;
}
printf("目前比分为 玩家:%d 电脑:%d\n", playerScore, computerScore);
round++;
}
if (playerScore > computerScore) {
printf("\n恭喜您!您赢得了比赛!\n");
} else {
printf("\n很遗憾,电脑赢得了比赛!\n");
}
return 0;
}
```
代码中使用了循环和条件语句,通过随机数生成电脑的选择,比较玩家和电脑的选择,计算每一局的胜负,并统计比分,最终判断比赛结果。
阅读全文