跳到正文

科技·工程

三国杀

Holy_Cabbage约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年11月22日
收录
2026年11月22日
更新
2026年11月25日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

欢迎私信催更和提议和bug反馈。

1.0
C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
#define PEOPLE_NUM 3
#define TYPE 1
#define CEIL_CARD 998244353
void Error(int cas,int play,string card){
	if(cas==1){
		cout<<"[Error]: 玩家"<<play<<"没有卡牌["<<card<<"]\n";
	}if(cas==2){
		cout<<"[Error]: 玩家"<<play<<"无法使用卡牌["<<card<<"]\n";
	}
}
void changelog(){
	string s={
		"2026/11/22 完成[杀],暂无bug,版本1.0\n"
	};
}
void gotoxy(int x,int y){
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}void hideCursor(){//隐藏光标(编辑界面时很有用,光标不闪)
	CONSOLE_CURSOR_INFO info;
	GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
	info.bVisible=FALSE;
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
}void showCursor(){//显示光标
	CONSOLE_CURSOR_INFO info;
	GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
	info.bVisible=TRUE;
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
}void clearScreen(){//清屏(比 system("cls") 快,不闪烁)
	HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD written;
	COORD topLeft={0,0};
	GetConsoleScreenBufferInfo(h, &csbi);
	DWORD size=csbi.dwSize.X*csbi.dwSize.Y;
	FillConsoleOutputCharacter(h,' ',size,topLeft,&written);
	FillConsoleOutputAttribute(h,csbi.wAttributes,size,topLeft,&written);
	SetConsoleCursorPosition(h,topLeft);
}
class every_people{
private:
public:
	int blood;
	string password;
	int kill;
	every_people(){
		blood=4;
		password="";
		kill=0;
	};
	void put(){
		cout<<"当前血量:"<<blood<<"\n";
		cout<<"1.[杀]:"<<kill<<"\n";
		cout<<"输入[-1]退出\n";
	}
	void add_card(int type){
		switch(type){
			case 1: kill++;break;
		}
	}
}pep[PEOPLE_NUM];
void password_read(string &s){
	int c=_getch();
	s="";
	while(c!='\r'&&c!='\n'){
		if(c=='\b'){
			if(s.empty()) goto END;
			cout<<"\b \b";
			s.pop_back();
			END:;
			c=_getch();
			continue;
		}cout<<'*';
		s.push_back(c);
		c=_getch();
	}cout<<"\n";
}
void password_in(int plays){
	cout<<"玩家"<<plays<<"请输入您的密码:\n";
	password_read(pep[plays].password);
}
bool password_check(int plays){
	cout<<"玩家"<<plays<<"请输入您的密码以检验:\n";
	string s;
	password_read(s);
	return s==pep[plays].password;
}
void use_kill(int play1,int play2,int &killnum){
	if(!pep[play1].kill) return Error(1,play1,"杀");
	if(!killnum) return Error(2,play1,"杀");
	pep[play1].kill--;
	pep[play2].blood--;
	killnum--;
}
bool game_over(){
	for(int i=1;i<PEOPLE_NUM;i++)
		if(pep[i].blood<=0) return 1;
	return 0;
}
int arand(int l,int r){
	static mt19937 rng(random_device{}()^rand());
	uniform_int_distribution<int> dist(l,r);
	return dist(rng);
}
int new_card(){
	return arand(1,TYPE);
}
int cardname_to_num(string name){
	if(name[0]=='[') name=name.substr(1);
	if(name.back()==']') name.pop_back();
	if(name=="杀") return 1;
	return -1;
}
bool in(int &number){
	cin>>number;
	if(!cin){
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
		return 0;
	}return 1;
}
void solve(int play){
	if("in the one's password"){
		START:;
		if(!password_check(play)) goto START;
	}int killnum=1;
	while(1){
		clearScreen();
		cout<<"请输入您要打出的牌的编号,您现在拥有:\n";
		pep[play].put();
		cout<<"要打出的牌的编号:";
		int number;
		bool asi=in(number);
		if(asi&&!number) goto DEFAULT;
		if(!asi) number=0;
		SWITCH_START:;
		switch(number){
			case 0:{
				gotoxy(0,0);
				cout<<"请输入您要打出的牌的编号,您现在拥有:\n";
				pep[play].put();
				cout<<"输入不合法,请重新输入:";
				if(!in(number)) number=0;
				goto SWITCH_START;
				break;
			}
			case 1:{
				clearScreen();
				if(pep[play].kill<=0){
					cout<<"您没有[杀],按任意键返回...\n";
					_getch();
					break;
				}
				if(killnum<=0){
					cout<<"本回合[杀]已用完,按任意键返回...\n";
					_getch();
					break;
				}
				cout<<"您正在使用[杀]\n";
				cout<<"请输入您要指定的目标:";
				int other_play;
				if(!in(other_play)) other_play=0;
				while(other_play>=PEOPLE_NUM||other_play<1||other_play==play){
					clearScreen();
					cout<<"您正在使用[杀]\n";
					cout<<"目标["<<other_play<<"]不合法,请重新输入:";
					if(!in(other_play)) other_play=0;
				}
				use_kill(play,other_play,killnum);
				if(game_over()) return ;
				break;
			}
			case -1:{
				return ;
				break;
			}
			default:{
				DEFAULT:;
				gotoxy(0,0);
				cout<<"请输入您要打出的牌的编号,您现在拥有:\n";
				pep[play].put();
				cout<<"未查询到编号["<<number<<"]请重新输入:";
				if(!in(number)) number=0;
				goto SWITCH_START;
				break;
			}
		}
	}
}
signed main(){
	system("chcp 65001>nul");
	for(int i=1;i<PEOPLE_NUM;i++)
		if("in playi password"){
			START:;
			password_in(i);
			if(!password_check(i)) goto START;
		}
	for(int j=1;j<PEOPLE_NUM;j++)
		for(int i=1;i<=4;i++)
			pep[j].add_card(new_card());
	while(!game_over()){
		for(int i=1;i<PEOPLE_NUM;i++){
			solve(i);
			if(game_over()) break;
		}
	}
	return 0;
}
原文