跳到正文

休闲·娱乐

2

Kryagon约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年9月21日
收录
2026年8月6日
更新
2026年11月14日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;
#define Made return
#define By 0
#define Kryagon ;
int getrandomnumber(int min, int max){
	random_device rd;
	mt19937 gen(rd());
	uniform_int_distribution<>dis(min, max);
	return dis(gen);
}
void print(vector<string>candy){
	cout << endl;
	for(int i = 0;i < 20;i++){
		cout << setw(10) << left << candy[i];
		cout.flush();
		if((i + 1) % 5 == 0){
			cout << endl;
		}
	}
	cout << endl;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << "游戏规则:" << endl;
	cout << endl;
	cout << "此游戏为人机对战,每人在20颗糖果中选择一颗作为毒药,接下来轮流每人选一颗糖吃掉。" << endl;
	cout << endl;
	cout << "判断胜负:" << endl;
	cout << endl;
	cout << "如果有人吃掉了毒药,则游戏结束,对方胜利。若最后只剩下两颗毒药则两人平局。" << endl;
	vector<string>candy(20);
	vector<bool>vis(20, false);
	for(int i = 0;i < 20;i++){
		candy[i] = to_string(i + 1);
	}
	print(candy);
	cout << "请选择你要将哪颗糖设为毒药:";
	cout.flush();
	int n;
	cin >> n;
	candy[n - 1] += "(毒药)";
	int poison;
	while(true){
		poison = getrandomnumber(1, 20);
		if(poison != n){
			break;
		}
	}
	int count = 20;
	while(count > 2){
		print(candy);
		int i;
		while(true){
			cout << "请选择一颗糖果吃掉:";
			cout.flush();
			cin >> i;
			cout << endl;
			if(vis[i - 1]){
				cout << "不能吃掉已经吃掉的糖果!" << endl;
				cout << endl;
			}
			else{
				break;
			}
		}
		if(i == poison || i == n){
			cout << "你吃掉了毒药!" << endl;
			cout << "You lose!" << endl;
			return 0;
		}
		vis[i - 1] = true;
		count--;
		candy[i - 1] += "(糖果)";
		while(true){
			i = getrandomnumber(1, 20);
			if(!vis[i - 1] && i != poison){
				break;
			}
		}
		if(i == n){
			cout << "对方吃掉了毒药!" << endl;
			cout << "You win!" << endl;
			return 0;
		}
		cout << "对方已吃掉糖果。" << endl;
		cout << endl;
		vis[i - 1] = true;
		count--;
		candy[i - 1] += "(糖果)";
	}
	cout << "游戏结束,两人平局!" << endl;
	Made By Kryagon
}
原文