版本比较
斗地主游戏
正文
增加 54 行删除 30 行
33 行未改动3435363738394041157 行未改动全部展开199200201202203204205206207122 行未改动全部展开330331332333334335336337338344 行未改动全部展开6836846856866876886896906918 行未改动700701702703704705706707708348 行未改动全部展开1057105810591060106110621063106410659 行未改动107510761077107810791080108110821083104 行未改动全部展开118811891190119111921193119411951196146 行未改动全部展开13431344134513461347134813491350135113521353135411 行未改动13661367136813691370137113721373137459 行未改动1434143514361437143814391440144114421443144414451446144714483 行未改动1452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149232 行未改动1525152615271528152915301531153215335 行未改动1539154015411542154315441545154615472 行未改动15501551155215531554155515561557155815591560156115 行未改动1577157815791580158115821583158489 行未改动33 行未改动KING_BOMB,INVALID};// 卡牌结构体struct Card {int value;string name;157 行未改动全部展开for (const auto& card : lastPlay) {cout << card.name << " ";}CardType type = Game::getCardType(lastPlay);cout << " (牌型: " << type << ")" << endl;} else {cout << "你是第一个出牌的玩家。" << endl;}122 行未改动全部展开cout << name << "出牌: ";for (const auto& card : playCards) {cout << card.name << " ";}cout << " (牌型: " << Game::getCardType(playCards) << ")" << endl;removeCards(playCards);} else {cout << name << "选择跳过。" << endl;}344 行未改动全部展开int planeSize = airplane.size() / 3;vector<Card> wings;for (const auto& c : hand) {if (airplaneValues.find(c.value) == airplaneValues.end() && c.value < 13) {wings.push_back(c);if (wings.size() == planeSize) {break;}8 行未改动wings.clear();vector<vector<Card>> pairs = findPairs();for (const auto& pair : pairs) {if (airplaneValues.find(pair[0].value) == airplaneValues.end() && pair[0].value < 13) {wings.insert(wings.end(), pair.begin(), pair.end());if (wings.size() == planeSize * 2) {break;}348 行未改动全部展开}vector<Card> wings;for (const auto& c : hand) {if (airplaneValues.find(c.value) == airplaneValues.end() && c.value < 13) {wings.push_back(c);if (wings.size() == planeSize) {break;}9 行未改动wings.clear();vector<vector<Card>> pairs = findPairs();for (const auto& pair : pairs) {if (airplaneValues.find(pair[0].value) == airplaneValues.end() && pair[0].value < 13) {wings.insert(wings.end(), pair.begin(), pair.end());if (wings.size() == planeSize * 2) {break;}104 行未改动全部展开}void Game::initializeDeck() {deck.clear();int value = 0;for (const auto& rank : RANKS) {for (int i = 0; i < 4; i++) {deck.push_back({value, rank});}146 行未改动全部展开cout << "牌型说明: 单张、对子、三张、三带一、三带二、" << endl;cout << " 顺子(5张及以上,不含2和王)、" << endl;cout << " 连对(3对及以上,不含2和王)、" << endl;cout << " 飞机(2组及以上连续三张,不含2)、" << endl;cout << " 飞机带翅膀(不含2和王)、" << endl;cout << " 四带两对、炸弹、王炸" << endl;cout << "========================================" << endl;cout << "注意:2和王不能出现在顺子、连对、飞机中!" << endl;cout << "========================================" << endl;while (true) {dealCards();11 行未改动cout << "游戏结束,谢谢参与!" << endl;}// ==================== 核心牌型判断(最终稳定版) ====================CardType Game::getCardType(const vector<Card>& cards) {size_t size = cards.size();if (size == 0) return INVALID;59 行未改动// ===== 顺子 (5张及以上,不含2和王) =====if (size >= 5) {bool isStraight = true;bool hasInvalid = false;for (const auto& p : count) {if (p.second != 1) { isStraight = false; break; }if (p.first >= 13 || p.first >= 16) { hasInvalid = true; break; }}if (isStraight && !hasInvalid) {int minValue = sorted[0].value;for (size_t i = 0; i < size; i++) {if (sorted[i].value != minValue + (int)i) {return INVALID;3 行未改动}}// ===== 连对 (3对及以上,不含2和王) =====// ?? 关键修复:连对判断独立进行,不被其他逻辑干扰if (size >= 6 && size % 2 == 0) {bool isConsecutivePairs = true;vector<int> pairValues;for (const auto& p : count) {if (p.second != 2) {isConsecutivePairs = false;break;}if (p.first >= 13 || p.first >= 16) {isConsecutivePairs = false;break;}pairValues.push_back(p.first);}if (isConsecutivePairs && (int)pairValues.size() >= 3) {sort(pairValues.begin(), pairValues.end());bool consecutive = true;for (int i = 0; i < (int)pairValues.size() - 1; i++) {if (pairValues[i + 1] != pairValues[i] + 1) {consecutive = false;break;}}if (consecutive) {return CONSECUTIVE_PAIRS; // ? 直接返回,不再往下判断}}}// ===== 四带两对 (8张) =====if (size == 8) {bool hasFour = false;int pairCount = 0;bool hasSingle = false;32 行未改动bool isAirplane = true;vector<int> threeValues;for (const auto& p : count) {if (p.second != 3) { isAirplane = false; break; }if (p.first >= 13 || p.first >= 16) { isAirplane = false; break; }threeValues.push_back(p.first);}if (isAirplane && (int)threeValues.size() >= 2) {sort(threeValues.begin(), threeValues.end());5 行未改动return AIRPLANE;}}// ===== 飞机带翅膀 (不含2和王) =====if (size >= 8) {int threeCount = 0;int wingTotal = 0;vector<int> threeValues;2 行未改动for (const auto& p : count) {if (p.second == 3) {threeCount++;threeValues.push_back(p.first);if (p.first >= 13 || p.first >= 16) { valid = false; break; }} else if (p.second == 1 || p.second == 2) {wingTotal += p.second;if (p.first >= 13 || p.first >= 16) { valid = false; break; }} else {valid = false;break;}15 行未改动}return INVALID;}int Game::getKeyValue(const vector<Card>& cards, CardType type) {map<int, int> count;for (const auto& card : cards) {count[card.value]++;89 行未改动33 行未改动343536373839404142434445464748495051525354555657585960157 行未改动全部展开218219220221222223224225226122 行未改动全部展开349350351352353354355356357358344 行未改动全部展开7037047057067077087097107118 行未改动720721722723724725726727728348 行未改动全部展开1077107810791080108110821083108410859 行未改动109510961097109810991100110111021103104 行未改动全部展开120812091210121112121213121412151216146 行未改动全部展开13631364136513661367136813691370137113721373137411 行未改动13861387138813891390139113921393139459 行未改动14541455145614571458145914601461146214631464146514661467146814691470147114723 行未改动1476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151032 行未改动154315441545154615471548154915501551155215535 行未改动1559156015611562156315641565156615672 行未改动15701571157215731574157515761577157815791580158115821583158415 行未改动16001601160216031604160516061607160889 行未改动33 行未改动KING_BOMB,INVALID};// 牌型名称映射string getCardTypeName(CardType type) {switch(type) {case SINGLE: return "单张";case PAIR: return "对子";case THREE: return "三张";case THREE_WITH_ONE: return "三带一";case THREE_WITH_TWO: return "三带二";case STRAIGHT: return "顺子";case CONSECUTIVE_PAIRS: return "连对";case AIRPLANE: return "飞机";case AIRPLANE_WITH_WINGS: return "飞机带翅膀";case FOUR_WITH_TWO: return "四带二";case BOMB: return "炸弹";case KING_BOMB: return "王炸";default: return "无效牌型";}}// 卡牌结构体struct Card {int value;string name;157 行未改动全部展开for (const auto& card : lastPlay) {cout << card.name << " ";}CardType type = Game::getCardType(lastPlay);cout << " (" << getCardTypeName(type) << ")" << endl;} else {cout << "你是第一个出牌的玩家。" << endl;}122 行未改动全部展开cout << name << "出牌: ";for (const auto& card : playCards) {cout << card.name << " ";}CardType type = Game::getCardType(playCards);cout << " (" << getCardTypeName(type) << ")" << endl;removeCards(playCards);} else {cout << name << "选择跳过。" << endl;}344 行未改动全部展开int planeSize = airplane.size() / 3;vector<Card> wings;for (const auto& c : hand) {if (airplaneValues.find(c.value) == airplaneValues.end()) {wings.push_back(c);if (wings.size() == planeSize) {break;}8 行未改动wings.clear();vector<vector<Card>> pairs = findPairs();for (const auto& pair : pairs) {if (airplaneValues.find(pair[0].value) == airplaneValues.end()) {wings.insert(wings.end(), pair.begin(), pair.end());if (wings.size() == planeSize * 2) {break;}348 行未改动全部展开}vector<Card> wings;for (const auto& c : hand) {if (airplaneValues.find(c.value) == airplaneValues.end()) {wings.push_back(c);if (wings.size() == planeSize) {break;}9 行未改动wings.clear();vector<vector<Card>> pairs = findPairs();for (const auto& pair : pairs) {if (airplaneValues.find(pair[0].value) == airplaneValues.end()) {wings.insert(wings.end(), pair.begin(), pair.end());if (wings.size() == planeSize * 2) {break;}104 行未改动全部展开}void Game::initializeDeck() {deck.clear();int value = 3;for (const auto& rank : RANKS) {for (int i = 0; i < 4; i++) {deck.push_back({value, rank});}146 行未改动全部展开cout << "牌型说明: 单张、对子、三张、三带一、三带二、" << endl;cout << " 顺子(5张及以上,不含2和王)、" << endl;cout << " 连对(3对及以上,不含2和王)、" << endl;cout << " 飞机(2组及以上连续三张,不含2)、" << endl;cout << " 飞机带翅膀(翅膀可以带2和王)、" << endl;cout << " 四带二(可以带2和王)、炸弹、王炸" << endl;cout << "========================================" << endl;cout << "注意:2和王不能出现在顺子、连对、飞机主体中!" << endl;cout << "========================================" << endl;while (true) {dealCards();11 行未改动cout << "游戏结束,谢谢参与!" << endl;}// ==================== 核心牌型判断(最终修复版) ====================CardType Game::getCardType(const vector<Card>& cards) {size_t size = cards.size();if (size == 0) return INVALID;59 行未改动// ===== 顺子 (5张及以上,不含2和王) =====if (size >= 5) {bool isStraight = true;for (const auto& p : count) {if (p.second != 1) {isStraight = false;break;}if (p.first == 15 || p.first == 16 || p.first == 17) {return INVALID;}}if (isStraight) {int minValue = sorted[0].value;for (size_t i = 0; i < size; i++) {if (sorted[i].value != minValue + (int)i) {return INVALID;3 行未改动}}// ===== 连对 (3对及以上,不含2和王) =====if (size >= 6 && size % 2 == 0) {bool isConsecutivePairs = true;vector<int> pairValues;for (const auto& p : count) {if (p.second != 2) {isConsecutivePairs = false;break;}if (p.first == 15 || p.first == 16 || p.first == 17) {return INVALID;}pairValues.push_back(p.first);}if (isConsecutivePairs && (int)pairValues.size() >= 3) {sort(pairValues.begin(), pairValues.end());for (int i = 0; i < (int)pairValues.size() - 1; i++) {if (pairValues[i + 1] != pairValues[i] + 1) {return INVALID;}}return CONSECUTIVE_PAIRS;}}// ===== 四带两对 (8张) - 可以带2和王 =====if (size == 8) {bool hasFour = false;int pairCount = 0;bool hasSingle = false;32 行未改动bool isAirplane = true;vector<int> threeValues;for (const auto& p : count) {if (p.second != 3) { isAirplane = false; break; }if (p.first == 15) {return INVALID;}threeValues.push_back(p.first);}if (isAirplane && (int)threeValues.size() >= 2) {sort(threeValues.begin(), threeValues.end());5 行未改动return AIRPLANE;}}// ===== 飞机带翅膀 (翅膀可以带2和王) =====if (size >= 8) {int threeCount = 0;int wingTotal = 0;vector<int> threeValues;2 行未改动for (const auto& p : count) {if (p.second == 3) {threeCount++;threeValues.push_back(p.first);// 飞机主体不能有2if (p.first == 15) {return INVALID;}} else if (p.second == 1 || p.second == 2) {wingTotal += p.second;// 翅膀可以是任何牌,包括2和王} else {valid = false;break;}15 行未改动}return INVALID;}int Game::getKeyValue(const vector<Card>& cards, CardType type) {map<int, int> count;for (const auto& card : cards) {count[card.value]++;89 行未改动