温馨提示×

C语言怎么实现简单的抽卡小游戏

小亿
118
2023-08-15 00:18:43
栏目: 编程语言

以下是一个简单的抽卡小游戏的C语言实现示例:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int cards[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int num_cards = 10;
int chosen_cards[3];
int i, j;
// 使用当前时间作为随机数种子
srand(time(0));
printf("=== 抽卡小游戏 ===\n");
// 抽3张卡
for (i = 0; i < 3; i++) {
printf("抽卡中...\n");
// 随机选择一张卡
int index = rand() % num_cards;
chosen_cards[i] = cards[index];
// 从数组中移除已抽到的卡
for (j = index; j < num_cards - 1; j++) {
cards[j] = cards[j + 1];
}
num_cards--;
printf("第%d张卡: %d\n", i + 1, chosen_cards[i]);
}
printf("=== 抽卡结束 ===\n");
return 0;
}

这个程序使用一个包含10张卡的数组来表示卡池,每张卡都有一个对应的整数值。程序通过调用 rand() 函数来随机选择一张卡,并将其从数组中移除,确保下次抽卡时不会再抽到。最后,程序输出抽到的3张卡的值。

注意:这只是一个简单的示例,实际上,抽卡游戏可能会更复杂,例如可能会有不同稀有度的卡、概率控制、卡牌属性等等。这个示例只是展示了一个简单的抽卡过程。

1