以下为实际代码:
共100组词语
//#include<bits/stdc++.h>
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<string>
#include<time.h>
using namespace std;
vector<string> v = {"白菜","生菜","辣椒","青椒","水桶","水盆","烤肉","涮肉","气泡","水泡",
"饮料","白开水","牛肉","羊肉","吉他","琵琶","橘子","橙子","红茶","绿茶",
"海豚","海狮","牛肉干","猪肉铺","老师","班主任","墨镜","近视眼镜","游戏机","手机",
"双肩包","手提包","面包","蛋糕","警察","保安","树叶","小草","地铁","高铁",
"铅笔","蜡笔","纸巾","湿巾","毛衣","外套","魔法","魔术","玻璃","镜子"
"空调","风扇","平板","电脑","童话","神话","奖牌","奖状","沐浴露","洗发水",
"枕头","抱枕","被子","床单","祖父","父亲","牛奶","豆浆","西瓜","哈密瓜",
"鸡蛋","鸭蛋","马路","人行道","红绿灯","应急灯","创口贴","绷带","地铁票","高铁票",
"抽水管道","下水管道","葡萄","荔枝","薯片","饼干","结婚","订婚","自行车","电动车",
"菠萝蜜","榴莲","蝴蝶","飞蛾","麦克风","扩音器","胡子","眉毛","期中考试","期末考试",
"同学","同桌","肉包","菜包","酸菜鱼","水煮鱼","存款","贷款","烘干机","甩干机",
"皇上","太子","篮球","足球","鹅毛","鸡毛","乌鸦","麻雀","猫","狐狸",
"汽车","电车","状元","冠军","汤圆","丸子","监狱","派出所","衬衫","背心",
"表白","求婚","手表","手机","程序","软件","大米","玉米","山脉","山峰",
"刀","剪刀","笔记本电脑","台式电脑","推销","销售","快餐","外卖","打火机","火柴",
"腌黄瓜","榨菜","春节","元旦","解释","狡辩","唇膏","口红","雨伞","遮阳伞",
"编译","运行","油条","麻花","螺蛳粉","臭豆腐","婚纱","喜服","好友","亲人",
"源头厂家","中间商","奢侈","富裕","黄金","钻石","首饰","珠宝","猪蹄","鸡爪",
"玫瑰","月季","贴纸","贴画","卧底","间谍","语文","英语","物理","数学",
"碰碰车","过山车","花朵","矮草","草丛","花坛","洗澡池","游泳池","田螺","福寿螺"};
bool now[100] = {false,false,false,false,false,false,false,false,false,false};
bool nsw[100] = {false,false,false,false,false,false,false,false,false,false};
void swap(int *p, int *q){
int temp;
temp = *p;
*p = *q;
*q = temp;
}
int sortcy(int arr[],int n) {
unordered_map<int, int> count;
int max_count = 0;
int max_num = 0;
for (int i = 1; i <= n; i++) {
count[arr[i]]++;
if (count[arr[i]] > max_count) {
max_count = count[arr[i]];
max_num = arr[i];
} else if (count[arr[i]] == max_count) {
max_num = 0;
}
}
return max_num;
}
int main(void){
srand(time(NULL));
int index1 = rand() % v.size();
if(index1 % 2 == 0){
index1 -= 1;
}
int index2 = index1 + 1;
cout << "欢迎来到谁是卧底" << endl;
cout << "游戏规则如下" << endl;
cout << "1.游戏开始后,每个人会收到一个词语,其中会有1~2个人的词语与其他人不同。" << endl;
cout << "2.每轮描述自己的词语,不能说自己的词语或相同的话。" << endl;
cout << "3.投票环节,请投出你认为是卧底的玩家或弃票,票数最多的人出局。" << endl;
cout << "4.所有卧底被投出后平民胜利。" << endl;
cout << "5.剩余平民数量小于等于剩余卧底数量时,卧底胜利。" << endl;
cout << "作者:抖音搜lengyi114514" << endl;
cout << "按a开始,按b关闭。" << endl;
string s;
cin >> s;
if(s == "b"){
return 0;
} else if(s != "a" && s != "b"){
cout << "无效输入" << endl;
cin >> s;
if(s == "b"){
return 0;
}
}
int n,wdrs;
cout << "请输入人数" << endl;
cin >> n;
if(n > 10){
cout << "人数过多";
cin >> n;
} else if(n < 3){
cout << "人数过少";
cin >> n;
} else if(n < 7){
wdrs = 1;
} else if(n >= 7 && n <= 10){
wdrs = 2;
}
for(int i = 1;i <= n;i++){
now[i] = true;
}
system("cls");
int where1,where2;
srand(time(0));
where1 = rand() % n;
srand(time(0));
where2 = rand() % n;
nsw[where1] = true;
if(where2 == where1 && wdrs == 2){
if(where2 == n){
where2 - 1;
} else if(where2 == 1){
where2 + 1;
} else {
where2 + 1;
}
nsw[where2] = true;
} else if(wdrs == 1){
where2 = -1;
}
for(int i = 1;i <= n;i++){
cout << "玩家" << i << endl;
cout << "你的词语是" << endl;
if(i == where1 || i == where2){
cout << v[index1];
} else {
cout << v[index2];
}
cout << endl << "按a确认" << endl;
string stri;
cin >> stri;
if(stri == "a"){
system("cls");
}
}
while(true){
cout << "发言环节" << endl;
cout << "按a结束" <<endl;
string str;
cin >> str;
if(str == "a"){
system("cls");
int tp[100];
for(int i = 1;i <= n;i++){
cout << "玩家" << i << "请投票(输入玩家序号)" << endl;
cout << "弃票填0" << endl;
cin >> tp[i];
}
system("cls");
if(sortcy(tp,n) == 0){
cout << "无玩家出局" << endl;
cout << "按a确认" << endl;
string ste;
cin >> ste;
if(sta == "a"){
continue;
}
} else {
cout << "玩家" << sortcy(tp,n) << "出局" << endl;
cout << "ta的词是";
if(sortcy(tp,n) == where1 || sortcy(tp,n) == where2){
cout << v[index1];
} else if(sortcy(tp,n) != where1 || sortcy(tp,n) != where2){
cout << v[index2];
}
cout << endl;
now[sortcy(tp,n)] = false;
}
cout << "按a继续" << endl;
string strin;
cin >> strin;
if(strin == "a"){
system("cls");
}
}
int sum1 = 0,sum2 = 0;
for(int i = 1;i <= n;i++){
if(now[i] == true && nsw[i] == true){
sum1++;
}
if(now[i] == true && nsw[i] == false){
sum2++;
}
}
if(sum1 >= sum2){
cout << "游戏结束,卧底胜利!" << endl;
cout << "按a确认" << endl;
string stb;
cin >> stb;
if(stb == "a"){
break;
}
} else if(sum1 == 0){
cout << "游戏结束,平民胜利!" << endl;
cout << "按a确认" << endl;
string sta;
cin >> sta;
if(sta == "a"){
break;
}
}
}
return 0;
}