首页 > 解决方案 > 如何为我的代码添加不同的字符?

问题描述

我想将我的角色绘制更改为#以外的其他字符。如何在不为新角色制作新板的情况下更改它?除此之外,我还想编译程序,使其可以是水平的,我打算使用“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" );
}

标签: c++

解决方案


我强烈建议从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";
   }
}

上面代码的目的是打印位图容器的每一行,然后是换行符。列被打印,然后位图之间的间距。


推荐阅读