帮我写一段devc++可以运行的人机对战代码
时间: 2023-10-18 07:19:53 浏览: 34
好的,这里是一个简单的人机对战游戏代码,你可以在 Dev-C++ 中运行它。请将代码复制到 Dev-C++ 编辑器中,然后按 `F9` 键运行。
```c++
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 人机对战游戏
int main() {
int player_score = 0; // 玩家得分
int computer_score = 0; // 电脑得分
int round = 1; // 当前回合数
int player_choice; // 玩家选择
int computer_choice; // 电脑选择
char input; // 用户输入
// 设置随机数种子
srand(time(NULL));
// 游戏循环
while (1) {
// 清屏
system("cls");
// 输出当前回合数和得分
printf("Round %d\n", round);
printf("Player: %d Computer: %d\n", player_score, computer_score);
// 玩家输入
printf("Please choose (1 - Rock, 2 - Scissors, 3 - Paper): ");
scanf("%d", &player_choice);
// 随机生成电脑选择
computer_choice = rand() % 3 + 1;
// 输出玩家和电脑选择
printf("Player: ");
switch (player_choice) {
case 1: printf("Rock\n"); break;
case 2: printf("Scissors\n"); break;
case 3: printf("Paper\n"); break;
}
printf("Computer: ");
switch (computer_choice) {
case 1: printf("Rock\n"); break;
case 2: printf("Scissors\n"); break;
case 3: printf("Paper\n"); break;
}
// 判断输赢
if (player_choice == computer_choice) {
printf("Draw!\n");
} else if ((player_choice == 1 && computer_choice == 2) ||
(player_choice == 2 && computer_choice == 3) ||
(player_choice == 3 && computer_choice == 1)) {
printf("Player Wins!\n");
player_score++;
} else {
printf("Computer Wins!\n");
computer_score++;
}
// 等待用户输入
printf("Press Enter to continue...");
while ((input = getchar()) != '\n');
// 增加回合数
round++;
}
return 0;
}
```
上面的代码实现了一个简单的人机对战游戏,玩家需要在每一回合中选择石头、剪刀或者布,然后与电脑进行比较,判断谁赢谁输。你可以根据需要修改代码中的一些参数,比如回合数、得分等等。