Bingo

本文介绍了一个使用C++实现的猜数字游戏程序。该程序利用了随机数生成、字符串比较等技术,通过用户输入进行游戏互动,实现了A和B的判断逻辑,即位置正确和数字正确的反馈。同时,程序还包含了错误输入处理,确保了游戏的稳定性和用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
#include <tchar.h>
using namespace std;
int tg[4];
char nb[4];
bool ok[10] = {false};
int main(){
	int x;
	srand(time(NULL));
	for(int i = 0; i < 4; i++){
		x = rand() % 10;
		while(ok[x] == true || (i == 0 && x == 0)){
			x = rand() % 10;
		}
		tg[i] = x;
		ok[x] = true;
		nb[i] = char(48 + tg[i]);
	}

	int cnt = 0;
	int a = 0, b = 0;
	string s;
	while(1){
		cnt++;
		cout << "Guess Number " << cnt << endl;
		cout << "Your Guess: ";
		cin >> s;
		if(s.size() != 4){
			cout << "Invalid Guessing!" << endl;
			if(s.size() == 10){
				MessageBox(NULL, _T(nb), _T("Answer"), MB_OK);
			}
			cnt--;
			continue;
		}
		for(int i = 0; i < 4; i++){
			for(int j = 0; j < 4; j++){
				if(s[i] == nb[j]){
					b++;
					if(i == j){
						a++;
					}
				}
			}
		}
		cout << "Result: ";
		if(b == 0){
			cout << "None!";
		}
		cout << a << "A" << b << "B" << endl;
		cout << endl;
		if(a == 4){
			cout << "You Win!" << endl;
			system("pause");
			return 0;
		}
		a = 0;
		b = 0;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值