也是用AI搓了一个斗地主和骗子的纸牌合集啊
本人改动了一下
欢迎游玩
/*
Dev-C++
点
工具[T]
编程选项 [C]
勾选 [ 编译时加入以下命令 ]
-std=c++11
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <limits>
#include <random>
#include <numeric>
#include <unordered_map>
#include <set>
#include <conio.h>
#include <cmath>
#ifdef _WIN32
#include <windows.h>
#define DELAY_MS(t) Sleep(t)
#define CLEAR_SCREEN() system("cls")
#define PAUSE_PROGRAM() system("pause")
#else
#include <unistd.h>
#define DELAY_MS(t) usleep(t * 1000)
#define CLEAR_SCREEN() system("clear")
#define PAUSE_PROGRAM() std::cin.get()
#endif
// 全局配置
bool g_cheatMode = false;
std::mt19937 g_rng(std::time(0));
int g_print_delay_ms = 15; // 全局输出延迟(毫秒),可在设置中调整
// 牌结构体
struct Card {
std::string rank;
bool is_joker;
int value;
};
// 玩家结构体
struct Player {
std::string name;
std::vector<Card> hand_cards;
int team_id;
bool is_human;
bool is_landlord;
bool pass_this_round; // 仅控制是否出牌,不控制质疑
};
// 牌型枚举(优先级从低到高)
enum class CardType {
INVALID, // 无效牌型
SINGLE, // 单牌
PAIR, // 对子
SINGLE_STRAIGHT,// 单牌顺子(5+张递增)
PAIR_STRAIGHT, // 对子顺子(3+组递增)
THREE, // 三不带
THREE_ONE, // 三带一
THREE_TWO, // 三带二
THREE_STRAIGHT,// 三代顺子(2+组递增,可带牌)
BOMB, // 炸弹(四张相同)
FOUR_TWO, // 四带二(四张+两张单/两个对子)
KING_BOMB // 王炸(大小王)
};
// 出牌组合结构体
struct PlayedCards {
std::vector<Card> cards; // 出的牌
CardType type; // 牌型
int main_value; // 牌型主值(用于比较大小)
int length; // 牌型长度(顺子类)
};
// 带延迟的打印函数(兼容格式化和换行符)
void print_with_delay(const std::string &s, const std::initializer_list<int> &args, int delay_ms = -1) {
// 使用全局延迟配置,允许临时覆盖
int actual_delay = (delay_ms == -1) ? g_print_delay_ms : delay_ms;
auto arg_it = args.begin();
int str_len = s.size();
for (int i = 0; i < str_len; ++i) {
if (i < str_len - 1 && s[i] == '%' && s[i+1] == 'd') {
if (arg_it != args.end()) {
printf("%d", *arg_it++);
} else {
printf("【错误】格式化参数不足!\n");
}
DELAY_MS(actual_delay);
++i;
continue;
}
if (i < str_len - 1 && s[i] == '/' && s[i+1] == 'n') {
printf("\n");
DELAY_MS(actual_delay);
++i;
continue;
}
printf("%c", s[i]);
DELAY_MS(actual_delay);
}
}
// 重载:无格式化参数的打印
void print_with_delay(const std::string &s, int delay_ms = -1) {
print_with_delay(s, {}, delay_ms);
}
// 初始化牌的数值
void init_card_value(Card& card) {
if (card.rank == "3") card.value = 3;
else if (card.rank == "4") card.value = 4;
else if (card.rank == "5") card.value = 5;
else if (card.rank == "6") card.value = 6;
else if (card.rank == "7") card.value = 7;
else if (card.rank == "8") card.value = 8;
else if (card.rank == "9") card.value = 9;
else if (card.rank == "10") card.value = 10;
else if (card.rank == "J") card.value = 11;
else if (card.rank == "Q") card.value = 12;
else if (card.rank == "K") card.value = 13;
else if (card.rank == "A") card.value = 14;
else if (card.rank == "2") card.value = 15;
else if (card.rank == "小王") { card.value = 16; card.is_joker = true; }
else if (card.rank == "大王") { card.value = 17; card.is_joker = true; }
}
// 创建牌组
std::vector<Card> create_card_set(int set_count) {
std::vector<Card> cards;
const std::string ranks[] = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (int i = 0; i < set_count; ++i) {
for (const std::string& rank : ranks) {
Card card;
card.rank = rank;
card.is_joker = false;
init_card_value(card);
for (int k = 0; k < 4; ++k) {
cards.push_back(card);
}
}
Card small_joker{"小王", true, 16};
Card big_joker{"大王", true, 17};
cards.push_back(small_joker);
cards.push_back(big_joker);
}
return cards;
}
// 洗牌
void shuffle_cards(std::vector<Card>& cards) {
std::shuffle(cards.begin(), cards.end(), g_rng);
}
// 随机出牌顺序
std::vector<int> draw_play_order(int player_count) {
std::vector<int> order(player_count);
std::iota(order.begin(), order.end(), 0);
std::shuffle(order.begin(), order.end(), g_rng);
return order;
}
// 手牌排序(从大到小)
void sort_hand_cards(std::vector<Card>& hand_cards) {
std::sort(hand_cards.begin(), hand_cards.end(),
[](const Card& a, const Card& b) { return a.value > b.value; });
}
// 显示玩家手牌(作弊模式控制可见性)
void show_player_hand(const Player& human_player, const Player& target_player) {
print_with_delay(target_player.name + " 的手牌:");
// 可见性规则:自己的牌 / 同队牌 / 作弊模式开启
bool is_visible = (human_player.name == target_player.name)
|| (human_player.team_id == target_player.team_id && human_player.team_id != 0)
|| g_cheatMode;
if (is_visible) {
for (int i = 0; i < target_player.hand_cards.size(); ++i) {
print_with_delay("[" + std::to_string(i) + "]" + target_player.hand_cards[i].rank + " ");
}
print_with_delay("(%d张)", {static_cast<int>(target_player.hand_cards.size())});
} else {
print_with_delay("??? (%d张)", {static_cast<int>(target_player.hand_cards.size())});
}
if (target_player.is_landlord) {
print_with_delay(" [地主]");
}
print_with_delay("/n");
}
// 安全读取整数
int safe_read_int(const std::string& prompt, int min_val, int max_val) {
int input;
while (true) {
print_with_delay(prompt);
while (!(std::cin >> input)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
print_with_delay("输入无效!请输入数字!/n");
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (input >= min_val && input <= max_val) {
break;
} else {
print_with_delay("输入超出范围!请输入%d~%d之间的数字!/n", {min_val, max_val});
}
}
return input;
}
// 统计手牌中相同数值的牌的数量
std::unordered_map<int, int> count_card_values(const std::vector<Card>& cards) {
std::unordered_map<int, int> count_map;
for (const Card& card : cards) {
count_map[card.value]++;
}
return count_map;
}
// 检测牌型(核心函数)
PlayedCards detect_card_type(const std::vector<Card>& cards) {
PlayedCards result;
result.cards = cards;
result.type = CardType::INVALID;
result.main_value = 0;
result.length = 0;
// 空牌组
if (cards.empty()) return result;
// 1. 王炸(大小王)
if (cards.size() == 2) {
bool has_small_joker = false, has_big_joker = false;
for (const Card& c : cards) {
if (c.value == 16) has_small_joker = true;
if (c.value == 17) has_big_joker = true;
}
if (has_small_joker && has_big_joker) {
result.type = CardType::KING_BOMB;
result.main_value = 100; // 王炸最大
result.length = 2;
return result;
}
}
// 排序牌组(从小到大)用于检测顺子
std::vector<Card> sorted_cards = cards;
std::sort(sorted_cards.begin(), sorted_cards.end(), [](const Card& a, const Card& b) {
return a.value < b.value;
});
auto count_map = count_card_values(cards);
std::vector<int> values;
for (const Card& c : sorted_cards) values.push_back(c.value);
// 2. 单牌
if (cards.size() == 1) {
result.type = CardType::SINGLE;
result.main_value = sorted_cards[0].value;
result.length = 1;
return result;
}
// 3. 对子
if (cards.size() == 2 && count_map.size() == 1) {
result.type = CardType::PAIR;
result.main_value = sorted_cards[0].value;
result.length = 2;
return result;
}
// 4. 炸弹(四张相同)
if (cards.size() == 4 && count_map.size() == 1) {
result.type = CardType::BOMB;
result.main_value = sorted_cards[0].value;
result.length = 4;
return result;
}
// 5. 单牌顺子(5+张,连续递增,不包含2和王)
if (cards.size() >= 5 && count_map.size() == cards.size()) {
bool is_straight = true;
for (int i = 1; i < values.size(); ++i) {
if (values[i] != values[i-1] + 1 || values[i] >= 15) { // 15是2的value,王是16/17
is_straight = false;
break;
}
}
if (is_straight) {
result.type = CardType::SINGLE_STRAIGHT;
result.main_value = sorted_cards.back().value; // 最大牌为基准
result.length = cards.size();
return result;
}
}
// 6. 对子顺子(3+组,每组2张,连续递增,不包含2和王)
if (cards.size() >= 6 && cards.size() % 2 == 0) {
int pair_count = cards.size() / 2;
if (pair_count >= 3 && count_map.size() == pair_count) {
// 检查每个值都是2张
bool all_pair = true;
for (auto& p : count_map) {
if (p.second != 2 || p.first >= 15) {
all_pair = false;
break;
}
}
if (all_pair) {
// 检查数值连续
std::vector<int> unique_values;
for (auto& p : count_map) unique_values.push_back(p.first);
std::sort(unique_values.begin(), unique_values.end());
bool is_straight = true;
for (int i = 1; i < unique_values.size(); ++i) {
if (unique_values[i] != unique_values[i-1] + 1) {
is_straight = false;
break;
}
}
if (is_straight) {
result.type = CardType::PAIR_STRAIGHT;
result.main_value = unique_values.back();
result.length = pair_count;
return result;
}
}
}
}
// 7. 三不带/三带一/三带二
if (cards.size() == 3 && count_map.size() == 1) {
// 三不带
result.type = CardType::THREE;
result.main_value = sorted_cards[0].value;
result.length = 3;
return result;
} else if (cards.size() == 4) {
// 三带一(3+1)
bool has_three = false, has_one = false;
int three_value = 0;
for (auto& p : count_map) {
if (p.second == 3) {
has_three = true;
three_value = p.first;
} else if (p.second == 1) {
has_one = true;
}
}
if (has_three && has_one) {
result.type = CardType::THREE_ONE;
result.main_value = three_value;
result.length = 4;
return result;
}
} else if (cards.size() == 5) {
// 三带二(3+2)
bool has_three = false, has_two = false;
int three_value = 0;
for (auto& p : count_map) {
if (p.second == 3) {
has_three = true;
three_value = p.first;
} else if (p.second == 2) {
has_two = true;
}
}
if (has_three && has_two) {
result.type = CardType::THREE_TWO;
result.main_value = three_value;
result.length = 5;
return result;
}
}
// 8. 三代顺子(2+组三不带,可带牌/不带牌,简化版:先支持6张不带牌)
if (cards.size() == 6) {
int three_count = 0;
int main_val = 0;
bool valid = true;
std::vector<int> three_values;
for (auto& p : count_map) {
if (p.second == 3 && p.first < 15) {
three_count++;
three_values.push_back(p.first);
} else {
valid = false;
break;
}
}
if (valid && three_count == 2) {
std::sort(three_values.begin(), three_values.end());
if (three_values[1] == three_values[0] + 1) {
result.type = CardType::THREE_STRAIGHT;
result.main_value = three_values.back();
result.length = 2; // 2组三代
return result;
}
}
}
// 9. 四带二(4+2,2可以是两张单或一对)
if (cards.size() == 6) {
bool has_four = false;
int four_value = 0;
int other_count = 0;
for (auto& p : count_map) {
if (p.second == 4) {
has_four = true;
four_value = p.first;
} else {
other_count += p.second;
}
}
if (has_four && other_count == 2) {
result.type = CardType::FOUR_TWO;
result.main_value = four_value;
result.length = 6;
return result;
}
}
return result;
}
// 比较两个牌型的大小(返回true表示new_cards能打过old_cards)
bool is_card_type_bigger(const PlayedCards& new_cards, const PlayedCards& old_cards) {
// 王炸最大,能打所有
if (new_cards.type == CardType::KING_BOMB) return true;
// 炸弹能打非王炸/非炸弹的所有牌型
if (new_cards.type == CardType::BOMB) {
return old_cards.type != CardType::KING_BOMB && old_cards.type != CardType::BOMB;
}
// 同牌型比较
if (new_cards.type == old_cards.type) {
// 顺子类需要长度相同
if ((old_cards.type == CardType::SINGLE_STRAIGHT ||
old_cards.type == CardType::PAIR_STRAIGHT ||
old_cards.type == CardType::THREE_STRAIGHT) &&
new_cards.length != old_cards.length) {
return false;
}
// 比较主值
return new_cards.main_value > old_cards.main_value;
}
// 不同牌型(非炸弹/王炸)打不过
return false;
}
// 选择要出的牌(玩家手动选择多个索引)
std::vector<int> select_card_indices(const Player& player, int max_count) {
std::vector<int> selected_indices;
print_with_delay("请输入要出的牌索引(空格分隔,输入-1结束,最多%d张):/n", {max_count});
while (true) {
int idx = safe_read_int("", -1, static_cast<int>(player.hand_cards.size()) - 1);
if (idx == -1) {
if (selected_indices.empty()) {
print_with_delay("至少选择1张牌!/n");
continue;
}
break;
}
// 检查是否重复选择
if (std::find(selected_indices.begin(), selected_indices.end(), idx) != selected_indices.end()) {
print_with_delay("已选择过该索引!/n");
continue;
}
selected_indices.push_back(idx);
if (selected_indices.size() >= max_count) {
print_with_delay("已选满%d张牌!/n", {max_count});
break;
}
}
// 排序索引(从大到小删除,避免索引错乱)
std::sort(selected_indices.begin(), selected_indices.end(), std::greater<int>());
return selected_indices;
}
// 机器人叫地主逻辑
bool robot_call_landlord(const Player& player) {
int big_card_count = 0;
int bomb_count = 0;
auto count_map = count_card_values(player.hand_cards);
// 统计大牌(2、王)
for (const Card& card : player.hand_cards) {
if (card.value >= 15) {
++big_card_count;
}
}
// 统计炸弹
for (auto& p : count_map) {
if (p.second == 4) bomb_count++;
}
// 有炸弹必叫,没炸弹则大牌≥2才叫
bool call = (bomb_count > 0) || (big_card_count >= 2);
print_with_delay(player.name + (call ? " 选择叫地主!/n" : " 不叫地主。/n"));
return call;
}
// 玩家叫地主
bool human_call_landlord(const Player& player) {
int choice = safe_read_int(player.name + ",是否叫地主?(1=叫,0=不叫):", 0, 1);
return (choice == 1);
}
// 机器人出牌逻辑(支持多牌型)
bool robot_play_landlord_card(Player& player, PlayedCards& played_cards, const PlayedCards& last_played) {
// 收集所有合法牌型
std::vector<PlayedCards> valid_plays;
// 生成所有可能的单牌组合(简化版:优先出能打过的最小牌型)
// 1. 先检查是否有王炸
bool has_small_joker = false, has_big_joker = false;
int small_idx = -1, big_idx = -1;
for (int i = 0; i < player.hand_cards.size(); ++i) {
if (player.hand_cards[i].value == 16) { has_small_joker = true; small_idx = i; }
if (player.hand_cards[i].value == 17) { has_big_joker = true; big_idx = i; }
}
if (has_small_joker && has_big_joker) {
std::vector<Card> king_bomb = {player.hand_cards[small_idx], player.hand_cards[big_idx]};
PlayedCards kb_play = detect_card_type(king_bomb);
if (is_card_type_bigger(kb_play, last_played)) {
valid_plays.push_back(kb_play);
}
}
// 2. 检查炸弹
auto count_map = count_card_values(player.hand_cards);
for (auto& p : count_map) {
if (p.second == 4) {
std::vector<Card> bomb;
for (const Card& c : player.hand_cards) {
if (c.value == p.first) bomb.push_back(c);
}
PlayedCards bomb_play = detect_card_type(bomb);
if (is_card_type_bigger(bomb_play, last_played)) {
valid_plays.push_back(bomb_play);
}
}
}
// 3. 同牌型找更大的
if (last_played.type != CardType::INVALID) {
// 单牌
if (last_played.type == CardType::SINGLE) {
for (const Card& c : player.hand_cards) {
std::vector<Card> single = {c};
PlayedCards single_play = detect_card_type(single);
if (is_card_type_bigger(single_play, last_played)) {
valid_plays.push_back(single_play);
}
}
}
// 对子
if (last_played.type == CardType::PAIR) {
for (auto& p : count_map) {
if (p.second >= 2 && p.first > last_played.main_value) {
std::vector<Card> pair;
int cnt = 0;
for (const Card& c : player.hand_cards) {
if (c.value == p.first && cnt < 2) {
pair.push_back(c);
cnt++;
}
}
PlayedCards pair_play = detect_card_type(pair);
valid_plays.push_back(pair_play);
}
}
}
}
// 4. 首轮出牌(随便出一个合法牌型)
if (last_played.type == CardType::INVALID && valid_plays.empty()) {
// 出最小的单牌
sort_hand_cards(player.hand_cards);
std::vector<Card> single = {player.hand_cards.back()};
PlayedCards single_play = detect_card_type(single);
valid_plays.push_back(single_play);
}
// 没有合法牌型则不出
if (valid_plays.empty()) {
print_with_delay(player.name + " 选择不出牌!/n");
played_cards.type = CardType::INVALID;
return false;
}
// 选择第一个合法牌型出牌
played_cards = valid_plays[0];
// 从手牌中删除出的牌
std::vector<Card> new_hand;
std::set<int> played_values;
for (const Card& c : played_cards.cards) played_values.insert(c.value);
int remove_count = 0;
for (const Card& c : player.hand_cards) {
bool keep = true;
for (const Card& pc : played_cards.cards) {
if (c.rank == pc.rank && remove_count < played_cards.cards.size()) {
keep = false;
remove_count++;
break;
}
}
if (keep) new_hand.push_back(c);
}
player.hand_cards = new_hand;
// 打印出牌信息
print_with_delay(player.name + " 出了:");
for (const Card& c : played_cards.cards) {
print_with_delay(c.rank + " ");
}
print_with_delay("/n");
return true;
}
// 玩家出牌逻辑(支持多牌型)
bool human_play_landlord_card(Player& player, PlayedCards& played_cards, const PlayedCards& last_played, bool is_first_round) {
show_player_hand(player, player);
while (true) {
std::string prompt;
if (is_first_round) {
prompt = "是否出牌?(1=出牌,0=不出牌):";
} else {
prompt = "上轮出牌:";
if (last_played.type != CardType::INVALID) {
for (const Card& c : last_played.cards) prompt += c.rank + " ";
}
prompt += "(1=出牌,0=不出牌):";
}
int choice = safe_read_int(prompt, 0, 1);
if (choice == 0) {
if (is_first_round) {
print_with_delay("首轮必须出牌!/n");
continue;
}
print_with_delay(player.name + " 选择不出牌!/n");
played_cards.type = CardType::INVALID;
return false;
}
// 选择要出的牌索引
std::vector<int> selected_indices = select_card_indices(player, player.hand_cards.size());
// 提取选中的牌
std::vector<Card> selected_cards;
for (int idx : selected_indices) {
selected_cards.push_back(player.hand_cards[idx]);
}
// 检测牌型
PlayedCards detected = detect_card_type(selected_cards);
if (detected.type == CardType::INVALID) {
print_with_delay("无效牌型!请重新选择!/n");
continue;
}
// 检查是否能打过上一轮(非首轮)
if (!is_first_round && !is_card_type_bigger(detected, last_played)) {
print_with_delay("该牌型打不过上一轮的牌!请重新选择!/n");
continue;
}
// 合法牌型,从手牌中删除(按从大到小删,避免索引错乱)
std::sort(selected_indices.begin(), selected_indices.end(), std::greater<int>());
for (int idx : selected_indices) {
player.hand_cards.erase(player.hand_cards.begin() + idx);
}
// 输出出牌信息
print_with_delay(player.name + " 出了:");
for (const Card& c : selected_cards) {
print_with_delay(c.rank + " ");
}
print_with_delay("(牌型:");
switch (detected.type) {
case CardType::SINGLE: print_with_delay("单牌"); break;
case CardType::PAIR: print_with_delay("对子"); break;
case CardType::SINGLE_STRAIGHT: print_with_delay("单顺"); break;
case CardType::PAIR_STRAIGHT: print_with_delay("对顺"); break;
case CardType::THREE: print_with_delay("三不带"); break;
case CardType::THREE_ONE: print_with_delay("三带一"); break;
case CardType::THREE_TWO: print_with_delay("三带二"); break;
case CardType::THREE_STRAIGHT: print_with_delay("三代顺"); break;
case CardType::BOMB: print_with_delay("炸弹"); break;
case CardType::FOUR_TWO: print_with_delay("四带二"); break;
case CardType::KING_BOMB: print_with_delay("王炸"); break;
default: print_with_delay("未知"); break;
}
print_with_delay(")/n");
played_cards = detected;
return true;
}
}
// 运行斗地主游戏
void run_landlord_game() {
CLEAR_SCREEN();
print_with_delay(std::string("===== 斗地主游戏(作弊模式:") + (g_cheatMode ? "开启" : "关闭") + ")=====/n");
int player_count = safe_read_int("请输入游戏人数(≥3):", 3, 10);
int card_set_count = safe_read_int("请输入牌套数(≥1):", 1, 4);
std::vector<Card> all_cards = create_card_set(card_set_count);
shuffle_cards(all_cards);
std::vector<Player> players;
for (int i = 0; i < player_count; ++i) {
Player player;
player.name = (i == 0) ? "玩家" : ("机器" + std::to_string(i));
player.is_human = (i == 0);
player.is_landlord = false;
player.team_id = 0;
player.pass_this_round = false;
players.push_back(player);
}
// 发牌(留3张底牌)
int card_per_player = (all_cards.size() - 3) / player_count;
int card_idx = 0;
for (Player& player : players) {
for (int j = 0; j < card_per_player; ++j) {
player.hand_cards.push_back(all_cards[card_idx++]);
}
sort_hand_cards(player.hand_cards);
}
std::vector<Card> bottom_cards(all_cards.begin() + card_idx, all_cards.end());
// 叫地主环节
print_with_delay("\n===== 叫地主环节 =====/n");
std::vector<int> call_order = draw_play_order(player_count);
int landlord_idx = -1;
for (int idx : call_order) {
bool call = players[idx].is_human ? human_call_landlord(players[idx]) : robot_call_landlord(players[idx]);
if (call) {
landlord_idx = idx;
players[idx].is_landlord = true;
// 地主拿底牌
players[idx].hand_cards.insert(players[idx].hand_cards.end(), bottom_cards.begin(), bottom_cards.end());
sort_hand_cards(players[idx].hand_cards);
print_with_delay(players[idx].name + " 成为地主,底牌:");
for (const Card& card : bottom_cards) {
print_with_delay(card.rank + " ");
}
print_with_delay("/n");
break;
}
}
// 无人叫地主则随机指定
if (landlord_idx == -1) {
landlord_idx = std::uniform_int_distribution<int>(0, player_count-1)(g_rng);
players[landlord_idx].is_landlord = true;
players[landlord_idx].hand_cards.insert(players[landlord_idx].hand_cards.end(), bottom_cards.begin(), bottom_cards.end());
sort_hand_cards(players[landlord_idx].hand_cards);
print_with_delay("无人叫地主,随机指定 " + players[landlord_idx].name + " 为地主!/n");
}
// 显示初始手牌(作弊模式下显示所有人的牌)
print_with_delay("\n===== 初始手牌 =====/n");
const Player& human_player = players[0];
for (const Player& player : players) {
show_player_hand(human_player, player);
}
// 出牌环节
print_with_delay("\n===== 出牌环节 =====/n");
int current_player_idx = landlord_idx; // 地主先出
PlayedCards last_played;
last_played.type = CardType::INVALID;
bool game_over = false;
int pass_count = 0; // 连续不出牌次数
while (!game_over) {
Player& current_player = players[current_player_idx];
print_with_delay("\n----- " + current_player.name + " 的回合 -----/n");
PlayedCards played_cards;
bool has_played = false;
// 出牌逻辑
if (current_player.is_human) {
has_played = human_play_landlord_card(current_player, played_cards, last_played, last_played.type == CardType::INVALID);
} else {
has_played = robot_play_landlord_card(current_player, played_cards, last_played);
}
// 更新状态
if (has_played) {
last_played = played_cards;
pass_count = 0; // 有人出牌,重置不出牌计数
} else {
pass_count++;
// 所有人都不出牌,本轮结束,当前玩家重新出牌
if (pass_count >= player_count - 1) {
print_with_delay("所有人都不出牌," + current_player.name + " 重新出牌!/n");
last_played.type = CardType::INVALID;
pass_count = 0;
}
}
// 检查是否出完牌
if (current_player.hand_cards.empty()) {
game_over = true;
print_with_delay("\n===== 游戏结束 =====/n");
print_with_delay(current_player.name + " 出完牌!" + (current_player.is_landlord ? "地主胜利!" : "农民胜利!") + "/n");
}
// 下一个玩家
current_player_idx = (current_player_idx + 1) % player_count;
}
// 显示最终手牌
print_with_delay("\n===== 最终手牌 =====/n");
for (const Player& player : players) {
show_player_hand(human_player, player);
}
PAUSE_PROGRAM();
}
// ======================= 骗子游戏(增强版:支持机器人性格+选队伍) =======================
// 机器人性格枚举(对应需求的8种性格)
enum class RobotPersonality {
TRUTH_FIRST_LIE_MID_TRUTH_LAST, // 1. 一开始真,中间假,最后真
LITTLE_LIE_FIRST_THEN_TRUTH, // 2. 一开始出1张假的,之后出真的
LOTS_TRUTH_TEMPT_CHALLENGE, // 3. 一次性出很多真牌,引诱别人开牌
REVERSE_THINKING, // 4. 逆向思维:被开真→更易出假,被开假→更易出真
CHALLENGE_MANIA, // 5. 开牌狂魔(高概率质疑别人)
LIAR_MANIA, // 6. 骗人狂魔(高概率出假牌)
HONEST, // 7. 老实人(几乎只出真牌)
TRUTH_ONE_LIE_ONE // 8. 一张真、一张假交替出
};
// 机器人状态(记录性格、出牌历史、被开牌历史等)
struct RobotState {
RobotPersonality personality; // 当前轮性格
int round_play_count; // 本轮出牌次数
bool last_play_is_truth; // 上一次出牌是否为真(用于性格8)
int truth_challenge_count; // 被开牌且牌为真的次数
int lie_challenge_count; // 被开牌且牌为假的次数
double lie_probability; // 当前出假牌的概率(动态调整)
};
// 每个机器人对每个玩家的“信任度”(0~1),越高越信任 => 越不质疑
static std::vector<std::vector<double>> g_trust;
// 机器人对每个目标“多久没亲自开过(质疑过)这个目标”的计数
static std::vector<std::vector<int>> g_noChallengeTicks;
// 机器人状态集合
static std::vector<RobotState> g_robot_states;
// clamp 工具
static double clamp_double(double x, double lo, double hi) {
if (x < lo) return lo;
if (x > hi) return hi;
return x;
}
// 根据 put_count 计算基础质疑概率:1张最低,多张更高
static double base_challenge_prob_by_putcount(int put_count) {
const double MIN_P = 0.15; // 1张时
const double MAX_P = 0.65; // 多张时上限
const double k = 0.85;
double t = 1.0 - std::exp(-k * std::max(0, put_count - 1));
return MIN_P + (MAX_P - MIN_P) * t;
}
// 牌池越大,质疑概率越大
static double pool_size_bonus(int pool_size) {
const double MAX_BONUS = 0.28;
const double k = 0.12;
double t = 1.0 - std::exp(-k * std::max(0, pool_size));
return MAX_BONUS * t;
}
// 机器人“放松警惕”机制
static void relax_to_neutral_if_needed(int robot_idx, int target_idx) {
const int RELAX_TICKS = 4;
if (robot_idx < 0 || robot_idx >= (int)g_noChallengeTicks.size()) return;
if (target_idx < 0 || target_idx >= (int)g_noChallengeTicks[robot_idx].size()) return;
if (g_noChallengeTicks[robot_idx][target_idx] >= RELAX_TICKS) {
g_trust[robot_idx][target_idx] = 0.5;
g_noChallengeTicks[robot_idx][target_idx] = 0;
}
}
// 统计手牌中指定点数的牌数量(包含王炸万能牌)
static int count_rank_in_hand(const std::vector<Card>& hand, const std::string& target_rank) {
int count = 0;
for (const Card& c : hand) {
if (c.rank == target_rank || c.is_joker) {
count++;
}
}
return count;
}
// 找到手牌中数量最多的点数(用于机器人选择宣称点数)
static std::string find_most_rank_in_hand(const std::vector<Card>& hand) {
std::unordered_map<std::string, int> rank_count;
const std::string ranks[] = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
// 初始化计数
for (const std::string& r : ranks) {
rank_count[r] = 0;
}
// 统计各点数数量(王炸不计入,因为是万能牌)
for (const Card& c : hand) {
if (!c.is_joker) {
rank_count[c.rank]++;
}
}
// 找到数量最多的点数
std::string max_rank = "3";
int max_count = 0;
for (const auto& pair : rank_count) {
if (pair.second > max_count) {
max_count = pair.second;
max_rank = pair.first;
}
}
return max_rank;
}
// 根据性格获取机器人出假牌的概率
static double get_lie_probability_by_personality(int robot_idx, const std::string& target_rank, const std::vector<Card>& hand) {
RobotState& state = g_robot_states[robot_idx];
double lie_prob = state.lie_probability;
switch (state.personality) {
case RobotPersonality::TRUTH_FIRST_LIE_MID_TRUTH_LAST: {
// 1. 真→假→真:前1/3轮低概率假,中间1/3高概率,最后1/3低概率
int total_round = std::max(1, (int)hand.size() / 2);
double stage = (double)state.round_play_count / total_round;
if (stage < 0.33) lie_prob = 0.05; // 前期:几乎真
else if (stage < 0.66) lie_prob = 0.8; // 中期:高概率假
else lie_prob = 0.05; // 后期:几乎真
break;
}
case RobotPersonality::LITTLE_LIE_FIRST_THEN_TRUTH: {
// 2. 先出1张假,之后真:仅第一次出牌高概率假,之后极低
if (state.round_play_count == 0) lie_prob = 0.9;
else lie_prob = 0.05;
break;
}
case RobotPersonality::LOTS_TRUTH_TEMPT_CHALLENGE: {
// 3. 多真引诱开牌:几乎只出真,出牌数量多
lie_prob = 0.05;
break;
}
case RobotPersonality::REVERSE_THINKING: {
// 4. 逆向思维:被开真越多→越易假;被开假越多→越易真
int total_challenge = state.truth_challenge_count + state.lie_challenge_count;
if (total_challenge == 0) lie_prob = 0.5;
else {
double truth_ratio = (double)state.truth_challenge_count / total_challenge;
lie_prob = clamp_double(truth_ratio * 0.8, 0.1, 0.9);
}
break;
}
case RobotPersonality::CHALLENGE_MANIA: {
// 5. 开牌狂魔:出牌偏真(专注质疑别人,自己少骗人)
lie_prob = 0.1;
break;
}
case RobotPersonality::LIAR_MANIA: {
// 6. 骗人狂魔:高概率出假牌
lie_prob = 0.9;
break;
}
case RobotPersonality::HONEST: {
// 7. 老实人:几乎不出假牌
lie_prob = 0.01;
break;
}
case RobotPersonality::TRUTH_ONE_LIE_ONE: {
// 8. 一张真、一张假交替
lie_prob = state.last_play_is_truth ? 0.95 : 0.05;
break;
}
}
return clamp_double(lie_prob, 0.01, 0.99);
}
// 机器人选择出牌数量(根据性格调整)
static int get_play_count_by_personality(int robot_idx, const std::vector<Card>& hand) {
RobotState& state = g_robot_states[robot_idx];
int max_possible = std::min(3, (int)hand.size()); // 最多出3张
int min_possible = std::max(1, (int)hand.size() / 5); // 最少出1张
switch (state.personality) {
case RobotPersonality::LOTS_TRUTH_TEMPT_CHALLENGE:
return std::min(3, max_possible); // 多真引诱:出更多牌
case RobotPersonality::LIAR_MANIA:
return std::min(2, max_possible); // 骗人狂魔:少量多次骗
case RobotPersonality::CHALLENGE_MANIA:
return std::min(2, max_possible); // 开牌狂魔:中等数量
default:
return std::uniform_int_distribution<int>(min_possible, max_possible)(g_rng);
}
}
// 机器人选择要出的牌(根据性格决定真/假)
static std::vector<Card> robot_select_play_cards(int robot_idx, std::vector<Card>& hand, const std::string& target_rank, bool& is_lie) {
RobotState& state = g_robot_states[robot_idx];
std::vector<Card> selected_cards;
// 获取出假牌概率
double lie_prob = get_lie_probability_by_personality(robot_idx, target_rank, hand);
is_lie = (std::uniform_real_distribution<double>(0.0, 1.0)(g_rng) < lie_prob);
// 选择出牌数量
int play_count = get_play_count_by_personality(robot_idx, hand);
play_count = std::min(play_count, (int)hand.size());
// 选择牌:真牌(目标点数+王) 或 假牌(非目标点数,排除王)
if (!is_lie) {
// 出真牌:优先选目标点数,不够补王
for (auto it = hand.begin(); it != hand.end() && selected_cards.size() < play_count; ) {
if (it->rank == target_rank || it->is_joker) {
selected_cards.push_back(*it);
it = hand.erase(it);
} else {
++it;
}
}
// 如果真牌不够,补任意牌(被迫出假)
if (selected_cards.size() < play_count) {
for (auto it = hand.begin(); it != hand.end() && selected_cards.size() < play_count; ) {
selected_cards.push_back(*it);
it = hand.erase(it);
}
is_lie = true;
}
} else {
// 出假牌:优先选非目标点数(排除王)
for (auto it = hand.begin(); it != hand.end() && selected_cards.size() < play_count; ) {
if (it->rank != target_rank && !it->is_joker) {
selected_cards.push_back(*it);
it = hand.erase(it);
} else {
++it;
}
}
// 如果假牌不够,补王/目标点数(被迫出真)
if (selected_cards.size() < play_count) {
for (auto it = hand.begin(); it != hand.end() && selected_cards.size() < play_count; ) {
selected_cards.push_back(*it);
it = hand.erase(it);
}
is_lie = false;
}
}
// 更新机器人状态
state.round_play_count++;
state.last_play_is_truth = !is_lie;
state.lie_probability = lie_prob;
return selected_cards;
}
// 机器人是否质疑(开牌):根据性格调整(如开牌狂魔高概率质疑)
static bool robot_challenge_liar_by_personality(int robot_idx, int target_idx, int put_count, int pool_size) {
RobotState& state = g_robot_states[robot_idx];
double base_p = base_challenge_prob_by_putcount(put_count);
double pool_p = pool_size_bonus(pool_size);
double trust = g_trust[robot_idx][target_idx];
double adapt = (0.5 - trust) * 0.40;
// 开牌狂魔额外增加质疑概率
if (state.personality == RobotPersonality::CHALLENGE_MANIA) {
base_p = clamp_double(base_p + 0.3, 0.2, 0.95);
}
double p = clamp_double(base_p + pool_p + adapt, 0.05, 0.95);
return std::uniform_real_distribution<double>(0.0, 1.0)(g_rng) < p;
}
// 质疑/观察后更新信任度
static void update_trust_after_reveal(int robot_idx, int target_idx, bool truthful, int put_count, bool direct) {
if (robot_idx < 0 || robot_idx >= (int)g_trust.size()) return;
if (target_idx < 0 || target_idx >= (int)g_trust[robot_idx].size()) return;
if (robot_idx >= (int)g_robot_states.size()) return;
double &trust = g_trust[robot_idx][target_idx];
double weight = clamp_double(0.75 + 0.15 * put_count, 0.75, 1.45);
double observer_scale = direct ? 1.0 : 0.55;
if (truthful) {
trust = clamp_double(trust + 0.10 * weight * observer_scale, 0.05, 0.95);
// 更新被开真的计数
if (direct) g_robot_states[target_idx].truth_challenge_count++;
} else {
trust = clamp_double(trust - 0.14 * weight * observer_scale, 0.05, 0.95);
// 更新被开假的计数
if (direct) g_robot_states[target_idx].lie_challenge_count++;
}
}
// 随机分配机器人性格
static void assign_robot_personalities(std::vector<RobotState>& robot_states) {
std::vector<RobotPersonality> personalities = {
RobotPersonality::TRUTH_FIRST_LIE_MID_TRUTH_LAST,
RobotPersonality::LITTLE_LIE_FIRST_THEN_TRUTH,
RobotPersonality::LOTS_TRUTH_TEMPT_CHALLENGE,
RobotPersonality::REVERSE_THINKING,
RobotPersonality::CHALLENGE_MANIA,
RobotPersonality::LIAR_MANIA,
RobotPersonality::HONEST,
RobotPersonality::TRUTH_ONE_LIE_ONE
};
for (auto& state : robot_states) {
int rand_idx = std::uniform_int_distribution<int>(0, personalities.size()-1)(g_rng);
state.personality = personalities[rand_idx];
state.round_play_count = 0;
state.last_play_is_truth = true;
state.lie_probability = 0.5;
}
}
void run_liar_game() {
CLEAR_SCREEN();
print_with_delay(std::string("===== 骗子游戏(作弊模式:") + (g_cheatMode ? "开启" : "关闭") + ")=====/n");
int player_count = safe_read_int("请输入游戏人数(≥2):", 2, 10);
int is_team = safe_read_int("是否分队?(1=是,0=否):", 0, 1);
int card_set_count = safe_read_int("请输入牌套数(≥1):", 1, 4);
int total_cards = create_card_set(card_set_count).size();
int max_card_per_player = total_cards / player_count;
int card_per_player = safe_read_int(
"请输入每人牌数(≥5,最大" + std::to_string(max_card_per_player) + "):",
5, max_card_per_player
);
std::vector<Player> players;
int team1_count = 0, team2_count = 0;
if (is_team) {
// 计算基础分队人数(队1人多,队2人少)
team1_count = (player_count + 1) / 2;
team2_count = player_count / 2;
print_with_delay("\n===== 分队选择 =====/n");
print_with_delay("当前可分队:/n");
print_with_delay("队1(人多):%d人/n", {team1_count});
print_with_delay("队2(人少):%d人/n", {team2_count});
// 让玩家选择加入的队伍
int team_choice = safe_read_int(
"请选择你要加入的队伍(1=队1(人多),2=队2(人少),0=随机):",
0, 2
);
// 确定玩家的队伍ID
int human_team_id = 0;
if (team_choice == 0) {
human_team_id = std::uniform_int_distribution<int>(1, 2)(g_rng);
} else {
human_team_id = team_choice;
}
print_with_delay("你选择加入:队%d/n", {human_team_id});
// 调整机器人数量(玩家占1个位置)
int team1_robot = (human_team_id == 1) ? (team1_count - 1) : team1_count;
int team2_robot = (human_team_id == 2) ? (team2_count - 1) : team2_count;
print_with_delay("\n===== 最终分队结果 =====/n");
print_with_delay("队1:玩家%d人 + 机器人%d人 = 总计%d人/n",
{human_team_id == 1 ? 1 : 0, team1_robot, (human_team_id == 1 ? 1 : 0) + team1_robot});
print_with_delay("队2:玩家%d人 + 机器人%d人 = 总计%d人/n",
{human_team_id == 2 ? 1 : 0, team2_robot, (human_team_id == 2 ? 1 : 0) + team2_robot});
// 添加人类玩家
Player human_player;
human_player.name = "玩家";
human_player.is_human = true;
human_player.team_id = human_team_id;
human_player.pass_this_round = false;
players.push_back(human_player);
// 添加队1机器人
int robot_num = 1;
for (int i = 0; i < team1_robot; ++i) {
Player p;
p.name = "机器" + std::to_string(robot_num++);
p.is_human = false;
p.team_id = 1;
p.pass_this_round = false;
players.push_back(p);
}
// 添加队2机器人
for (int i = 0; i < team2_robot; ++i) {
Player p;
p.name = "机器" + std::to_string(robot_num++);
p.is_human = false;
p.team_id = 2;
p.pass_this_round = false;
players.push_back(p);
}
// 打乱玩家顺序(避免玩家固定在第一个)
std::shuffle(players.begin(), players.end(), g_rng);
} else {
// 不分队模式
for (int i = 0; i < player_count; ++i) {
Player player;
player.name = (i == 0) ? "玩家" : ("机器" + std::to_string(i));
player.is_human = (i == 0);
player.team_id = 0;
player.pass_this_round = false;
players.push_back(player);
}
}
// 初始化机器人状态、信任度和计数
g_robot_states.clear();
g_robot_states.resize(players.size());
assign_robot_personalities(g_robot_states); // 随机分配本轮性格
g_trust.assign(players.size(), std::vector<double>(players.size(), 0.5));
g_noChallengeTicks.assign(players.size(), std::vector<int>(players.size(), 0));
// 发牌
std::vector<Card> all_cards = create_card_set(card_set_count);
shuffle_cards(all_cards);
int card_idx = 0;
for (Player& player : players) {
for (int j = 0; j < card_per_player; ++j) {
player.hand_cards.push_back(all_cards[card_idx++]);
}
sort_hand_cards(player.hand_cards);
}
// 生成出牌顺序
std::vector<int> play_order(players.size());
std::iota(play_order.begin(), play_order.end(), 0);
std::shuffle(play_order.begin(), play_order.end(), g_rng);
print_with_delay("\n===== 出牌顺序 =====/n");
for (int idx : play_order) {
print_with_delay(players[idx].name + " → ");
}
print_with_delay("结束/n");
// 显示初始手牌
print_with_delay("\n===== 初始手牌 =====/n");
const Player& human_player = *std::find_if(players.begin(), players.end(), [](const Player& p) {
return p.is_human;
});
for (const Player& player : players) {
show_player_hand(human_player, player);
}
// 游戏核心逻辑
std::string target_rank;
std::string original_target;
std::vector<Card> pool_cards;
int current_order_idx = 0;
int last_play_idx = 0;
bool game_over = false;
bool all_pass = false;
print_with_delay("\n===== 游戏开始 =====/n");
int first_player_idx = play_order[0];
if (players[first_player_idx].is_human) {
print_with_delay(players[first_player_idx].name + " 请宣称本轮点数(如3、J、2):");
std::cin >> target_rank;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
// 机器人选择宣称点数:优先选自己手牌中数量最多的点数
target_rank = find_most_rank_in_hand(players[first_player_idx].hand_cards);
print_with_delay(players[first_player_idx].name + " 宣称点数:" + target_rank + "/n");
}
original_target = target_rank;
print_with_delay("本轮宣称点数:" + target_rank + "(大小王为万能牌)/n");
while (!game_over) {
for (Player& player : players) {
player.pass_this_round = false;
}
all_pass = true;
for (int i = 0; i < (int)play_order.size() && !game_over; ++i) {
int player_idx = play_order[(current_order_idx + i) % play_order.size()];
Player& current_player = players[player_idx];
if (current_player.pass_this_round) continue; // 仅跳过出牌,不影响质疑
print_with_delay("\n----- " + current_player.name + " 的回合 -----/n");
int put_count = 0;
std::vector<Card> put_cards;
bool is_robot_lie = false; // 机器人出牌是否为假
if (current_player.is_human) {
put_count = safe_read_int(
"请选择出牌数量(0=不出牌,1-" + std::to_string(current_player.hand_cards.size()) + "):",
0, static_cast<int>(current_player.hand_cards.size())
);
if (put_count > 0) {
show_player_hand(human_player, current_player);
// 收集选中的牌索引
std::vector<int> selected_indices;
for (int j = 0; j < put_count; ++j) {
int cidx = safe_read_int(
"选择第" + std::to_string(j+1) + "张牌的索引:",
0, static_cast<int>(current_player.hand_cards.size()) - 1
);
// 检查重复选择
while (std::find(selected_indices.begin(), selected_indices.end(), cidx) != selected_indices.end()) {
print_with_delay("已选择过该索引!请重新选择:/n");
cidx = safe_read_int("", 0, static_cast<int>(current_player.hand_cards.size()) - 1);
}
selected_indices.push_back(cidx);
}
// 按从大到小排序索引(避免删除偏移)
std::sort(selected_indices.begin(), selected_indices.end(), std::greater<int>());
// 提取并删除牌
for (int idx : selected_indices) {
put_cards.push_back(current_player.hand_cards[idx]);
current_player.hand_cards.erase(current_player.hand_cards.begin() + idx);
}
print_with_delay("你出的牌:");
for (const Card& card : put_cards) print_with_delay(card.rank + " ");
print_with_delay("/n");
}
} else {
// 机器人出牌逻辑:先决定是否出牌,再选牌
bool will_play = std::uniform_int_distribution<int>(0, 99)(g_rng) < 80; // 80%概率出牌
if (will_play && !current_player.hand_cards.empty()) {
put_cards = robot_select_play_cards(player_idx, current_player.hand_cards, target_rank, is_robot_lie);
put_count = put_cards.size();
} else {
put_count = 0;
}
print_with_delay(current_player.name + " 选择" + (put_count == 0 ? "不出牌(0)/n" : "出%d张牌/n"), {put_count});
if (put_count > 0) {
print_with_delay(current_player.name + " 出的牌:");
bool is_teammate = (human_player.team_id == current_player.team_id) && (human_player.team_id != 0);
if (g_cheatMode || is_teammate || current_player.is_human) {
for (const Card& card : put_cards) print_with_delay(card.rank + " ");
} else {
print_with_delay("%d张(暗扣)", {put_count});
}
print_with_delay("/n");
}
}
if (put_count == 0) {
current_player.pass_this_round = true; // 标记为本轮不出牌,但仍可质疑
continue;
}
all_pass = false;
last_play_idx = player_idx;
// 加入牌池
pool_cards.insert(pool_cards.end(), put_cards.begin(), put_cards.end());
// 质疑环节 - 关键修改:移除对pass_this_round的判断
bool is_challenged = false;
int challenger_idx = -1;
std::vector<bool> robotChallengedTarget(players.size(), false);
print_with_delay("\n----- 质疑环节 -----/n");
for (int j = 0; j < (int)players.size(); ++j) {
// 仅跳过出牌者本人,不跳过不出牌的玩家
if (j == player_idx) continue;
bool is_teammate = (players[j].team_id == current_player.team_id) && (players[j].team_id != 0);
if (is_teammate) {
print_with_delay(players[j].name + " 是队友,跳过质疑!/n");
continue;
}
bool challenge = false;
if (players[j].is_human) {
challenge = (safe_read_int("是否质疑" + current_player.name + "?(1=是,0=否):", 0, 1) == 1);
} else {
// 机器人质疑逻辑:根据性格调整
challenge = robot_challenge_liar_by_personality(j, player_idx, put_count, (int)pool_cards.size());
print_with_delay(players[j].name + (challenge ? " 选择质疑!/n" : " 选择不质疑。/n"));
}
if (challenge) {
is_challenged = true;
challenger_idx = j;
if (!players[j].is_human) robotChallengedTarget[j] = true;
break;
}
}
// 放松警惕逻辑
for (int r = 0; r < (int)players.size(); ++r) {
if (r == player_idx || players[r].is_human) continue;
bool teammate = (players[r].team_id == current_player.team_id) && (players[r].team_id != 0);
if (teammate) continue;
if (!robotChallengedTarget[r]) {
g_noChallengeTicks[r][player_idx] += 1;
relax_to_neutral_if_needed(r, player_idx);
} else {
g_noChallengeTicks[r][player_idx] = 0;
}
}
// 开牌判定
if (is_challenged) {
Player& challenger = players[challenger_idx];
print_with_delay("\n----- 开牌判定 -----/n");
print_with_delay(current_player.name + " 实际出的牌:");
for (const Card& card : put_cards) print_with_delay(card.rank + " ");
print_with_delay("/n");
bool is_liar = false;
for (const Card& card : put_cards) {
if (!card.is_joker && card.rank != original_target) {
is_liar = true;
break;
}
}
// 更新信任度和机器人状态
if (!players[challenger_idx].is_human) {
update_trust_after_reveal(challenger_idx, player_idx, !is_liar, put_count, true);
g_noChallengeTicks[challenger_idx][player_idx] = 0;
}
for (int r = 0; r < (int)players.size(); ++r) {
if (r == player_idx || r == challenger_idx || players[r].is_human) continue;
bool teammate = (players[r].team_id == current_player.team_id) && (players[r].team_id != 0);
if (teammate) continue;
update_trust_after_reveal(r, player_idx, !is_liar, put_count, false);
}
// 判定结果
if (is_liar) {
print_with_delay("判定:" + current_player.name + " 骗人了!拿走牌池所有牌!/n");
current_player.hand_cards.insert(current_player.hand_cards.end(), pool_cards.begin(), pool_cards.end());
sort_hand_cards(current_player.hand_cards);
auto challenger_it = std::find(play_order.begin(), play_order.end(), challenger_idx);
current_order_idx = (challenger_it != play_order.end()) ? (int)(challenger_it - play_order.begin()) : 0;
} else {
print_with_delay("判定:" + current_player.name + " 没骗人!" + challenger.name + " 拿走牌池所有牌!/n");
challenger.hand_cards.insert(challenger.hand_cards.end(), pool_cards.begin(), pool_cards.end());
sort_hand_cards(challenger.hand_cards);
auto player_it = std::find(play_order.begin(), play_order.end(), player_idx);
current_order_idx = (player_it != play_order.end()) ? (int)(player_it - play_order.begin()) : 0;
}
pool_cards.clear();
// 新轮次:重新分配机器人性格
assign_robot_personalities(g_robot_states);
int new_first_idx = play_order[current_order_idx];
print_with_delay("\n===== 新轮次 =====/n");
if (players[new_first_idx].is_human) {
print_with_delay(players[new_first_idx].name + " 请宣称本轮点数:");
std::cin >> target_rank;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
// 机器人选择宣称点数:优先选自己手牌中数量最多的点数
target_rank = find_most_rank_in_hand(players[new_first_idx].hand_cards);
print_with_delay(players[new_first_idx].name + " 宣称点数:" + target_rank + "/n");
}
original_target = target_rank;
print_with_delay("本轮宣称点数:" + target_rank + "(大小王为万能牌)/n");
break;
}
}
// 所有人都不出牌,结束本轮
if (all_pass) {
print_with_delay("\n===== 本轮所有人都不出牌,本轮结束!=====/n");
auto last_play_it = std::find(play_order.begin(), play_order.end(), last_play_idx);
current_order_idx = (last_play_it != play_order.end()) ? (int)(last_play_it - play_order.begin()) : 0;
// 重新分配机器人性格
assign_robot_personalities(g_robot_states);
int new_first_idx = play_order[current_order_idx];
print_with_delay(players[new_first_idx].name + " 请宣称本轮点数:");
if (players[new_first_idx].is_human) {
std::cin >> target_rank;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
// 机器人选择宣称点数:优先选自己手牌中数量最多的点数
target_rank = find_most_rank_in_hand(players[new_first_idx].hand_cards);
print_with_delay(players[new_first_idx].name + " 宣称点数:" + target_rank + "/n");
}
original_target = target_rank;
print_with_delay("本轮宣称点数:" + target_rank + "(大小王为万能牌)/n");
}
// 检查游戏结束
for (const Player& player : players) {
if (player.hand_cards.empty()) {
game_over = true;
print_with_delay("\n===== 游戏结束 =====/n");
if (is_team) {
print_with_delay("队%d 获胜!/n", {player.team_id});
} else {
print_with_delay(player.name + " 获胜!/n");
}
break;
}
}
// 显示当前手牌
if (!game_over) {
print_with_delay("\n===== 当前手牌 =====/n");
for (const Player& player : players) {
show_player_hand(human_player, player);
}
}
}
PAUSE_PROGRAM();
}
// 游戏设置(新增:调整输出速度)
void init_game_settings() {
CLEAR_SCREEN();
print_with_delay("===== 游戏全局设置 =====/n");
// 作弊模式设置
g_cheatMode = (safe_read_int("是否开启作弊模式?(1=是,0=否):", 0, 1) == 1);
print_with_delay(std::string("当前作弊模式:") + (g_cheatMode ? "开启" : "关闭") + "/n");
// 输出速度设置
g_print_delay_ms = safe_read_int(
"请设置文字输出延迟(毫秒,0=最快,50=适中,100=慢):",
0, 200
);
print_with_delay("当前输出延迟:%d 毫秒/n", {g_print_delay_ms});
}
// 主函数
int main() {
std::srand(static_cast<unsigned int>(std::time(nullptr)));
int menu_choice = -1;
while (menu_choice != 0) {
CLEAR_SCREEN();
print_with_delay("===== 纸牌游戏合集 =====/n");
print_with_delay("1. 斗地主/n");
print_with_delay("2. 骗子游戏/n");
print_with_delay("3. 设置/n");
print_with_delay("0. 退出/n");
print_with_delay(std::string("当前作弊模式:") + (g_cheatMode ? "开启" : "关闭") + "/n");
print_with_delay(std::string("当前输出延迟:") + std::to_string(g_print_delay_ms) + " 毫秒/n");
menu_choice = safe_read_int("请选择功能(0-3):", 0, 3);
switch (menu_choice) {
case 1:
print_with_delay("按下 任意键 开始游戏!/n");
_getch();
run_landlord_game();
break;
case 2:
print_with_delay("按下 任意键 开始游戏!/n");
_getch();
run_liar_game();
break;
case 3:
init_game_settings();
PAUSE_PROGRAM();
break;
case 0:
print_with_delay("退出游戏,再见!/n");
break;
default:
print_with_delay("选择无效,请重新输入!/n");
PAUSE_PROGRAM();
break;
}
}
return 0;
}
本人第一篇文章,点个赞再走吧!