You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++作业:如何从switch逻辑中移除玩家1选中的卡牌

抽卡功能实现方案

你原代码的核心问题是没有维护实体卡牌池,仅靠数值相等判断无法真正实现卡牌移除,很容易出现抽卡重复、编号和剩余卡牌不匹配的bug。下面的实现方案完全覆盖你要求的if语句、switch语句、for循环、while循环四种语法,同时严格遵循你列的4步流程。

实现逻辑

  • 用数组作为卡牌存储容器,初始存入1-10共10张卡牌
  • 每次抽卡前打乱当前卡牌池的顺序,洗牌逻辑用while循环实现
  • 玩家输入选牌编号时,用if语句判断输入合法性,非法输入要求重输
  • 玩家1抽卡完成后,用for循环遍历原卡牌池,剔除被抽走的卡牌,生成仅含9张牌的新卡牌池
  • 玩家抽卡结果用switch语句匹配对应卡牌效果

完整可运行代码

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

// 洗牌函数,用while循环实现随机打乱
void shuffle(int arr[], int len) {
    int i = len - 1;
    while (i > 0) {
        int randIdx = rand() % (i + 1);
        int temp = arr[i];
        arr[i] = arr[randIdx];
        arr[randIdx] = temp;
        i--;
    }
}

int main() {
    srand((unsigned int)time(NULL)); // 初始化随机数种子
    string name1 = "玩家1", name2 = "玩家2";
    int p1_choice, p2_choice, p1_card, p2_card;

    // 1. 初始化1-10的卡牌池,for循环赋值
    int cards[10];
    for (int i = 0; i < 10; i++) {
        cards[i] = i + 1;
    }
    // 第一次洗牌
    shuffle(cards, 10);

    // 2. 玩家1抽卡
    while (true) {
        cout << name1 << ": 请输入1-10之间的数字选择卡牌\n";
        cin >> p1_choice;
        // if判断输入合法性
        if (p1_choice >= 1 && p1_choice <= 10) {
            p1_card = cards[p1_choice - 1];
            break;
        }
        cout << "输入无效,请重新输入\n";
    }

    // 3. 移除玩家1抽到的卡,生成9张卡的剩余牌组
    int remaining[9];
    int idx = 0;
    for (int i = 0; i < 10; i++) {
        if (i != p1_choice - 1) {
            remaining[idx] = cards[i];
            idx++;
        }
    }
    // 剩余卡牌洗牌
    shuffle(remaining, 9);

    // 4. 玩家2抽卡
    while (true) {
        cout << name2 << ": 请输入1-9之间的数字选择卡牌\n";
        cin >> p2_choice;
        if (p2_choice >= 1 && p2_choice <= 9) {
            p2_card = remaining[p2_choice - 1];
            break;
        }
        cout << "输入无效,请重新输入\n";
    }

    // switch语句展示卡牌效果
    cout << "\n===== 抽卡结果 =====\n";
    cout << name1 << "抽到卡牌【" << p1_card << "】,效果:";
    switch (p1_card) {
        case 1: cout << "攻击力+2\n"; break;
        case 2: cout << "防御力+2\n"; break;
        case 3: cout << "速度+2\n"; break;
        case 4: cout << "回复2点生命值\n"; break;
        case 5: cout << "抽1张额外卡牌\n"; break;
        case 6: cout << "对敌方造成3点伤害\n"; break;
        case 7: cout << "抵消下一次敌方攻击\n"; break;
        case 8: cout << "丢弃敌方1张手牌\n"; break;
        case 9: cout << "本回合免疫所有伤害\n"; break;
        case 10: cout << "直接获得胜利\n"; break;
        default: cout << "普通卡牌\n";
    }

    cout << name2 << "抽到卡牌【" << p2_card << "】,效果:";
    switch (p2_card) {
        case 1: cout << "攻击力+2\n"; break;
        case 2: cout << "防御力+2\n"; break;
        case 3: cout << "速度+2\n"; break;
        case 4: cout << "回复2点生命值\n"; break;
        case 5: cout << "抽1张额外卡牌\n"; break;
        case 6: cout << "对敌方造成3点伤害\n"; break;
        case 7: cout << "抵消下一次敌方攻击\n"; break;
        case 8: cout << "丢弃敌方1张手牌\n"; break;
        case 9: cout << "本回合免疫所有伤害\n"; break;
        case 10: cout << "直接获得胜利\n"; break;
        default: cout << "普通卡牌\n";
    }

    return 0;
}

语法点对应说明

  • while循环:用于洗牌逻辑、玩家输入合法性校验,不满足条件就持续循环
  • for循环:用于初始卡牌池赋值、遍历原卡牌池生成剩余卡牌组
  • if语句:用于判断玩家输入是否在合法范围、判断当前卡牌是否需要保留到剩余牌组
  • switch语句:用于根据抽到的卡牌点数匹配对应的效果描述

这个方案真正实现了卡牌的物理移除,玩家2的可选卡牌池里完全不存在玩家1已经抽走的卡,不需要靠数值不等的判断来规避重复抽卡问题,逻辑更严谨。

内容的提问来源于stack exchange,提问作者Jie Min wong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 22:42:28