疯狂猜颜色小游戏C++个人项目

@疯狂猜颜色小游戏

欢迎浏览

这是一个使用C++语言编写的控制台小游戏,详细代码如下,敬请参考,不足之处还请提出,我的联系方式:1207787189@qq.com

1、交互界面和功能

玩家进入游戏后可以选择游戏难度,选择成功后,系统将会显示相应个数的彩色汉字,玩家需要输入汉字的背景颜色编号,而不是汉字本身。
游戏主要有:
1.难度模式选择
2.汉字模块
3.系统工具模块
4.UI界面模块
在这里插入图片描述在这里插入图片描述
//游戏模式代码.h(GameModes.h)

class GameModes
{
public:
	// 该变量用于表示不同难度下生成的颜色序号列表
	int ColorNumList[MaxNum];
	// 该变量用于表示某个模式下的字符长度
	int modesLen;

	void SimpleModes();  // 简单模式 1个
	void NormalModes();  // 普通模式 3个
	void HardModes();    // 难度模式 5个
	void HellModes();    // 地狱模式 7个

private:
	// 该函数根据选择的难度模式不同,生成对应不同的字符数据
	void getModesData(int mIndex);
public:
	GameModes();
	~GameModes();
};

//游戏模式代码.cpp(GameModes.cpp)

#include "stdafx.h"
#include "GameModes.h"
#include"SystemUI.h"

GameModes::GameModes()
{
	//初始化颜色序号数组
		for (int i = 0; i <= MaxNum; i++) {
			ColorNumList[i]=0;
		}
	this->modesLen = 0;
}


GameModes::~GameModes()
{
}
void GameModes::SimpleModes() {
	this->modesLen = 1;
	getModesData(this->modesLen);
}
void GameModes::NormalModes() {
	this->modesLen = 3;
	getModesData(this->modesLen);
}
void GameModes::HardModes() {
	this->modesLen =5;
	getModesData(this->modesLen);
}
void GameModes::HellModes() {
	this->modesLen = 7;
	getModesData(this->modesLen);
}
void GameModes::getModesData(int mIndex) {
	ColorCharset *cc = new ColorCharset();
	for (int i = 0; i < mIndex; i++) {
		this->ColorNumList[i] = cc->getColorOneCharset();
	}
	// 停顿1秒
	Sleep(1000);
	system("CLS");
}

//汉字模式.h

class ColorCharset
{
public:
	int colorIndex[5];  // 该变量用于表示字体颜色序号(有默认值)

						// 该函数用于表示随机生成一个有背景颜色的汉字
						// 返回值:该字符的颜色序号
	int getColorOneCharset();

private:
	string charsetName[5];  // 该变量用于表示字符文字(有默认值)

							/* 该函数用于给字符串str添加序号为i(C++默认)的背景颜色
							4 - 红
							6 - 黄
							3 - 蓝
							2 - 绿
							7 - 白
							*/
	void addColor(string str, int i);

	// 该函数用于获取一个(0-4)的随机数(通过当前时间设置随机数)
	int getRand();
public:
	ColorCharset();
	~ColorCharset();
};

//汉字模式.cpp

#include "stdafx.h"
#include "ColorCharset.h"


ColorCharset::ColorCharset()
{
	this->colorIndex[0] = Red;
	this->colorIndex[1] = Yellow;
	this->colorIndex[2] =  Blue;
	this->colorIndex[3] = Green;
	this->colorIndex[4] = White;

	this->charsetName[0] = "红";
	this->charsetName[1] = "黄";
	this->charsetName[2] = "蓝";
	this->charsetName[3] = "绿";
	this->charsetName[4] = "白";
}


ColorCharset::~ColorCharset()
{
}
int ColorCharset::getRand()
{
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	Sleep(500);
	int temp = (int)sys.wMilliseconds;
	return (temp % 5);
}
void ColorCharset::addColor(string str, int i) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i);
	cout << str << endl;
}
int ColorCharset::getColorOneCharset() {
	// 从字符数组中随机获取一个汉字
	string str = this->charsetName[this->getRand()];
	// 从颜色数组中随机获取一个颜色序号
	int i = this->colorIndex[this->getRand()];
	// 按照随机生成的str和i获取一个有背景颜色的汉字
	this->addColor(str, i);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	return i;
}

//系统工具.h

class SystemTool
{
public:
	int CheckPlayerCinIndex();
	void ClearUI();
public:
	SystemTool();
	~SystemTool();
};

//系统工具.cpp

#include "stdafx.h"
#include "SystemTool.h"


SystemTool::SystemTool()
{
}


SystemTool::~SystemTool()
{
}
int SystemTool::CheckPlayerCinIndex() {
	int index = 0;
	cin >> index;
	if (!cin) {
		cin.clear();
		cin.sync();
		return -1;
	}
	return index;
}
void  SystemTool::ClearUI() {
	system("PAUSE");
	system("CLS");
	system("PAUSE");
}

//系统UI.cpp

#include "stdafx.h"
#include "SystemUI.h"


SystemUI::SystemUI()
{
	cout << "==================================================================" << endl;
	cout << "                欢迎进入猜颜色小游戏" << endl << endl;
	cout << "游戏规则:" << endl;
	cout << "    请正确选择出字体的颜色,输入正确的颜色序号即可" << endl;
	cout << "==================================================================" << endl;
	cout << "例如游戏界面如下:" << endl;
	cout << "红\n蓝\n绿\n" << endl;
	cout << "颜色序号如下所示:" << endl;
	cout << "0 - 红" << endl;
	cout << "1 - 黄" << endl;
	cout << "2 - 蓝" << endl;
	cout << "3 - 绿" << endl;
	cout << "4 - 白" << endl;
	cout << "按照刚刚游戏中汉字出现的顺序输入汉字的实际背景颜色序号(不要换行):" << endl;
	cout << "444" << endl;
	cout << "恭喜你,回答正确!" << endl;
	cout << "==================================================================" << endl;
}


SystemUI::~SystemUI()
{
}

void SystemUI::StartGame() {
	int  iround;
	cout << "您希望游戏进行几局(不要换行)" << endl;
	cin >> iround;
	while (iround) {
		int index = ChoiceModesIndex();
		// 判断用户选择,创建对应的游戏模式,或者其他操作
		if (index == SimpleModes) {
			g.SimpleModes();
		}
		else if (index == NormalModes) {
			g.NormalModes();
		}
		else if (index == HardModes) {
			g.HardModes();
		}
		else if (index == HellModes) {
			g.HellModes();
		}
		else if (index == ExitIndex) {
			cout << "感谢体验该游戏!期待您下次的到来!"<< endl;
			break;  // 结束整个游戏循环
		}
		else {
			cout << "对不起,您的输入有误,请重新输入!"  << endl;
			continue; // 结束当前循环,重新开始选择
		}
		// 游戏显示后清屏,进入玩家输入阶段
		tool.ClearUI();
		// 游戏给出游玩界面后,用户进行回答
		// 判断用户的回答是否正确,给出相应的操作
		//int gradeintegral = 0;
		if (CheckResult()) {
		
			cout << "恭喜你,回答正确!" << endl;

		}
		else {
			cout << "好遗憾,回答错误!" << endl;
			
		}
		PAUSE;
		iround--;
	}
}
int SystemUI::ChoiceModesIndex() {
	cout << "游戏难度模式:" << endl;
	cout << "1、简单模式" << endl;
	cout << "2、普通模式" << endl;
	cout << "3、困难模式" << endl;
	cout << "4、地狱模式" << endl;
	cout << "5、退出" << endl;
	cout << "请选择您要挑战的游戏难度模式:" << endl;
	return tool.CheckPlayerCinIndex();
}
bool SystemUI::GetPlayerResult() {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	cout << "颜色序号如下所示:" << endl;
	cout << "0 - 红" << endl;
	cout << "1 - 黄" << endl;
	cout << "2 - 蓝" << endl;
	cout << "3 - 绿" << endl;
	cout << "4 - 白" << endl;
	cout << "按照刚刚游戏中汉字出现的顺序输入汉字的实际背景颜色序号(不要换行):" << endl;
	int result = tool.CheckPlayerCinIndex();
	if (result == -1) {
		cout << "对不起,您的输入有误!";
		return false;
	}
		// 通过除10取余的方式获取到每一个数字,并保存至数组中,最后一位用于保存数组实际长度
	this->resultList[MaxNum] = 0;
	for (int i = MaxNum - 1; i >= 0; i--) {
		resultList[i] = result % 10;
		result = result / 10;
		resultList[MaxNum]++;
		if (result == 0) break; // 如果数字统计完毕,则退出循环
	}
	return true;
		}
		
bool SystemUI::CheckResult() {
	if (!GetPlayerResult()) return false;
	/*
	计算思路:resultList[MaxNum]是数组最后一位,表示这个数组中实际有效数字个数
	假设resultList[MaxNum]是3,即len = 3
	意味着resultList[MaxNum-1],resultList[MaxNum-2],resultList[MaxNum-3]三个数字有效
	且顺序分别是用户输入的最后一位、中间位、第一位
	因此想要正向获取数据时,应 resultList[MaxNum-len+i] (i从0开始)
	*/
	int len = resultList[MaxNum];
	for (int i = 0; i < len; i++) {
		int index =resultList[MaxNum - len + i];
		// cc->colorIndex[index]是根据用户输入的序号获取颜色序号列表
		// g->ColorNumList[i]是正确答案
		if (cc.colorIndex[index] != g.ColorNumList[i]) {
			return false;
		}
	}
	return true;
	}

//系统UI.h

#include"ColorCharset.h"
#include"GameModes.h"
#include"SystemTool.h"
class SystemUI
{
public:
	GameModes g;  // 该指针变量用于指向具体的游戏模式对象
	SystemTool tool;  // 该指针变量用于指向具体的系统辅助工具
	ColorCharset cc;  // 该指针变量用于指向具体的颜色字符对象

	int resultList[MaxNum+1]; // 该数组用于保存用户输入的游戏结果,最后一位用于保存用户输入的实际长度

	string name;
	void StartGame();
private:
	int ChoiceModesIndex();  // 用于返回用户选择的游戏难度模式序号
	bool CheckResult();   // 用于返回用户输入的结果与游戏结果是否一致
	bool GetPlayerResult();  // 用于获取用户输入的颜色序号结果,返回是否成功获取结果
public:
	SystemUI();
	~SystemUI();
};

// 登陆.h

class DengLuGame
{
public:
	string name;
	void SetName();
	string GetName(string name);
public:
	DengLuGame();
	~DengLuGame();
};

//登陆.cpp

#include "stdafx.h"
#include "DengLuGame.h"


DengLuGame::DengLuGame()
{
	cout << "请为自己设置一个游戏昵称:" << endl;
	SetName();
	cout << "欢迎" <<this->GetName(name) << "进入游戏" << endl;
	cout << "请勿沉迷于游戏!!适度游戏!" << endl;
	PAUSE;
}


DengLuGame::~DengLuGame()
{
}
void DengLuGame::SetName() 
{
	cin >> name;
	this->name = name;
}
string DengLuGame::GetName(string name)
{
	return name;
}

//主函数

#include "stdafx.h"
#include"SystemUI.h"
#include"DengLuGame.h"

int main()
{
	string name;
	DengLuGame d;
	d.GetName(name);
	int iround;
	SystemUI s;
	s.StartGame();
	cout << "游戏已结束" << endl;
    return 0;
}

//stdafx.h(必须包含的头文件等)

#include<iostream>
#include<string>
#include<iomanip>
#include<windows.h>
using namespace std;
#define PAUSE system("pause")
#define CLS system("cls")
#define PC PAUSE;CLS
const int MaxNum = 7; // 该常量表示最大难度时的颜色个数

const int SimpleModes = 1;  // 简单模式序号
const int NormalModes = 2;  // 普通模式序号
const int HardModes = 3;    // 困难模式序号
const int HellModes = 4;    // 地狱模式序号
const int ExitIndex = 5;   // 退出系统序号

const int Red = 4;  // 红
const int Yellow = 6;  // 黄
const int Blue = 3;    // 蓝
const int Green = 2;    // 绿
const int White = 7;   // 白

  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值