写出一个小游戏c语言程序
下面是一个石头剪刀布小游戏的C语言程序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int player, computer, result;
srand(time(NULL)); // 初始化随机数种子
printf("欢迎来玩石头剪刀布游戏!\n");
printf("1. 石头\n2. 剪刀\n3. 布\n");
printf("请选择你的出拳:");
scanf("%d", &player);
computer = rand() % 3 + 1; // 计算电脑出拳
// 判断输赢
if (player == computer) {
result = 0;
} else if (player == 1 && computer == 2 || player == 2 && computer == 3 || player == 3 && computer == 1) {
result = 1;
} else {
result = -1;
}
// 输出结果
if (result == 0) {
printf("平局!\n");
} else if (result == 1) {
printf("你赢了!\n");
} else {
printf("你输了!\n");
}
return 0;
}
玩家和电脑分别出拳,用if语句判断输赢,输出结果。在程序中使用了srand和rand函数来生成随机数,确保电脑的出拳是随机的。
原文地址: https://www.cveoy.top/t/topic/bgJy 著作权归作者所有。请勿转载和采集!