c++猜单词游戏(1P/2P)

#include <bits/stdc++.h>
using namespace std;
int main() {
	srand(time(NULL));
	int n = 200;
	int a[n];
	string chop;
	string qp;
	int life;
	bool p[26] = {false};
	cout << "欢迎来到猜词游戏!(虽然电脑给的不是真的词)" << endl;
	cout << "选择模式 (1P / 2P)" << endl;
	cin >> chop;
	if (chop == "2P") {
		cout << "1号玩家,请输入目标词" << endl;
		cin >> qp;
		n = qp.size();
		cout << "请输入您想给对方的机会数" << endl;
		cin >> life;
		for (int i = 0; i < n; i++) {
			a[i] = qp[i];
		}
	} else {
		cout << "请输入你想猜的单词的长度 " << endl;
		cin >> n;
		cout << "请输入你想要的机会数 " << endl;
		cin >> life;
	}
	system("cls");
	bool rev[n] = {false};
	if (chop == "1P") {
		for (int i = 0; i < n; i++) {
			a[i] = char(rand() % 26 + 97);
		}
	}
	int cnt = 0;
	char ch;
	while (life > 0) {
		cout << "猜词状态:" << endl;
		for (int i = 0; i < n; i++) {
			if (rev[i]) {
				cout << char(a[i]);
				cnt++;
			} else {
				cout << "-";
			}
		}
		cout << endl;
		cout << "猜出" << cnt << "个字母" << endl;
		if (cnt == n) {
			cout << "你赢了!" << endl;
			return 0;
		}
		cout << "剩余机会数: " << life << endl;
		cout << "选择你要猜的字母: ";
		cnt = 0;
		cin >> ch;
		while (1) {
			if (p[int(ch) - 97] == true) {
				cout << endl;
				cout << "猜过了!";
				cin >> ch;
			} else {
				p[int(ch) - 97] = true;
				cout << "检测中..." << endl;
				_sleep(300);
				for (int i = 0; i < n; i++) {
					if (a[i] == ch) {
						rev[i] = true;
						cnt++;
					}
				}
				if (cnt == 0) {
					cout << "你失去了一次机会!" << endl;
					life--;
				}
				cout << "本轮猜出" << cnt << "个字母" ;
				cnt = 0;
				system("pause");
				system("cls");
				break;
			}
		}
	}
	cout << "你输了。" << endl << "单词是:";
	for (int i = 0; i < n; i++) {
		cout << char(a[i]);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值