使用C语言研发一个人机交互的棒子老虎鸡的游戏!游戏规则是:棒子打老虎,老虎吃鸡,鸡吃虫,虫吃棒子!要求游戏生动形象,每10次给一个输赢统计! 代码
时间: 2023-11-21 17:06:20 浏览: 83
好的,以下是一个简单的棒子老虎鸡游戏的C语言代码。请注意,为了简化代码,以下实现没有使用ASCII艺术字符,而是使用了简单的文字表示棒子、老虎、鸡、虫四种动物的形象。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int player_choice, computer_choice, player_win = 0, computer_win = 0, round = 0;
srand((unsigned int)time(NULL)); // 用当前时间初始化随机数发生器
printf("Welcome to the Stick-Tiger-Chicken-Insect game!\n");
printf("In this game, Stick beats Tiger, Tiger eats Chicken, Chicken eats Insect, Insect beats Stick.\n");
printf("You will play against the computer. Let's start!\n\n");
do {
round++;
printf("Round %d:\n", round);
// 生成电脑随机选择的动物
computer_choice = rand() % 4 + 1;
// 让玩家输入自己选择的动物
printf("Enter your choice (1 for Stick, 2 for Tiger, 3 for Chicken, 4 for Insect): ");
scanf("%d", &player_choice);
// 判断玩家和电脑的胜负关系
if (player_choice == computer_choice) {
printf("Draw!\n");
} else if ((player_choice == 1 && computer_choice == 4) || (player_choice == 2 && computer_choice == 1) ||
(player_choice == 3 && computer_choice == 2) || (player_choice == 4 && computer_choice == 3)) {
printf("You win!\n");
player_win++;
} else {
printf("Computer wins!\n");
computer_win++;
}
// 每10次游戏结束后打印胜负次数统计
if (round % 10 == 0) {
printf("You won %d times and the computer won %d times in the last 10 rounds.\n", player_win, computer_win);
player_win = 0;
computer_win = 0;
}
printf("\n");
} while (1);
return 0;
}
```
希望这个代码对您有帮助!
阅读全文