#include<bits/stdc++.h> #include <windows.h> using namespace std;

// ====================== 全局结构体 ====================== // 角色配置:数量+名称 struct IDname { int cnt; string name; } roleCfg[100];

// 玩家信息 struct Player { int id; // 座位号 bool alive; // 是否存活 string role; // 身份 int know; // 玩家知晓信息:0未知/1好人狼人/2完整身份 int dieReason; // 死亡原因:0存活/1狼刀/2投票/3毒杀/4猎人枪 } player[21];

// 投票结构体 struct Vote { int voteCnt; // 得票数 int id; // 玩家编号 bool canVote; // 是否拥有本轮投票权 } voteArr[13];

// ====================== 全局变量 ====================== int n; // 总人数 int mySelf; // 玩家自身座位 int killWolf; // 狼人刀人目标 int killPoison; // 女巫毒人目标 char op; // 操作输入字符 bool hasCure = true; // 女巫解药 bool hasPoison = true; // 女巫毒药 int hunterId = 0; // 猎人座位 int guardId = 0; // 守卫座位 int guardTarget = 0; // 守卫当晚守护目标 bool hunterShot = false;// 猎人是否已经开枪 int randSeedArr[10] = {7,4,6,43,35,1,2,8,20,19};

// ====================== 函数声明 ====================== void initRoleBase(); void setRoleCount(int num); void randomAssignRole(int idx); bool cmpVoteDesc(Vote a, Vote b); bool cmpVoteIdAsc(Vote a, Vote b); void printGameUI(int day, bool isNight); void guardSleep(int day, bool isNight); void voteProcess(int day, bool isNight); bool checkGameOver(); void nightFirstRound(); void nightLoop(int day, bool isNight); void hunterShoot(); void printDeadNotice(); void showGameResult();

// ====================== 初始化角色基础名称 ====================== void initRoleBase() { roleCfg[1].name = "村民 "; roleCfg[2].name = "狼人 "; roleCfg[3].name = "女巫 "; roleCfg[4].name = "预言家 "; roleCfg[5].name = "猎人 "; roleCfg[6].name = "守卫 "; }

// ====================== 根据人数配置各角色数量 ====================== void setRoleCount(int num) { // 全部清零 for(int i = 1; i <= 6; i++) roleCfg[i].cnt = 0; switch(num) { case 6: roleCfg[1].cnt = 3; roleCfg[2].cnt = 2; break; case 7: roleCfg[1].cnt = 3; roleCfg[2].cnt = 2; roleCfg[3].cnt = 1; roleCfg[4].cnt = 1; break; case 8: roleCfg[1].cnt = 3; roleCfg[2].cnt = 3; roleCfg[3].cnt = 1; roleCfg[4].cnt = 1; break; case 9: roleCfg[1].cnt = 3; roleCfg[2].cnt = 3; roleCfg[3].cnt = 1; roleCfg[4].cnt = 1; roleCfg[5].cnt = 1; break; case 10: roleCfg[1].cnt = 4; roleCfg[2].cnt = 3; roleCfg[3].cnt = 1; roleCfg[4].cnt = 1; roleCfg[5].cnt = 1; break; case 11: roleCfg[1].cnt = 4; roleCfg[2].cnt = 4; roleCfg[3].cnt = 1; roleCfg[4].cnt = 1; roleCfg[5].cnt = 1; break; case 12: roleCfg[1].cnt = 4; roleCfg[2].cnt = 4; roleCfg[3].cnt = 1; roleCfg[4].cnt = 1; roleCfg[5].cnt = 1; roleCfg[6].cnt = 1; break; default: cout << "人数输入错误,程序退出!" << endl; system("pause"); exit(0); } }

// ====================== 随机分配单个玩家身份 ====================== void randomAssignRole(int idx) { srand((unsigned)time(0)); Sleep(rand() % 44); int base = 10000; int t = rand(); int y = randSeedArr[(rand() % 100 + t) % 10]; int selRole;

// 根据人数限定可选角色范围
if(n <= 6) selRole = abs(base * 6 / y) % 3 + 1;
else if(n <= 8) selRole = abs(base * 7 / y) % 4 + 1;
else if(n <= 11) selRole = abs(base * 8 / y) % 5 + 1;
else selRole = abs(base * 9 / y) % 6 + 1;

// 循环找到还有剩余名额的角色
while(true) {
    if(n <= 6) selRole = selRole % 3 + 1;
    else if(n <= 8) selRole = selRole % 4 + 1;
    else if(n <= 11) selRole = selRole % 5 + 1;
    else selRole = selRole % 6 + 1;

    if(roleCfg[selRole].cnt > 0) break;
}

// 赋值玩家信息
player[idx].role = roleCfg[selRole].name;
player[idx].alive = true;
player[idx].id = idx;
player[idx].know = 0;
player[idx].dieReason = 0;
roleCfg[selRole].cnt--;

// 记录猎人、守卫位置
if(player[idx].role == "猎人 ") hunterId = idx;
if(player[idx].role == "守卫 ") guardId = idx;

}

// ====================== 投票排序规则:得票降序,同票编号升序 ====================== bool cmpVoteDesc(Vote a, Vote b) { if(a.voteCnt != b.voteCnt) return a.voteCnt > b.voteCnt; return a.id < b.id; }

// 按编号升序 bool cmpVoteIdAsc(Vote a, Vote b) { return a.id < b.id; }

// ====================== 控制台UI打印(状态面板) ====================== void printGameUI(int day, bool isNight) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // 白天白底,夜晚黑底 if(isNight) SetConsoleTextAttribute(hConsole, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY); else SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

cout << "\t\t\t\t第" << day << "天 ";
if(isNight) cout << "夜晚" << endl;
else cout << "白天" << endl;
cout << "你的座位:" << mySelf << "号" << endl << endl;

// 1~6号玩家
cout << "1-6号位:";
for(int i = 1; i <= 6; i++) cout << player[i].id << "号\t";
cout << "\n存活状态:";
for(int i = 1; i <= 6; i++) {
    if(player[i].alive)
        SetConsoleTextAttribute(hConsole, isNight ? (BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|FOREGROUND_INTENSITY|FOREGROUND_GREEN) : (FOREGROUND_INTENSITY|FOREGROUND_GREEN));
    else
        SetConsoleTextAttribute(hConsole, isNight ? (BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|FOREGROUND_INTENSITY|FOREGROUND_RED) : (FOREGROUND_INTENSITY|FOREGROUND_RED));
    cout << (player[i].alive ? "存活\t" : "死亡\t");
}
SetConsoleTextAttribute(hConsole, isNight ? (BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|FOREGROUND_INTENSITY) : (FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE));
cout << "\n身份信息:";
for(int i = 1; i <= 6; i++) {
    if(player[i].know == 0) cout << "未知\t";
    else if(player[i].know == 1) cout << (player[i].role == "狼人 " ? "狼人\t" : "好人\t");
    else cout << player[i].role << "\t";
}
cout << "\n\n";

// 7~n号玩家
if(n >= 7) {
    cout << "7-" << n << "号位:";
    for(int i = 7; i <= n; i++) cout << player[i].id << "号\t";
    cout << "\n存活状态:";
    for(int i = 7; i <= n; i++) {
        if(player[i].alive)
            SetConsoleTextAttribute(hConsole, isNight ? (BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|FOREGROUND_INTENSITY|FOREGROUND_GREEN) : (FOREGROUND_INTENSITY|FOREGROUND_GREEN));
        else
            SetConsoleTextAttribute(hConsole, isNight ? (BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|FOREGROUND_INTENSITY|FOREGROUND_RED) : (FOREGROUND_INTENSITY|FOREGROUND_RED));
        cout << (player[i].alive ? "存活\t" : "死亡\t");
    }
    SetConsoleTextAttribute(hConsole, isNight ? (BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|FOREGROUND_INTENSITY) : (FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE));
    cout << "\n身份信息:";
    for(int i = 7; i <= n; i++) {
        if(player[i].know == 0) cout << "未知\t";
        else if(player[i].know == 1) cout << (player[i].role == "狼人 " ? "狼人\t" : "好人\t");
        else cout << player[i].role << "\t";
    }
    cout << "\n\n";
}

}

// ====================== 守卫夜间守护流程 ====================== void guardSleep(int day, bool isNight) { Sleep(3000); system("cls"); printGameUI(day, isNight); cout << "守~卫~请~睁~眼~~~\n"; Sleep(3000); system("cls"); printGameUI(day, isNight);

int tar;
// 玩家自己是守卫
if(mySelf == guardId && player[mySelf].alive) {
    cout << "请输入今晚要守护的玩家编号:";
    cin >> tar;
    while(tar == guardTarget || tar < 1 || tar > n || !player[tar].alive) {
        cout << "输入非法,请重新输入:";
        cin >> tar;
    }
    guardTarget = tar;
}
// AI守卫
else if(player[guardId].alive) {
    do {
        srand((unsigned)time(0));
        tar = rand() % n + 1;
    } while(tar == guardTarget || !player[tar].alive);
    guardTarget = tar;
}
// 无守卫/守卫已死
else guardTarget = -1;

Sleep(3000);
system("cls");
printGameUI(day, isNight);
cout << "守~卫~请~闭~眼~~~\n";

}

// ====================== 白天投票流程(最多三轮平票) ====================== void voteProcess(int day, bool isNight) { Sleep(2000); system("cls"); printGameUI(day, isNight); cout << "开始投票"; for(int i = 1; i <= 3; i++) { cout << "."; Sleep(500); } cout << endl;

// 初始化投票数据
for(int i = 1; i <= n; i++) {
    voteArr[i].id = i;
    voteArr[i].canVote = true;
    voteArr[i].voteCnt = 0;
}

// ========== 第一轮投票 ==========
for(int i = 1; i <= n; i++) {
    if(!player[i].alive) continue;
    Sleep(3000);
    int tar;
    if(i == mySelf) {
        cout << "请投票(输入0弃权):";
        cin >> tar;
        while(tar != 0 && !player[tar].alive) cin >> tar;
        if(tar == 0) cout << mySelf << "号弃权\n";
        else {
            cout << mySelf << "投给" << tar << "号\n";
            voteArr[tar].voteCnt++;
        }
    } else {
        srand((unsigned)time(0));
        // 狼人优先投好人
        if(player[i].role == "狼人 ") {
            do {
                tar = rand() % (n + 1);
            } while(tar != 0 && (!player[tar].alive || player[tar].role == "狼人 " || tar == i));
        }
        // 预言家优先投狼
        else if(player[i].role == "预言家 ") {
            do {
                tar = rand() % (n + 1);
            } while(tar != 0 && (!player[tar].alive || player[tar].role != "狼人 " || tar == i));
        }
        // 村民随机投
        else {
            do {
                tar = rand() % (n + 1);
            } while(tar != 0 && (!player[tar].alive || tar == i));
        }
        if(tar == 0) cout << i << "号弃权\n";
        else {
            cout << i << "投给" << tar << "号\n";
            voteArr[tar].voteCnt++;
        }
    }
}
Sleep(3000);
sort(voteArr + 1, voteArr + n + 1, cmpVoteDesc);
// 无平票直接出局
if(voteArr[1].voteCnt > voteArr[2].voteCnt) {
    int out = voteArr[1].id;
    cout << "投票结束," << out << "号被投票出局!\n";
    player[out].alive = false;
    player[out].dieReason = 2;
    Sleep(3000);
    return;
}
// 标记平票玩家本轮无投票权
int maxVote = voteArr[1].voteCnt;
for(int i = 1; i <= n; i++) {
    if(voteArr[i].voteCnt == maxVote) voteArr[i].canVote = false;
    else break;
}
system("cls");
printGameUI(day, isNight);
cout << "平票!进入第二轮投票\n";

// ========== 第二轮投票 ==========
sort(voteArr + 1, voteArr + n + 1, cmpVoteIdAsc);
cout << "二次投票";
for(int i = 1; i <= 3; i++) { cout << "."; Sleep(500); }
cout << endl;
for(int i = 1; i <= n; i++) {
    if(!player[i].alive || !voteArr[i].canVote) continue;
    Sleep(3000);
    int tar;
    if(i == mySelf) {
        cout << "请投票(0弃权):";
        cin >> tar;
        while(tar != 0 && (!player[tar].alive || !voteArr[tar].canVote)) cin >> tar;
        if(tar != 0) voteArr[tar].voteCnt++;
    } else {
        srand((unsigned)time(0));
        if(player[i].role == "狼人 ") {
            do { tar = rand() % (n + 1); } while(tar != 0 && (!player[tar].alive || player[tar].role == "狼人 " || tar == i || !voteArr[tar].canVote));
        } else if(player[i].role == "预言家 ") {
            do { tar = rand() % (n + 1); } while(tar != 0 && (!player[tar].alive || player[tar].role != "狼人 " || tar == i || !voteArr[tar].canVote));
        } else {
            do { tar = rand() % (n + 1); } while(tar != 0 && (!player[tar].alive || tar == i || !voteArr[tar].canVote));
        }
        if(tar != 0) voteArr[tar].voteCnt++;
    }
}
Sleep(3000);
sort(voteArr + 1, voteArr + n + 1, cmpVoteDesc);
if(voteArr[1].voteCnt > voteArr[2].voteCnt) {
    int out = voteArr[1].id;
    cout << "投票结束," << out << "号被投票出局!\n";
    player[out].alive = false;
    player[out].dieReason = 2;
    Sleep(3000);
    return;
}
// 再次标记平票
maxVote = voteArr[1].voteCnt;
for(int i = 1; i <= n; i++) {
    if(voteArr[i].voteCnt == maxVote) voteArr[i].canVote = false;
    else break;
}
system("cls");
printGameUI(day, isNight);
cout << "再次平票!第三轮投票\n";

// ========== 第三轮投票 ==========
sort(voteArr + 1, voteArr + n + 1, cmpVoteIdAsc);
cout << "最终投票";
for(int i = 1; i <= 3; i++) { cout << "."; Sleep(500); }
cout << endl;
for(int i = 1; i <= n; i++) {
    if(!player[i].alive || !voteArr[i].canVote) continue;
    Sleep(3000);
    int tar;
    if(i == mySelf) {
        cout << "请投票(0弃权):";
        cin >> tar;
        while(tar != 0 && (!player[tar].alive || !voteArr[tar].canVote)) cin >> tar;
        if(tar != 0) voteArr[tar].voteCnt++;
    } else {
        srand((unsigned)time(0));
        if(player[i].role == "狼人 ") {
            do { tar = rand() % (n + 1); } while(tar != 0 && (!player[tar].alive || player[tar].role == "狼人 " || tar == i || !voteArr[tar].canVote));
        } else if(player[i].role == "预言家 " || player[i].role == "猎人 ") {
            do { tar = rand() % (n + 1); } while(tar != 0 && (!player[tar].alive || player[tar].role != "狼人 " || tar == i || !voteArr[tar].canVote));
        } else {
            do { tar = rand() % (n + 1); } while(tar != 0 && (!player[tar].alive || tar == i || !voteArr[tar].canVote));
        }
        if(tar != 0) voteArr[tar].voteCnt++;
    }
}
Sleep(3000);
sort(voteArr + 1, voteArr + n + 1, cmpVoteDesc);
if(voteArr[1].voteCnt > voteArr[2].voteCnt) {
    int out = voteArr[1].id;
    cout << "投票结束," << out << "号被投票出局!\n";
    player[out].alive = false;
    player[out].dieReason = 2;
} else {
    cout << "三轮投票全部平票,无人出局\n";
}
Sleep(5000);

}

// ====================== 判断游戏是否结束 ====================== bool checkGameOver() { int villager = 0; // 存活村民 int wolf = 0; // 存活狼人 int god = 0; // 存活神(女巫/预言/猎人/守卫) for(int i = 1; i <= n; i++) { if(!player[i].alive) continue; if(player[i].role == "狼人 ") wolf++; else if(player[i].role == "村民 ") villager++; else god++; } // 好人全灭 / 狼人数量>=好人直接胜利 if(god == 0 || wolf == 0 || wolf >= villager + god) return true; return false; }

// ====================== 第一晚夜晚流程(无前日数据) ====================== void nightFirstRound() { system("cls"); system("color 0f"); printGameUI(1, true); cout << "天~黑~请~闭~眼~~~\n"; guardTarget = 0; if(n >= 12) guardSleep(1, true); Sleep(3000);

// 狼人行动
system("cls");
printGameUI(1, true);
cout << "狼~人~请~睁~眼~~~\n";
if(player[mySelf].role == "狼人 ") {
    Sleep(1000);
    cout << "你的狼队友:";
    for(int i = 1; i <= n; i++) {
        if(i == mySelf) continue;
        if(player[i].role == "狼人 ") {
            cout << i << "号 ";
            player[i].know = 2;
        }
    }
    Sleep(3000);
    cout << "\n输入今晚刀人目标:";
    cin >> killWolf;
    system("cls");
    printGameUI(1, true);
    cout << "今晚击杀目标:" << killWolf << "号\n";
} else {
    Sleep(4000);
    // AI狼人随机刀好人
    do {
        srand((unsigned)time(0));
        killWolf = rand() % n + 1;
    } while(player[killWolf].role == "狼人 " || !player[killWolf].alive);
    Sleep(5000);
}
Sleep(3000);
system("cls");
printGameUI(1, true);
cout << "狼~人~请~闭~眼~~~\n";
Sleep(2000);

// 女巫行动
system("cls");
printGameUI(1, true);
cout << "女~巫~请~睁~眼~~~\n";
Sleep(2000);
system("cls");
printGameUI(1, true);
killPoison = 0;
int witchId = 0;
for(int i = 1; i <= n; i++) if(player[i].role == "女巫 ") witchId = i;

if(player[mySelf].role == "女巫 " && player[mySelf].alive) {
    Sleep(1000);
    if(hasCure) {
        cout << "今晚" << killWolf << "号中刀\n是否使用解药?A救/B不救:";
        cin >> op;
        if(op == 'A') {
            cout << "你解救了" << killWolf << "号\n";
            hasCure = false;
            if(guardTarget != killWolf) killWolf = 0;
            // 询问毒药
            Sleep(2000);
            system("cls");
            printGameUI(1, true);
            cout << "是否使用毒药?A毒/B不毒:";
            cin >> op;
            if(op == 'A' && hasPoison) {
                cout << "输入毒杀目标:";
                cin >> killPoison;
                while(!player[killPoison].alive) { cin >> killPoison; }
                hasPoison = false;
            }
        } else {
            if(guardTarget == killWolf) killWolf = 0;
            Sleep(2000);
            system("cls");
            printGameUI(1, true);
            cout << "是否使用毒药?A毒/B不毒:";
            cin >> op;
            if(op == 'A' && hasPoison) {
                cout << "输入毒杀目标:";
                cin >> killPoison;
                while(!player[killPoison].alive) { cin >> killPoison; }
                hasPoison = false;
            }
        }
    } else {
        if(guardTarget == killWolf) killWolf = 0;
        Sleep(2000);
        system("cls");
        printGameUI(1, true);
        cout << "是否使用毒药?A毒/B不毒:";
        cin >> op;
        if(op == 'A' && hasPoison) {
            cout << "输入毒杀目标:";
            cin >> killPoison;
            while(!player[killPoison].alive) { cin >> killPoison; }
            hasPoison = false;
        }
    }
} else {
    // AI女巫逻辑
    bool useCure = false;
    if(hasCure && player[witchId].alive) {
        // 优先救神
        if(player[killWolf].role == "预言家 " || player[killWolf].role == "女巫 " || player[killWolf].role == "猎人 ") {
            if(guardTarget != killWolf) {
                killWolf = 0;
                hasCure = false;
                useCure = true;
            }
        }
    }
    // 有药随机毒好人
    if(!useCure && hasPoison && player[witchId].alive) {
        srand((unsigned)time(0));
        if(rand() % 2 == 1) {
            do {
                killPoison = rand() % n + 1;
            } while(player[killPoison].role == "女巫 " || player[killPoison].role == "预言家 " || killPoison == killWolf || !player[killPoison].alive);
            hasPoison = false;
        }
    }
}
Sleep(3000);
system("cls");
printGameUI(1, true);
cout << "女~巫~请~闭~眼~~~\n";

// 预言家行动(6人局无预言家)
if(n > 6) {
    Sleep(3000);
    system("cls");
    printGameUI(1, true);
    cout << "预~言~家~请~睁~眼~~~\n";
    if(player[mySelf].role == "预言家 ") {
        Sleep(3000);
        int check;
        cout << "输入查验玩家编号:";
        cin >> check;
        player[check].know = 1;
        Sleep(2000);
        system("cls");
        printGameUI(1, true);
        cout << "该玩家身份:" << (player[check].role == "狼人 " ? "狼人" : "好人") << "\n";
    } else {
        Sleep(3000);
        cout << "AI预言家查验中...\n";
    }
    Sleep(3000);
    system("cls");
    printGameUI(1, true);
    cout << "预~言~家~请~闭~眼~~~\n";
}
Sleep(3000);

// 结算死亡
if(killWolf != 0) {
    player[killWolf].alive = false;
    player[killWolf].dieReason = 1;
}
if(killPoison != 0) {
    player[killPoison].alive = false;
    player[killPoison].dieReason = 3;
}
system("cls");
system("color F0");
printGameUI(2, false);

}

// ====================== 通用夜晚流程(第二晚及以后) ====================== void nightLoop(int day, bool isNight) { system("cls"); system("color 0f"); printGameUI(day, true); cout << "天~黑~请~闭~眼~~~\n"; guardTarget = 0; if(n >= 12) guardSleep(day, true); Sleep(3000);

// 狼人刀人
system("cls");
printGameUI(day, true);
cout << "狼~人~请~睁~眼~~~\n";
if(player[mySelf].role == "狼人 " && player[mySelf].alive) {
    Sleep(3000);
    cout << "输入刀人目标:";
    cin >> killWolf;
    system("cls");
    printGameUI(day, true);
    cout << "今晚击杀:" << killWolf << "号\n";
} else {
    do {
        srand((unsigned)time(0));
        killWolf = rand() % n + 1;
    } while(player[killWolf].role == "狼人 " || !player[killWolf].alive);
    Sleep(5000);
}
Sleep(3000);
system("cls");
printGameUI(day, true);
cout << "狼~人~请~闭~眼~~~\n";
Sleep(2000);

// 女巫
system("cls");
printGameUI(day, true);
cout << "女~巫~请~睁~眼~~~\n";
Sleep(2000);
system("cls");
printGameUI(day, true);
killPoison = 0;
int witchId = 0;
for(int i = 1; i <= n; i++) if(player[i].role == "女巫 ") witchId = i;

if(player[mySelf].role == "女巫 " && player[mySelf].alive) {
    if(hasCure) {
        cout << killWolf << "号中刀,解药A救/B不救:";
        cin >> op;
        if(op == 'A') {
            hasCure = false;
            if(guardTarget != killWolf) killWolf = 0;
        } else {
            if(guardTarget == killWolf) killWolf = 0;
        }
    } else {
        if(guardTarget == killWolf) killWolf = 0;
    }
    if(hasPoison) {
        cout << "是否毒人?A毒/B不毒:";
        cin >> op;
        if(op == 'A') {
            cout << "输入毒目标:";
            cin >> killPoison;
            while(!player[killPoison].alive) cin >> killPoison;
            hasPoison = false;
        }
    }
} else {
    bool useCure = false;
    if(hasCure && player[witchId].alive) {
        if(player[killWolf].role == "预言家 " || player[killWolf].role == "猎人 ") {
            if(guardTarget != killWolf) {
                killWolf = 0;
                hasCure = false;
                useCure = true;
            }
        }
    }
    if(!useCure && hasPoison && player[witchId].alive) {
        srand((unsigned)time(0));
        if(rand() % 2 == 1) {
            do {
                killPoison = rand() % n + 1;
            } while(player[killPoison].role == "女巫 " || player[killPoison].role == "预言家 " || killPoison == killWolf || !player[killPoison].alive);
            hasPoison = false;
        }
    }
}
Sleep(3000);
system("cls");
printGameUI(day, true);
cout << "女~巫~请~闭~眼~~~\n";

// 预言家
if(n > 6) {
    Sleep(3000);
    system("cls");
    printGameUI(day, true);
    cout << "预~言~家~请~睁~眼~~~\n";
    if(player[mySelf].role == "预言家 " && player[mySelf].alive) {
        int check;
        cout << "查验编号:";
        cin >> check;
        player[check].know = 1;
        cout << "身份:" << (player[check].role == "狼人 " ? "狼人" : "好人") << "\n";
    } else {
        cout << "AI预言家查验\n";
    }
    Sleep(3000);
    system("cls");
    printGameUI(day, true);
    cout << "预~言~家~请~闭~眼~~~\n";
}
Sleep(3000);

// 结算死亡
if(killWolf != 0) {
    player[killWolf].alive = false;
    player[killWolf].dieReason = 1;
}
if(killPoison != 0) {
    player[killPoison].alive = false;
    player[killPoison].dieReason = 3;
}
system("cls");
system("color F0");
printGameUI(day + 1, false);

}

// ====================== 猎人开枪 ====================== void hunterShoot() { if(hunterShot) return; hunterShot = true; int shootTar; if(mySelf == hunterId) { cout << "你被淘汰,请选择开枪带走一人:"; cin >> shootTar; while(!player[shootTar].alive) { cin >> shootTar; } } else { srand((unsigned)time(0)); do { shootTar = rand() % n + 1; } while(!player[shootTar].alive); } cout << hunterId << "号猎人开枪带走" << shootTar << "号\n"; player[shootTar].alive = false; player[shootTar].dieReason = 4; Sleep(1000); }

// ====================== 天亮死亡播报 ====================== void printDeadNotice() { cout << "天亮了!昨晚"; if(killWolf == 0 && killPoison == 0) { cout << "平安夜\n"; return; } if(killWolf != 0) cout << killWolf << "号"; if(killPoison != 0) cout << "," << killPoison << "号"; cout << "玩家死亡\n"; killPoison = 0; }

// ====================== 游戏结束展示全部身份与结局 ====================== void showGameResult() { int villager = 0, wolf = 0, god = 0; for(int i = 1; i <= n; i++) { if(!player[i].alive) continue; if(player[i].role == "狼人 ") wolf++; else if(player[i].role == "村民 ") villager++; else god++; } if(wolf >= villager + god || god == 0) cout << "===== 狼人阵营胜利 =" << endl; else cout << "= 好人阵营胜利 =====" << endl; cout << "\n全部玩家身份详情:\n"; cout << left << setw(4) << "座位" << setw(8) << "身份" << setw(6) << "状态" << "死亡原因\n"; for(int i = 1; i <= n; i++) { cout << left << setw(4) << player[i].id << setw(8) << player[i].role; if(player[i].alive) cout << setw(6) << "存活" << "全程存活\n"; else { cout << setw(6) << "死亡"; switch(player[i].dieReason) { case 1: cout << "狼人刀杀\n"; break; case 2: cout << "投票放逐\n"; break; case 3: cout << "女巫毒杀\n"; break; case 4: cout << "猎人枪杀\n"; break; } } } system("pause"); system("pause"); }

// ====================== 主函数游戏循环 ====================== int main() { system("cls"); cout << "========== 控制台狼人杀 ==========\n"; cout << "请输入游玩人数(6~12):"; cin >> n; cout << "身份分配加载中,请稍候"; initRoleBase(); setRoleCount(n);

// 随机分配所有玩家身份
for(int i = 1; i <= n; i++) {
    randomAssignRole(i);
    cout << ".";
    Sleep(17);
}
system("cls");
system("color F0");
cout << "游戏即将开始";
for(int i = 1; i <= 6; i++) { cout << "."; Sleep(500); }
Sleep(1500);

// 分配玩家自身身份
cout << "\n\n查看你的身份牌......\n";
srand((unsigned)time(0));
mySelf = rand() % n + 1;
cout << "你的身份:" << player[mySelf].role << "\n座位:" << mySelf << "号\n";
system("pause");
system("cls");
player[mySelf].know = 2;
printGameUI(1, false);
cout << "即将进入第一个夜晚";
for(int i = 1; i <= 6; i++) { cout << "."; Sleep(500); }

// 第一晚
nightFirstRound();
printDeadNotice();
if(checkGameOver()) { Sleep(1000); system("cls"); showGameResult(); return 0; }
// 猎人出局开枪
if(!player[hunterId].alive && !hunterShot) hunterShoot();
if(checkGameOver()) { Sleep(1000); system("cls"); showGameResult(); return 0; }
// 白天投票
voteProcess(2, false);
system("cls");
printGameUI(2, false);
if(checkGameOver()) { Sleep(1000); system("cls"); showGameResult(); return 0; }
if(!player[hunterId].alive && !hunterShot) hunterShoot();

// 循环多日夜(2~7天)
for(int day = 2; day <= 7; day++) {
    cout << "即将进入夜晚";
    for(int i = 1; i <= 6; i++) { cout << "."; Sleep(500); }
    nightLoop(day, true);
    printDeadNotice();
    if(checkGameOver()) { Sleep(1000); system("cls"); showGameResult(); return 0; }
    if(!player[hunterId].alive && !hunterShot) hunterShoot();
    if(checkGameOver()) { Sleep(1000); system("cls"); showGameResult(); return 0; }
    voteProcess(day + 1, false);
    system("cls");
    printGameUI(day + 1, false);
    if(checkGameOver()) { Sleep(1000); system("cls"); showGameResult(); return 0; }
    if(!player[hunterId].alive && !hunterShot) hunterShoot();
    if(checkGameOver()) { Sleep(1000); system("cls"); showGameResult(); return 0; }
}

system("pause");
return 0;

}

1 条评论

  • ?? 用我的 #include<bits/stdc++.h> #include<windows.h> #include<conio.h> using namespace std; const int daytime=0,night=1; int day=0, during_time=daytime, player_number, my_number; HWND hwnd=GetForegroundWindow(); const int blue=0,yellow=1,red=2,green=3,purple=4,white=5;//颜色常量 void color(int c){ switch(c){ case red:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);break; case green:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);break; case yellow:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN);break; case blue:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);break; case white:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN | FOREGROUND_BLUE);break; case purple:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);break; } } int idx_police=-1; void gotoxy(int x,int y){ COORD position; position.X=x; position.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); } void init_Show_Window(){ system("mode con lines=60 cols=188"); ShowWindow(hwnd,SW_MAXIMIZE);//窗口最大化 DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow());//删除×字符 }

    /玩家类/ const int nvwu=0,cunmin=1,yuyanjia=2,langren=3,lieren=4,shouwei=5,good=6,die=1,life=2; class player{ public: int type; int die_or_life; int how(){ return die_or_life; } int is_light;//是否已经公布 int killer; }; player players[1000];

    /转换白天模式/ void change_daytime(){ during_time=daytime; day++; }

    /转换黑夜模式/ void change_night(){ during_time=night; } int nnvwu=0,ncunmin=0,nyuyanjia=0,nlangren=0,nlieren=0,nshouwei=0; int idxnvwu,idxshouwei,idxyuyanjia,idxlieren,idxlangren[4]={-1,-1,-1,-1};

    /b是否在Arr中/ bool is_include(int arr[],int b,int l){ for(int i=0;i<l;i++){ if(arr[i]==b) return true; } return false; }

    /初始化人数/ void init_players(){ my_number=rand()%player_number; if(player_number12) nlangren=4; else if(player_number>=10) nlangren=3; else nlangren=2; for(int i=0;i<player_number;i++) { players[i].die_or_life=life; players[i].is_light=0; players[i].type=-1; players[i].killer=2147483647; } for(int i=0;i<nlangren;i++) { int p=rand()%player_number; if(!is_include(idxlangren,p,4)) idxlangren[i]=p,players[p].type=langren; else i--; Sleep(rand()%80+100); } if(player_number12) { do{ idxshouwei=rand()%player_number; }while(players[idxshouwei].type!=-1);
    players[idxshouwei].type=shouwei;
    } do{ idxnvwu=rand()%player_number; }while(players[idxnvwu].type!=-1); players[idxnvwu].type=nvwu; if(player_number>=10) { do{ idxlieren=rand()%player_number; }while(players[idxlieren].type!=-1); players[idxlieren].type=lieren;
    } do{ idxyuyanjia=rand()%player_number; }while(players[idxyuyanjia].type!=-1); players[idxyuyanjia].type=yuyanjia; for(int i=0;i<player_number;i++) if(players[i].type==-1) players[i].type=cunmin, ncunmin++; if(players[my_number].type==langren) { for(int i=0;i<nlangren;i++) { players[idxlangren[i]].is_light=1; } } players[my_number].is_light=1; }

    /在屏幕上打印东西/ void print(){ gotoxy(0,0); cout<<"作者:洛谷393864"; gotoxy(90,0); if(during_timenight) color(red); else color(blue); printf("第%d天 | ",day); if(during_timenight) cout<<"黑夜"; else cout<<"白天";

    gotoxy(0,3);
    color(blue);
    cout<<" 我的号位:"<<my_number+1;
    for(int i=0;i<player_number;i++){
    	gotoxy(i*8+1,4);
    	if(i==idx_police) color(yellow);
    	else color(blue);
    	cout<<i+1<<"号位"; 
    	gotoxy(i*8+1,5);
    	if(players[i].how()==die){
    		color(red);
    		cout<<"死 亡";
    	}else{
    		color(green);
    		cout<<"存 活";
    	}
    	gotoxy(i*8+1,6);
    	color(blue);
    	if(players[i].is_light){
    		if(players[i].is_light==1){
    			switch(players[i].type){
    				case nvwu: cout<<"女 巫";break;
    				case yuyanjia: cout<<"\b预言家";break;
    				case cunmin: cout<<"村 民";break;
    				case langren:cout<<"狼 人"; break;
    				case lieren:cout<<"猎 人"; break; 
    				case shouwei:cout<<"守 卫"; break;
    			}   
    		}else{
    			cout<<"好人";
    		}
    		
    	}else{
    		cout<<"未知";
    	}
    }
    

    }

    /判断是否结束,没结束返回0 好人胜利返回1 狼人胜利返回2 平局返回3/ int is_end(){ int die_bad=0; int die_people=0; int die_god=0; for(int i=0;i<player_number;i++){ if((players[i].type == nvwu || players[i].type == yuyanjia || players[i].type == shouwei)&&players[i].die_or_lifedie) die_god++; else if(players[i].type == langren && players[i].die_or_lifedie) die_bad++; else if(players[i].type == cunmin && players[i].die_or_lifedie) die_people++; } if((die_baddie_people || die_bad==die_god)&&(die_bad>=nlangren)) return 3; if(die_bad>=nlangren) return 1; if(die_people>=ncunmin||die_god>=(player_number>=10 ? 3:2)) return 2; return 0; }

    /游戏开始前的骚操作/ void before_game(){ srand(time(NULL)); init_Show_Window(); color(green); cout<<"欢迎来到狼人杀游戏\t\t\t为了更好的游戏体验,请右键点击上方↑↑,点击"属性",点击"字体"栏目,将字体修改为宋体或新宋体,将字号改为20\n作者:洛谷393864\n请勿私自转载,违者依法追究法律责任 注:10 11 12人局开设猎人 12人局开设守卫警长\n______________________\n"; cout<<"请输入玩家人数(8-12人):"; cin>>player_number; while(player_number<8||player_number>12) { cout<<"请重新输入!\n"; cin>>player_number; } system("cls"); cout<<"初始化身份中,请稍等."; for(int i=0;i<6;i++){ for(int j=0;j<12;j++){ cout<<"."; Sleep(50); }
    cout<<"\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"; } system("cls");

    init_players();
    cout<<"我的号位:"<<my_number+1<<endl
    <<"我的身份:";
    switch(players[my_number].type){
    	case nvwu: cout<<"女巫\n";break;
    	case yuyanjia: cout<<"预言家\n";break;
    	case cunmin: cout<<"村民\n";break;
    	case langren:cout<<"狼人\n";break;  
    	case lieren:cout<<"猎人\n"; break;  
    	case shouwei:cout<<"守卫\n";break;
    }
    change_daytime();
    system("pause");
    system("cls");
    cout<<"游戏加载中.";int ppppp=rand()%3+2;
    for(int i=0;i<ppppp;i++){
    	for(int j=0;j<6;j++){
    		cout<<".";
    		Sleep(rand()%100+150);
    	}   
    	cout<<"\b\b\b\b\b\b      \b\b\b\b\b\b";
    }   
    print();
    

    }

    /每一天开始前的操作/ void something_before_everyday(){ change_night(); system("cls"); print(); gotoxy(0,7); cout<<"________________________"; gotoxy(0,8); color(white); cout<<"天黑~请闭眼~~~\n"; }

    /守卫操作/ int shouweishou=0; int ShouWei(){ color(blue); cout<<"守卫~请睁眼~~\n"; Sleep(1500); cout<<"你要守护的是?\n"; if(players[my_number].type==shouwei&&players[my_number].die_or_life == life){ cin>>shouweishou; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ cout<<"请重新输入!\n"; cin>>shouweishou; } cout<<"你今晚要守护的是"<<shouweishou<<"号\n"; Sleep(1500); shouweishou--; }else{ if(players[idxshouwei].die_or_life == life){ shouweishou=rand()%10; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ shouweishou=rand()%10; } } }
    Sleep(2000); cout<<"守卫请闭眼"<<endl<<endl; return shouweishou; }

    /狼人操作/ int LangRen(){ int langrensha=-1; color(red); cout<<"狼人~请睁眼~~~\n"; Sleep(1500); cout<<"你们今晚要杀~谁~~??\n"; if(players[my_number].typelangren&&players[my_number].die_or_life == life){ cin>>langrensha; while(!(langrensha>=1&&langrensha<=player_number&&players[langrensha-1].die_or_lifelife)){ cout<<"请重新输入!\n"; cin>>langrensha; } cout<<"你们今晚要杀的是"<<langrensha--<<"号\n"; Sleep(3500); }else{ while(langrensha==-1 || players[langrensha].die_or_life == die || players[langrensha].type==langren){ langrensha=rand()%player_number; } Sleep(3000); } cout<<"狼人请~闭眼~~\n\n"; return langrensha; }

    /女巫操作/ int nvwujiu=0,nvwudu=0,is_nvwujiu=0,is_nvwudu=0; int NvWu(int langrensha){ color(purple); cout<<"女巫请睁眼\n"; Sleep(2000); if(players[my_number].typenvwu&&players[my_number].die_or_life == life){ if(is_nvwujiu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"今晚"<<langrensha+1<<"号死了,你想用解药吗?(1想 / 2不想)\n"; int is_nvwujie=0; cin>>is_nvwujie; while(is_nvwujie!=1&&is_nvwujie!=2){ cout<<"请重新输入\n"; cin>>is_nvwujie; } if(is_nvwujie1) { Sleep(1000); cout<<"已经解救"<<langrensha+1<<"号\n"; nvwujiu=langrensha; } is_nvwujiu=1; } Sleep(1500); if(::is_nvwudu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"你想用毒药吗?(1想 / 2不想)\n"; Sleep(1500); int is_nvwudu=0; cin>>is_nvwudu; while(is_nvwudu!=1&&is_nvwudu!=2){ cout<<"请重新输入\n"; cin>>is_nvwudu; }
    if(is_nvwudu1){ Sleep(1500); cout<<"你想毒谁?\n"; cin>>nvwudu; while(!(nvwudu>=1&&nvwudu<=player_number&&players[nvwudu].die_or_lifelife)){ cout<<"请重新输入\n"; cin>>nvwudu; } nvwudu--; Sleep(1500); cout<<"已经毒死了"<<nvwudu+1<<"号\n"; } ::is_nvwudu=1; }
    }else{ if(players[idxnvwu].die_or_life == life){ if(!is_nvwujiu) { int is_jiu=rand()%8; if(is_jiu0){ nvwujiu=langrensha; is_nvwujiu=1; }
    } if(!is_nvwudu) { int is_du=rand()%4; if(is_du
    0){ int num=rand()%player_number; nvwudu=num; is_nvwudu=1; } } }

    } 
    cout<<"女巫~请闭眼~~\n\n";
    return nvwujiu*10000+nvwudu;//传回两个变量,“加密”操作 
    

    }

    int yuyanjiabixutoupiao=-1; /预言家操作/ void YuYanJia(){ color(green); cout<<"预言家~请睁眼~~\n"; Sleep(2000); if(players[my_number].typeyuyanjia&&players[my_number].die_or_life == life){ cout<<"请问你想查验谁的身份\n"; int p; cin>>p; while(!(p>=1&&p<=player_number)){ cout<<"请重新输入!\n"; cin>>p; } Sleep(2000); cout<<p<<"号的身份是——"; Sleep(1000); if(players[p-1].type == langren){ cout<<"狼人\n"; players[p-1].is_light = 1; }else{ cout<<"好人\n"; players[p-1].is_light = 2; } }else{ int p=-1; while(p-1||players[p].die_or_lifedie||pidxlieren) p=rand()%player_number; if(players[p].type==langren)//锁定目标! { yuyanjiabixutoupiao=p; } } cout<<"预言家请闭眼\n"; }

    /黑夜操作/ int LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1; void Night(){
    LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1;

    //如果有12人局,添加守卫 
    if(player_number==12){
    	SHOUWEISHOU=ShouWei();
    	Sleep(2000);
    } 
    /*狼人部分*/
    LANGRENSHA=LangRen();
    Sleep(3500);
    /*女巫部分*/ 
    int nvwu=NvWu(LANGRENSHA);
    NVWUDU=nvwu%10+nvwu/10%10;
    NVWUJIU=nvwu/10000%10+nvwu/100000%10;
    Sleep(3000);
    /*预言家部分*/
    YuYanJia();
    Sleep(2000);
    

    }

    /猎人操作/ void Lieren(){ int lierendai=-1; cout<<idxlieren+1<<"号是猎人\n";

    players[idxlieren].is_light = 1;
    Sleep(1000);
    if(idxlieren==my_number){
    	cout<<"你想带走几号?\n";
    	cin>>lierendai;
    	while(lierendai<1||lierendai>player_number||players[lierendai].die_or_life==die){
    		cout<<"请重新输入!\n";
    		cin>>lierendai;
    	}
    	lierendai--;
    }else{
    	lierendai=rand()%player_number;
    	while(players[lierendai].die_or_life == die){
    		lierendai=rand()%player_number;
    	}
    }
    Sleep(2000);
    cout<<"猎人选择带走"<<lierendai+1<<"号\n";
    Sleep(2000);
    players[lierendai].die_or_life = die;
    

    }

    void police_die(); /判断谁死了/ void panduansiwang(){ system("cls"); print(); gotoxy(0,7); cout<<"________________________\n"; Sleep(3000); color(white); cout<<"天亮了\n"; Sleep(2000); gotoxy(0,9); cout<<"昨晚"; bool is_die[15]={false},is_die_lieren=false,flag=false; for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife) { if(iLANGRENSHA||iNVWUDU) { if(players[i].typelieren) is_die_lieren=true; players[i].killer= (iLANGRENSHA ? langren:nvwu); players[i].die_or_life=die; is_die[i]=true; } if(iSHOUWEISHOU||iNVWUJIU) { if(players[i].typelieren) is_die_lieren=false; players[i].killer=-1; players[i].die_or_life=life; is_die[i]=false; }
    } } bool is_police_die=false; for(int i=0;i<player_number;i++) { if(is_die[i]) { if(flag) cout<<"和"<<i+1<<"号"; else cout<<i+1<<"号",flag=true; if(i==idx_police) is_police_die=true; } } if(flag) cout<<"死了\n"; else cout<<"是平安夜\n";

    if(is_die_lieren) Lieren();
    if(is_police_die) police_die();
    

    }

    /选警长/ void choose_police(){ system("cls"); print(); color(blue); gotoxy(0,7); cout<<"________________________\n"; color(yellow); cout<<"下面开始选举警长,各位不能选举自己~\n"; int tong[100]={0},cannot[100],must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); CHOOSE: color(yellow); Sleep(1500); for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife&&!is_include(cannot,i,player_number)) { if(imy_number) { cout<<"你要选举几号?\n"; int n; cin>>n; while(n<1||n>player_number||ni+1||players[n-1].die_or_lifedie||!is_include(must,n-1,player_number)) { cout<<"请重新输入!\n"; cin>>n; } cout<<i+1<<"号选举"<<n--<<"号\n"; tong[n]++; } else { int n=rand()%player_number; while(ni||players[n].die_or_lifedie||!is_include(must,n,player_number)) n=rand()%player_number; cout<<i+1<<"号选举"<<n+1<<"号\n"; tong[n]++; } Sleep(1500); } } int idx_max=-1,maxn=-1,len=0; for(int i=0;i<player_number;i++) if(maxn<tong[i]) { maxn=tong[i]; idx_max=i; } int maxn_arr[15]={0}; for(int i=0;i<player_number;i++) { if(tong[i]maxn) { maxn_arr[len++]=i; } } color(blue); if(len>1) { for(int i=0;i<len;i++) { if(ilen-1) { cout<<maxn_arr[i]+1<<"号平票\n"; } else { cout<<maxn_arr[i]+1<<"号,"; } } for(int i=0;i<len;i++) cannot[i]=maxn_arr[i]; for(int i=0;i<player_number;i++) { if(is_include(cannot,i,len)) must[i]=i; else must[i]=-1; } color(white); goto CHOOSE; } cout<<"恭喜"<<idx_max+1<<"号当选警长\n"; Sleep(3000); idx_police=idx_max; return; }

    /投票/ int toupiao(){ int tong[100]={0},cannot[100]={},must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); gotoxy(0,7); color(blue); cout<<"________________________\n"; color(white); cout<<"下面进入投票环节\n"; memset(tong,0,sizeof(tong)); Sleep(2000); TOUPIAO: for(int i=0;i<player_number;i++){ if(players[i].die_or_life == life&&!is_include(cannot,i,player_number)){ if(imy_number){ color(white); cout<<"你要投几号?\n"; int n; cin>>n; while(!(n>=1&&n<=player_number&&is_include(must,n-1,player_number))){ cout<<"请重新输入!\n"; cin>>n; } Sleep(2000); cout<<setw(2)<<my_number+1<<"号投了"<<setw(2)<<n<<"号"; if(my_numbern-1) color(red),cout<<"快来看!这有个疯子投自己!"; if(iidx_police) color(yellow),cout<<"(警长)\n"; else cout<<"\n"; if(iidx_police) tong[n-1]++; tong[n-1]++; }else{ color(white); int t=-1; while(t==-1 || players[t].die_or_life == die || ti || !is_include(must,t,player_number)){ if(iidxyuyanjia&&yuyanjiabixutoupiao!=-1) { t=yuyanjiabixutoupiao; yuyanjiabixutoupiao=-1; continue; } t=rand()%player_number; if(is_include(idxlangren,i,nlangren)) { if(players[t].type == langren) t=-1; } } cout<<setw(2)<<i+1<<"号"<<"投了"<<setw(2)<<t+1<<"号"; if(iidx_police) cout<<"(警长2票)\n"; else cout<<"\n"; if(iidx_police) tong[t]++; tong[t]++;
    }

    		Sleep(rand()%1000+1000);
    	}
    }
    int idx_max=-1,maxn=-1,len=0;
    for(int i=0;i<player_number;i++)
    	if(maxn<tong[i])
    	{
    		maxn=tong[i];
    		idx_max=i;
    	}
    int maxn_arr[15]={0};
    for(int i=0;i<player_number;i++)
    {
    	if(tong[i]==maxn)
    	{
    		maxn_arr[len++]=i;
    	}
    }
    color(blue);
    if(len>1)
    {
    	for(int i=0;i<len;i++)
    	{
    		if(i==len-1)
    		{
    			cout<<maxn_arr[i]+1<<"号平票\n"; 
    		}
    		else
    		{
    			cout<<maxn_arr[i]+1<<"号,";
    		}
    	}
    	for(int i=0;i<len;i++)
    		cannot[i]=maxn_arr[i];
    	for(int i=0;i<player_number;i++)
    	{
    		if(is_include(cannot,i,len))
    			must[i]=i;
    		else
    			must[i]=-1;
    	}
    	color(white);
    	goto TOUPIAO;
    	
    }
    
    cout<<idx_max+1<<"号"<<"出局\n";
    Sleep(4000);
    players[idx_max].die_or_life = die;
    players[idx_max].killer = good;
    return idx_max;
    

    }

    /警长死亡/ void police_die(){ color(yellow); int type; if(idx_police==my_number) { Sleep(1550); cout<<"你是想撕毁警徽还是移交警徽?(撕毁输入1,移交输入2)";

    	cin>>type;
    	while(!(type==1||type==2))
    	{
    		cout<<"请重新输入!\n";
    		cin>>type;
    	}
    }
    else{
    	type=rand()%3+1;
    }
    if(type==1)
    {
    	cout<<"警长选择撕毁警徽\n";
    	Sleep(1000);
    	idx_police=-1;
    }
    else
    {
    	int lucky=-1;
    	while(lucky==-1||players[lucky].die_or_life==die)
    		lucky=rand()%player_number;
    	cout<<"警长选择把警徽移交给"<<lucky+1<<"号\n";
    	Sleep(1500);
    	idx_police=lucky; 
    }
    

    }

    /故事的最后/ void the_end(){ system("cls"); switch(is_end()){ case 1:cout<<"好人胜利\n\n"; break; case 2:cout<<"狼人胜利\n\n"; break; case 3:cout<<"本局平局\n\n"; break; } for(int i=0;i<player_number;i++){ cout<<i+1<<"号位:\t"; switch(players[i].type){ case nvwu: cout<<"女巫\t";break; case yuyanjia: cout<<"预言家\t";break; case cunmin: cout<<"村民\t";break; case langren:cout<<"狼人\t";break; case lieren:cout<<"猎人\t"; break;
    case shouwei:cout<<"守卫\t";break;
    } cout<<"最终"; switch(players[i].killer){ case nvwu:cout<<"被女巫毒死\n"; break; case langren:cout<<"被狼人杀死\n"; break; case good:cout<<"被投票出局\n"; break; case lieren:cout<<"被猎人带走\n";break; default :cout<<"存活\n"; } cout<<endl; } }

    /主函数/ int main(){ int wheel=0; before_game(); while(!is_end()){ //黑夜准备 something_before_everyday(); Sleep(1500);

    	//黑夜部分 
    	Night(); //进入黑夜! 
    	change_daytime(); //换天 
    	
    	//天亮了 
    	panduansiwang();//判断谁死了 
    	Sleep(2000);
    	system("cls");
    	print();
    	if(is_end()) break;
    	
    	//选警长 
    	if(!wheel&&player_number==12)
    	{
    		choose_police();
    		system("cls");
    		print();
    	}
    	
    	//投票环节 
    	int idx_max=toupiao();//票数最多的人 
    	if(idx_max==idx_police){
    		police_die();
    	}
    	if(players[idx_max].type==lieren){//启动猎人程序 
    		Lieren();
    		if(is_end()) break;
    	}
    	system("cls");
    	print(); 
    	wheel++;
    }
    
    the_end();
    system("pause");
    return 0;
    

    }

    • 1