【C++算法题】随机输出字符串的文字

// 随机输出字符串的文字.cpp : 定义控制台应用程序的入口点。
//

// 飞机大战(敌机放子弹).cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <vector>
#include <time.h>
#include <string>
using namespace std;

#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code)&0x8000?1:0
int _tmain(int argc, _TCHAR* argv[])
{
	//定义飞机头结构体
	struct SPlaneHead
	{
		int nPlaneHeadRow;
		int nPlaneHeadCol;

	};
	//头部初始化
	SPlaneHead sPlaneHead;
	sPlaneHead.nPlaneHeadRow = 12;
	sPlaneHead.nPlaneHeadCol = 8;


	//机身结构体
	struct SPlaneBody
	{
		int nPlaneBodyRow;
		int nPlaneBodyCol;

	};
	//机身初始化
	SPlaneBody arrPlane[3];

	//机身数组
	for (int i = 0; i < 3; i++)
	{
		arrPlane[i].nPlaneBodyRow = 13;
		arrPlane[i].nPlaneBodyCol = 7 + i;
	}

	//子弹结构体
	struct SBullet
	{
		int nBulletRow;
		int nBulletCol;
	};

	//初始化子弹
	//SBullet sBullet[100];

	//用容器初始化子弹,注意数据类型为结构体类型
	vector<SBullet> vecBullet;

	//敌机结构体
	struct SEnemyNode
	{
		int nEnemyNodeRow;
		int nEnemyNodeCol;
	};

	struct SEnemy
	{
		string strPic; //在结构体中定义敌人的值(要区别于字符串的值)
		SEnemyNode arrEnemyNode[4]; //在结构体中定义结构体数组!!
	};

	vector<SEnemy> vecEnemy;
	string arrPic[4] = { "恭", "喜", "发", "财" }; //初始化字符串的值

	//初始化敌机
	/*
	arrEnemyNode[0].nEnemyNodeRow = 1;
	arrEnemyNode[0].nEnemyNodeCol = 7;
	arrEnemyNode[1].nEnemyNodeRow = 1;
	arrEnemyNode[1].nEnemyNodeCol = 8;
	arrEnemyNode[2].nEnemyNodeRow = 1;
	arrEnemyNode[2].nEnemyNodeCol = 9;
	arrEnemyNode[3].nEnemyNodeRow = 2;
	arrEnemyNode[3].nEnemyNodeCol = 8;
	*/


	//int nLen = 1; //子弹数量
	int nDir = 0; //用于控制子弹向上移动
	int nDir1 = 0; //用于控制敌机向下运动
	int nTime = 0; //用于统计帧数
	int nEnemyTime = 0;

	srand(time(nullptr));

	int arrMap[15][20] = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
	};


	while (true)
	{

		nTime++; //每一次循环统计一次次数
		if (nTime >= 5)
		{
			nTime = 0; //归零

			/*
			nLen++;  //每10帧,子弹数量(长度)再增加1

			//设置子弹位置
			for (int i = 0; i < 100; i++)
			{
			//子弹位置为飞机头部的位置
			sBullet[i].nBulletRow = sPlaneHead.nPlaneHeadRow;
			sBullet[i].nBulletCol = sPlaneHead.nPlaneHeadCol;
			}
			*/

			//容器塞入数据
			//压入新子弹
			SBullet sBullet;//实例化
			sBullet.nBulletRow = sPlaneHead.nPlaneHeadRow;
			sBullet.nBulletCol = sPlaneHead.nPlaneHeadCol;

			vecBullet.push_back(sBullet);

		}

		nEnemyTime++;
		if (nEnemyTime>10)
		{
			nEnemyTime = 0;
			//定义敌机的中心坐标,用于敌机的初始化
			int nCenterRow = 1;
			int nCenterCol = rand() % 15 + 2;

			SEnemy enemy;
			int nIndex = rand() % 4; //定义字符串数组的索引,随机一个图片
			enemy.strPic = arrPic[nIndex]; //把字符串的值赋给敌人的值

			enemy.arrEnemyNode[0].nEnemyNodeRow = 1;
			enemy.arrEnemyNode[0].nEnemyNodeCol = nCenterCol - 1;
			enemy.arrEnemyNode[1].nEnemyNodeRow = 1;
			enemy.arrEnemyNode[1].nEnemyNodeCol = nCenterCol;
			enemy.arrEnemyNode[2].nEnemyNodeRow = 1;
			enemy.arrEnemyNode[2].nEnemyNodeCol = nCenterCol + 1;
			enemy.arrEnemyNode[3].nEnemyNodeRow = 2;
			enemy.arrEnemyNode[3].nEnemyNodeCol = nCenterCol;

			vecEnemy.push_back(enemy);

		}

		if (KEY_DOWN(VK_LEFT))
		{
			arrPlane[0].nPlaneBodyCol--;
			arrPlane[1].nPlaneBodyCol--;
			arrPlane[2].nPlaneBodyCol--;
			sPlaneHead.nPlaneHeadCol--;

			/*
			for (int i = 0; i < 100; i++)
			{
			sBullet[i].nBulletCol--;
			}
			*/
		}
		else if (KEY_DOWN(VK_RIGHT))
		{
			arrPlane[0].nPlaneBodyCol++;
			arrPlane[1].nPlaneBodyCol++;
			arrPlane[2].nPlaneBodyCol++;
			sPlaneHead.nPlaneHeadCol++;
		}

		//子弹向上运动
		if (nDir == 0)
		{
			for (int i = 0; i < vecBullet.size(); i++)
			{
				vecBullet[i].nBulletRow--;
			}
		}

		//敌机向下运动
		if (nDir1 == 0)
		{
			for (int i = 0; i < vecEnemy.size(); i++)  //等下要改为vector.size()
			{
				for (int j = 0; j < 4; j++)
				{
					vecEnemy[i].arrEnemyNode[j].nEnemyNodeRow++;
				}

			}
		}

		//不撞墙
		if (1 == arrMap[arrPlane[0].nPlaneBodyRow][arrPlane[0].nPlaneBodyCol])
		{
			arrPlane[0].nPlaneBodyCol++;
			arrPlane[1].nPlaneBodyCol++;
			arrPlane[2].nPlaneBodyCol++;
			sPlaneHead.nPlaneHeadCol++;
		}
		if (1 == arrMap[arrPlane[2].nPlaneBodyRow][arrPlane[2].nPlaneBodyCol])
		{
			arrPlane[0].nPlaneBodyCol--;
			arrPlane[1].nPlaneBodyCol--;
			arrPlane[2].nPlaneBodyCol--;
			sPlaneHead.nPlaneHeadCol--;
		}


		//渲染
		//二维数组的遍历
		for (int i = 0; i < 15; i++) //行数为15
		{
			for (int j = 0; j < 20; j++)//列数为20
			{

				bool DrawPlane = false;
				for (int k = 0; k < 3; k++)
				{
					if (i == arrPlane[k].nPlaneBodyRow && j == arrPlane[k].nPlaneBodyCol)
					{
						DrawPlane = true;
						break;
					}
				}

				/*
				bool DrawBullet = false;
				for (int m = 0; m < 100; m++)
				{
				if (i == sBullet[m].nBulletRow && j == sBullet[m].nBulletCol)
				{
				DrawBullet = true;
				break;
				}
				}
				*/

				bool DrawBullet = false;
				for (int m = 0; m < vecBullet.size(); m++)  //这里不能是100 因为数量是随着帧数变化 这里定义为长度
				{
					if (i == vecBullet[m].nBulletRow && j == vecBullet[m].nBulletCol)
					{
						DrawBullet = true;
						break;
					}
				}

				bool DrawEnemy = false;
				int nEnemyIndex = 0;//定义一个变量用于存放m
				for (int m = 0; m < vecEnemy.size(); m++)
				{
					for (int n = 0; n < 4; n++)
					{
						if (i == vecEnemy[m].arrEnemyNode[n].nEnemyNodeRow && j == vecEnemy[m].arrEnemyNode[n].nEnemyNodeCol)
						{
							DrawEnemy = true;
							nEnemyIndex = m; //把m的值存起来
							break;
						}

					}
				}


				//判断第i行第j列应该画什么
				if (1 == arrMap[i][j])//把数字为1的地方替换为■,作为墙壁
				{
					cout << "■";
				}
				else if (DrawPlane)
				{
					cout << "■";
				}
				else if (i == sPlaneHead.nPlaneHeadRow && j == sPlaneHead.nPlaneHeadCol)
				{
					cout << "■";
				}
				else if (DrawBullet)
				{
					cout << "弹";
				}
				else if (DrawEnemy)
				{
					//cout << "▼";
					cout << vecEnemy[nEnemyIndex].strPic; //打印出随机的字符串值
				}
				else
				{
					cout << "  ";//数字为0的地方设为空格
				}

			}
			cout << endl;//换行,排列成二维数组的形状
		}
		system("cls");

	}
	return 0;
}

教师案例

// PlaneWar.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <time.h>
#include <string>

//
#include <vector>

using namespace std;
#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code)&0x8000?1:0)

struct SNode
{
	int nRow;
	int nCol;
	int nRowBk;
	int nColBk;
};

struct SBullet
{
	int nRow;
	int nCol;
};

int g_arrMap[25][15] = {
	{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	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Node[4];
};

SNode g_arrPlane[4] = {};
//SBullet g_VecBullet[100] = {};

vector<SBullet> g_VecBullet;
vector<SEnemy> g_VecEnemy;

string arrPic[4] = { "恭", "喜", "发", "财" };

int g_nLen = 0;
int g_nTime = 0;
int g_nEnemyTime = 0;

int _tmain(int argc, _TCHAR* argv[])
{
	
	srand(time(nullptr));

	g_arrPlane[0].nRow = 23;
	g_arrPlane[0].nCol = 7;
	g_arrPlane[1].nRow = 23;
	g_arrPlane[1].nCol = 6;
	g_arrPlane[2].nRow = 23;
	g_arrPlane[2].nCol = 8;
	g_arrPlane[3].nRow = 22;
	g_arrPlane[3].nCol = 7;

	while (true)
	{
		//坐标备份
		for (int i = 0; i < 4; i++)
		{
			g_arrPlane[i].nRowBk = g_arrPlane[i].nRow;
			g_arrPlane[i].nColBk = g_arrPlane[i].nCol;
		}
		//移动
		if (KEY_DOWN(VK_LEFT))
		{
			for (int i = 0; i < 4; i++)
			{
				g_arrPlane[i].nCol--;
			}
		}
		else if (KEY_DOWN(VK_RIGHT))
		{
			for (int i = 0; i < 4; i++)
			{
				g_arrPlane[i].nCol++;
			}
		}

		//统计次数
		g_nTime++;
		if (g_nTime >= 10)
		{
			g_nTime = 0;
			//发射子弹
			//g_nLen++;
			SBullet bullet;
			//设置子弹位置
			bullet.nRow = g_arrPlane[3].nRow;
			bullet.nCol = g_arrPlane[3].nCol;
			g_VecBullet.push_back(bullet);
		}

		g_nEnemyTime++;
		if (g_nEnemyTime > 20)
		{
			g_nEnemyTime = 0;
			SEnemy enemy;
			int nIndex = rand() % 4;
			enemy.strPic = arrPic[nIndex];
			//设置敌人的坐标
			int nCenterRow = 1;
			int nCenterCol = 2 + rand() % 11;
			enemy.arrNode[0].nRow = nCenterRow;
			enemy.arrNode[0].nCol = nCenterCol;
			enemy.arrNode[1].nRow = nCenterRow;
			enemy.arrNode[1].nCol = nCenterCol - 1;
			enemy.arrNode[2].nRow = nCenterRow;
			enemy.arrNode[2].nCol = nCenterCol + 1;
			enemy.arrNode[3].nRow = nCenterRow + 1;
			enemy.arrNode[3].nCol = nCenterCol;
			g_VecEnemy.push_back(enemy);
		}

		//敌人移动
		for (int i = 0; i < g_VecEnemy.size(); i++)
		{
			for (int j = 0; j < 4; j++)
			{
				g_VecEnemy[i].arrNode[j].nRow++;
			}
		}

		//子弹移动
		for (int i = 0; i < g_VecBullet.size(); i++)
		{
			g_VecBullet[i].nRow--;
		}


		//碰墙检测
		if ((1 == g_arrMap[g_arrPlane[1].nRow][g_arrPlane[1].nCol]) || 1 == g_arrMap[g_arrPlane[2].nRow][g_arrPlane[2].nCol])
		{
			for (int i = 0; i < 4; i++)
			{
				g_arrPlane[i].nRow = g_arrPlane[i].nRowBk;
				g_arrPlane[i].nCol = g_arrPlane[i].nColBk;
			}
		}

		//子弹碰墙移除
		for (int i = 0; i < g_VecBullet.size(); i++)
		{
			if (1 == g_arrMap[g_VecBullet[i].nRow][g_VecBullet[i].nCol])
			{
				g_VecBullet.erase(g_VecBullet.begin() + i);
			}
		}

		//渲染
		for (int i = 0; i < 25; i++)
		{
			for (int j = 0; j < 15; j++)
			{
				//渲染蛇
				bool bDraw = false;
				for (int m = 0; m < 4; m++)
				{
					if (g_arrPlane[m].nRow == i && g_arrPlane[m].nCol == j)
					{
						bDraw = true;
						break;
					}
				}

				bool bDrawBullet = false;
				for (int m = 0; m < g_VecBullet.size(); m++)
				{
					if (g_VecBullet[m].nRow == i && g_VecBullet[m].nCol == j)
					{
						bDrawBullet = true;
						break;
					}
				}

				bool bDrawEnemy = false;
				int nEnemyIndex = 0;
				for (int m = 0; m < g_VecEnemy.size(); m++)
				{
					for (int n = 0; n < 4; n++)
					{
						if (g_VecEnemy[m].arrNode[n].nRow == i && g_VecEnemy[m].arrNode[n].nCol == j)
						{
							bDrawEnemy = true;
							nEnemyIndex = m;
							break;
						}
					}

				}

				if (1 == g_arrMap[i][j])
				{
					cout << "■";
				}
				else if (bDraw)
				{
					cout << "■";
				}
				else if (bDrawEnemy)
				{
					//cout << "敌";
					cout << g_VecEnemy[nEnemyIndex].strPic;
				}
				else if (bDrawBullet)
				{
					cout << "●";
				}
				else{
					cout << "  ";
				}
			}
			cout << endl;
		}
		system("cls");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值