C++狼人杀小游戏

 这个版本的狼人杀是经改良过得

代码如下 

#include <iostream>//C++输入输出流库
#include <cstdlib>//使用srand函数要用到这个库
#include <ctime>//使用time函数要用到这个库
#include <Windows.h>
#include <conio.h>
long long sr=0;
using namespace std;
void brc()
{
	system("cls");
	long long jy=1,wd=3;
	long long sy=3,wj=3;
	srand((int)time(0));
	long long n=rand()%15+1;
	cout<<"                                你是"<<n<<"号"<<endl;
	cout<<"                               正在分配身份"<<endl;
	Sleep(1500);
	long long m[20];
	for(long long i=1;i<=15;i++)
		m[i]=1;
	long long s[20]={0};
	long long yy[20]={0};
	long long l=0,w=0,j=0,p=0;
	for(long long i=1;i<=15;i++)
	{
		do
		{
			long long a=rand()%4+1;
			if(a==1&&l<1)
			{
				l++;
				s[i]=1;
				sr=i;
			}
			else if(a==2&&w<2)
			{
				w++;
				s[i]=2;
			}
			else if(a==4&&p<3)
			{
				p++;
				s[i]=4;
			}
			else if(a==3&&j<9
			)
			{
				j++;
				s[i]=3;
			}
		}
		while(s[i]==0);
	}
	if(s[n]==1)
		cout<<"                               你是>>杀手<<"<<endl;
	else if(s[n]==2)
		cout<<"                               你是>>预言家<<"<<endl;
	else if(s[n]==3)
		cout<<"                               你是>>平民<<"<<endl;
	else
		cout<<"                               你是>>法师<<"<<endl;
	cout<<"                             游戏将在5秒后开始"<<endl; 
	Sleep(5000);
	long long c=15;
	long long f=0,flag=0;
	long long day=1;
	long long lr;
	long long tp[20]={0};
	do
	{
		lr=0;
		system("cls");
		cout<<"                                 第"<<day<<"晚开始"<<endl;
		cout<<"你是"<<n<<"号"<<endl;
		long long x;
		for(long long i=1;i<=15;i++) 
		{
			if(i==n&&m[n]==1)
			{
				if(s[n]==1)
				{
					cout<<"                                 你是杀手"<<endl;
					for(long long j=1;j<=15;j++)
						if(m[j]==1&&s[j]!=1)
							cout<<j<<" ";
					cout<<endl;
					cout<<"                                 你选择杀掉"<<endl; 
					long long a;
					cin>>a;
					m[a]=0;
					cout<<endl;
					cout<<"                                 杀手杀了"<<a<<"号"; 
					if(s[a]==1)
						cout<<"(杀手)"<<endl;
					else if(s[a]==2)
						cout<<"(预言家)"<<endl;
					else if(s[a]==3)
						cout<<"(平民)"<<endl;
					else
						cout<<"(法师)"<<
当然可以!下面是一个简单的C++狼人小游戏的代码示例: ```cpp #include <iostream> #include <vector> #include <string> #include <cstdlib> #include <ctime> using namespace std; // 角色类 class Role { public: string name; bool isWolf; bool isDead; Role(string n, bool w) : name(n), isWolf(w), isDead(false) {} void kill() { isDead = true; } }; // 游戏类 class WerewolfGame { private: vector<Role> players; public: void addPlayer(Role player) { players.push_back(player); } void startGame() { cout << "游戏开始!" << endl; // 狼人人 for (int i = 0; i < players.size(); i++) { if (players[i].isWolf && !players[i].isDead) { int target = rand() % players.size(); while (players[target].isDead || players[target].isWolf) { target = rand() % players.size(); } players[target].kill(); cout << players[i].name << "死了" << players[target].name << endl; } } // 显示游戏结果 cout << "游戏结束!" << endl; for (int i = 0; i < players.size(); i++) { cout << players[i].name << " "; if (players[i].isDead) { cout << "死亡"; } else { cout << "存活"; } cout << endl; } } }; int main() { srand(time(0)); // 创建角色 Role player1("玩家1", false); Role player2("玩家2", false); Role player3("玩家3", true); Role player4("玩家4", true); // 创建游戏 WerewolfGame game; // 添加角色到游戏中 game.addPlayer(player1); game.addPlayer(player2); game.addPlayer(player3); game.addPlayer(player4); // 开始游戏 game.startGame(); return 0; } ``` 这个代码实现了一个简单的狼人小游戏游戏中有两个狼人和两个平民,狼人会在每轮中选择一个平民进行死,直到所有狼人或者所有平民都死亡为止。最后会显示每个角色的生死状态。 请注意,这只是一个简单的示例代码,实际的狼人游戏可能需要更多的规则和功能。你可以根据自己的需求进行扩展和修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值