c++ - 如何为我的代码添加不同的字符?
问题描述
我想将我的角色绘制更改为#以外的其他字符。如何在不为新角色制作新板的情况下更改它?除此之外,我还想编译程序,使其可以是水平的,我打算使用“if else”语句,但不确定是否可以完成。
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void display( string displayBoard[][ 7 ], int row);
void clearScren();
void delay();
int main()
{
string letter;
string displayBoard[ 7 ][ 7 ];
cout << "Enter a letter or number:\n";
getline(cin, letter);
for (int i = 0; i < letter.length(); ++i)
{
switch (letter[i])
{
case 'c' :
case 'C' :
for (int a = 0; a < 1; ++a)
{
displayBoard[ 0 ][ a ] = " #### ";
displayBoard[ 1 ][ a ] = " # #";
displayBoard[ 2 ][ a ] = " # ";
displayBoard[ 3 ][ a ] = " # ";
displayBoard[ 4 ][ a ] = " # ";
displayBoard[ 5 ][ a ] = " # #";
displayBoard[ 6 ][ a ] = " #### ";
display( displayBoard, 7);
}
break;
case 'd' :
case 'D' :
for (int a = 0; a < 1; ++a)
{
displayBoard[ 0 ][ a ] = " ##### ";
displayBoard[ 1 ][ a ] = " # #";
displayBoard[ 2 ][ a ] = " # #";
displayBoard[ 3 ][ a ] = " # #";
displayBoard[ 4 ][ a ] = " # #";
displayBoard[ 5 ][ a ] = " # #";
displayBoard[ 6 ][ a ] = " ##### ";
display( displayBoard, 7);
}
break;
}
}
return 0;
}
void display( string displayBoard[][ 7 ], int row )
{
for( int i = 0; i < row; ++i )
{
for( int j = 0; j < 7; ++j )
{
cout << displayBoard[ i ][ j ];
}
cout << endl;
}
delay();
clearScren();
}
void delay()
{
for( int i = 0; i < 300000000; ++i )
{ }
}
void clearScren()
{
system( "cls" );
}
解决方案
我强烈建议从switch
语句转移到使用数组。
古老的设备,例如点阵打印机、光栅显示器、激光打印机和喷墨打印机,都会使用位图字体。字体将由一组位图组成,每个可打印字符一个。
这些设备将有一个“光栅”缓冲区。格式化程序函数会将位图(输出行中的每个字符一个)复制到“光栅”缓冲区中。raster 函数将打印每个位图的顶行,然后是第二行,依此类推,直到打印完位图的所有行。
该算法减少了switch
对每个位图的语句的需要。此外,它增加了更多的通用性,从而减少了位图和字母之间的耦合(还允许将自定义位图放置到缓冲区中)。这种技术转化为数据驱动;您可以通过更改数据来更改行为,而不必更改可执行代码。在您的代码中,要添加另一个字母或位图,必须添加另一个case
语句。
编辑 1:示例代码片段
typedef std::array<std::array<char, 5>, 7> BitMap; // 5 columns x 7 rows
Bitmap Letter_C = {{
{"#####"},
{"# "},
{"# "},
{"# "},
{"# "},
{"# "},
{"#####"},
} };
void Print_Single_Bitmap(const Bitmap& b, std::ostream& output)
{
for (int row = 0; row < 7; ++row)
{
for (int column = 0; column < 5; ++column)
{
output << b[row][column];
}
output << "\n";
}
}
注意:以上代码未经测试;行和列可能需要切换。
typedef std::vector<Bitmap> Bitmap_Buffer;
void Print_Bitmap_Line(const Bitmap_Buffer& buffer, std::ostream& output)
{
const unsigned int quantity = buffer.size();
for (int row = 0; row < 7; ++row)
{
for (unsigned int bitmap_index = 0; bitmap_index < quantity; ++bitmap_index)
{
for (int column = 0; column < 5; ++column)
{
output << buffer[bitmap_index][row][column];
}
output << " ";
}
output << "\n";
}
}
上面代码的目的是打印位图容器的每一行,然后是换行符。列被打印,然后位图之间的间距。
推荐阅读
- java - 如何使用 java 动态 Web 项目在后台运行进程
- openssl - 使用 openssl 创建带有 CSR 的自签名证书
- java - SpannableString 和 ClickableSpan 的问题
- python - pd.read_hdf 抛出“无法将此数组的 WRITABLE 标志设置为 True”和“没有名为“numpy.core._multiarray_umath”的模块
- android - 出现错误“与属性色调 (attr) 颜色不兼容”。在使用数据绑定时
- tmux - 将参数传递给 tmuxinator 项目文件
- android - 如何更改工具栏三点菜单可绘制颜色或色调?
- java - 是否可以从 Singleton 类访问 RequestScoped 类?
- drools - 我们可以在 Drools 中实现离散窗口规则吗?
- linux - 协调 Kaldi 配方的链接和路径文件