版本比较
斗地主游戏
正文
增加 54 行删除 30 行
33 行未改动343536373839404142434445464748495051525354555657585960157 行未改动全部展开218219220221203222223224225226122 行未改动全部展开349350351352334353354355356357358344 行未改动全部展开7037047057066877077087097107118 行未改动720721722723704724725726727728348 行未改动全部展开10771078107910801061108110821083108410859 行未改动1095109610971098107910991100110111021103104 行未改动全部展开1208120912101211119212121213121412151216146 行未改动全部展开13631364136513661347134813671368136913501370137113721373137411 行未改动138613871388138913701390139113921393139459 行未改动145414551456145714381458144014411459146014611462146314641465146614671444146814691470147114723 行未改动147614771478147914561480148114821483148414851486148714881466146714681489149014911492149314941495149614751497149814781479149915001501148214831502148415031504150514881506150715081509151032 行未改动1543154415451546152915471548154915501551155215535 行未改动15591560156115621543156315641565156615672 行未改动1570157115721573155415741575157615771578157915571580158115821583158415 行未改动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 << " (牌型: " << type << ")" << endl; cout << " (" << getCardTypeName(type) << ")" << endl; } else { cout << "你是第一个出牌的玩家。" << endl; } 122 行未改动全部展开 cout << name << "出牌: "; for (const auto& card : playCards) { cout << card.name << " "; } cout << " (牌型: " << Game::getCardType(playCards) << ")" << endl; 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() && c.value < 13) { 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() && pair[0].value < 13) { 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() && c.value < 13) { 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() && pair[0].value < 13) { 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 = 0; 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 << " 四带两对、炸弹、王炸" << endl; cout << " 飞机带翅膀(翅膀可以带2和王)、" << endl; cout << " 四带二(可以带2和王)、炸弹、王炸" << endl; cout << "========================================" << endl; cout << "注意:2和王不能出现在顺子、连对、飞机中!" << 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 (p.second != 1) { isStraight = false; break; } if (p.first == 15 || p.first == 16 || p.first == 17) { return INVALID; } } if (isStraight && !hasInvalid) { 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 >= 13 || p.first >= 16) { 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()); bool consecutive = true; for (int i = 0; i < (int)pairValues.size() - 1; i++) { if (pairValues[i + 1] != pairValues[i] + 1) { consecutive = false; break; return INVALID; } } if (consecutive) { return CONSECUTIVE_PAIRS; // ? 直接返回,不再往下判断 return CONSECUTIVE_PAIRS; } } } // ===== 四带两对 (8张) ===== // ===== 四带两对 (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 >= 13 || p.first >= 16) { 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和王) ===== // ===== 飞机带翅膀 (翅膀可以带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; } // 飞机主体不能有2 if (p.first == 15) { return INVALID; } } else if (p.second == 1 || p.second == 2) { wingTotal += p.second; if (p.first >= 13 || p.first >= 16) { valid = false; break; } // 翅膀可以是任何牌,包括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 行未改动