centos7编写猜拳游戏
时间: 2024-04-03 12:30:07 浏览: 71
CentOS 7是一种流行的Linux操作系统,可以用于编写各种类型的应用程序,包括猜拳游戏。下面是一个简单的示例,展示了如何在CentOS 7上使用C语言编写一个简单的猜拳游戏:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int playerChoice, computerChoice;
int playerScore = 0, computerScore = 0;
char choices[3][10] = {"Rock", "Paper", "Scissors"};
srand(time(NULL));
printf("Welcome to the Rock-Paper-Scissors game!\n");
while (1) {
printf("\nPlease choose:\n");
printf("1. Rock\n");
printf("2. Paper\n");
printf("3. Scissors\n");
printf("0. Quit\n");
scanf("%d", &playerChoice);
if (playerChoice == 0) {
break;
}
if (playerChoice < 1 || playerChoice > 3) {
printf("Invalid choice. Please try again.\n");
continue;
}
computerChoice = rand() % 3 + 1;
printf("You chose: %s\n", choices[playerChoice - 1]);
printf("Computer chose: %s\n", choices[computerChoice - 1]);
if (playerChoice == computerChoice) {
printf("It's a tie!\n");
} else if ((playerChoice == 1 && computerChoice == 3) ||
(playerChoice == 2 && computerChoice == 1) ||
(playerChoice == 3 && computerChoice == 2)) {
printf("You win!\n");
playerScore++;
} else {
printf("Computer wins!\n");
computerScore++;
}
printf("Player score: %d\n", playerScore);
printf("Computer score: %d\n", computerScore);
}
printf("\nThanks for playing!\n");
return 0;
}
```
这个示例使用了C语言来实现一个简单的猜拳游戏。玩家可以选择石头、剪刀或者布,然后与计算机进行比较,根据规则判断胜负并计分。玩家可以选择退出游戏。
阅读全文