如何将.art文件中的值转换为C ++中的ASCII art

时间:2015-07-03 02:44:53

标签: c++ ascii-art

处理我应该根据用户输入加载文件的项目,将该文件中的数据转换为窗口中的坐标,然后使用ASCII字符绘制图片。

文件位于.art中,以所需窗口的宽度和高度开始,每个后续行都是需要绘制的ROW,COLUMN,CHARACTER和COUNT。所以基本上应该开始特定角色的位置,然后应该绘制多少次。 我正在努力的是如何将数据导入可用的东西,以便我可以绘制所请求的图像。我最初的想法是将数据导入为4-D阵列,但后来我在那里画了一个空白。

.art文件的示例行:

  50 x 25
  2, 15, *, 9
  2, 48, *, 9
  3, 6,  *, 15

更新:考虑到尝试加载文件,因为我无法绕过那个,所以我改为只是手工绘制艺术并调用字符串,但我甚至遇到了问题。使用下面的代码,如果我选择选项1,则输出为001FFA80,而不是输出字符串中的ASCII艺术。

    #include <stdio.h>
    #include <cstdlib>
    #include <string>
    #include <iostream>
    using namespace std;

    int main ()
   {
string shapeCupid[]=
{"                                      ",
 "          ..                          ",
 "           $. ,o$$$o.                 ",
 "           $. $$$$$$$o.   ..          ",
 "          .$. $' $$$$$$ ,o''          ",
 "         .$'  $  '$$$$$,o'.,'   .oo ' ",
 "        .$'   $.   $$$$'  ,,  .o'.    ",
 "       .$'    '$o. 'O$ .. ooo''',oo ' ",
 "      .$'     .o$'  '$$''     ,,o'    ",
 "    .%$,,,,,ooO'      '       ,,o''   ",
 "  .$o.           ,o'    $o     ..oo'  ",
 "   ''O'''''''''','       $'$. .o'     "};
string shapeFly = "Fly";
string shapeHeart = "Heart";
string shapeImpossible = "Impossible";
string shapeSeuss = "Seuss";
string shapeWorry = "Worry";
int UserInput;
cout << "What do you want to draw?\n";
cout << "1. Cupid\n2. Fly\n3. Heart\n4. Impossible\n5. Seuss\n6. Worry\nNumber: ";
cin >> UserInput;
if (UserInput == 1){
        cout << shapeCupid;}
else if (UserInput == 2){
    cout << shapeFly;}
else if (UserInput == 3){
    cout << shapeHeart;}
else if (UserInput == 4){
        cout << shapeImpossible;}
else if (UserInput == 5){
    cout << shapeSeuss;}
else if (UserInput == 6){
        cout << shapeWorry;}
else if (UserInput != 1 || 2 || 3 || 4 || 5 || 6)
    cout << "Please select proper value.\n";

system("pause");
return 0;

}

1 个答案:

答案 0 :(得分:1)

这是一个快速而肮脏的版本,可能有所帮助。我没有添加大量的错误检查,但它适用于我使用的2个示例。如果您计划将数据嵌入为字符串文字,那么您希望确保转义任何特殊字符,如反斜杠和双引号。 Boop示例包含两者。

对于嵌入的字符串文字数据,第一行必须填充为与最长行一样长,因为它用于写入宽度。增强功能是迭代所有行以获得最大宽度并使用它。

#include <string>
#include <iostream>
#include <vector>
#include <fstream>

using StringVec = std::vector<std::string>;

StringVec shapeCupid =
{
    "                                      ",
    "          ..                          ",
    "           $. ,o$$$o.                 ",
    "           $. $$$$$$$o.   ..          ",
    "          .$. $' $$$$$$ ,o''          ",
    "         .$'  $  '$$$$$,o'.,'   .oo ' ",
    "        .$'   $.   $$$$'  ,,  .o'.    ",
    "       .$'    '$o. 'O$ .. ooo''',oo ' ",
    "      .$'     .o$'  '$$''     ,,o'    ",
    "    .%$,,,,,ooO'      '       ,,o''   ",
    "  .$o.           ,o'    $o     ..oo'  ",
    "   ''O'''''''''','       $'$. .o'     "
};

//Borrowed from http://www.chris.com/ascii/index.php?art=cartoons/betty%20boop
StringVec boop =
{
    "        _(,__           __),          ",
    "    (_,d888888888b,d888888888b",
    "     d888888888888/888888888888b_)",
    "  (_8888888P'\"\"'`Y8Y`'\"\"'\"Y88888b",
    "    Y8888P.-'     `      '-.Y8888b_)",
    "   ,_Y88P (_(_(        )_)_) d88Y_,",
    "    Y88b,  (o  )      (o  ) d8888P",
    "    `Y888   '-'        '-'  `88Y`",
    "    ,d/O\\         c         /O\\b,",
    "      \\_/'.,______w______,.'\\_/",
    "         .-`             `-.",
    "        /   , d88b  d88b_   \\",
    "       /   / 88888bd88888`\\  \\",
    "      /   / \\ Y88888888Y   \\  \\",
    "      \\  \\   \\ 88888888    /  /",
    "       `\\ `.  \\d8888888b, /\\\\/",
    "         `.//.d8888888888b; |",
    "           |/d888888888888b/",
    "           d8888888888888888b",
    "        ,_d88p\"\"q88888p\"\"q888b,",
    "        `\"\"'`\\    \"`|    /`'\"\"`",
    "              `.    |===/",
    "                >   |   |",
    "                /   |   |",
    "               |    |   |",
    "               |    Y  /",
    "               \\   /  /",
    "         jgs    | /| /",
    "               / / / |",
    "              /=/  |=/"
};

void Write(const std::string& filename, StringVec& data)
{
    if(data.empty())
    {
        return;
    }

    std::ofstream out(filename);
    if(out)
    {
        out << data[0].size() << " x " << data.size() << "\n";
        for(size_t row = 0; row < data.size(); ++row)
        {
            const std::string& line = data[row];
            size_t col_start = 0;
            size_t col_end = 0;
            char col_char = line[0];
            for(size_t col = 0; col < line.size(); ++col)
            {
                //If we hit a new character write the previous run to the file
                if(col_char != line[col])
                {
                    //but only if it wasn't a run of spaces.
                    if(col_char != ' ')
                    {
                        out << row << ", " << col_start << ", " << col_char << ", " << col_end - col_start + 1 << "\n";
                    }
                    col_char = line[col];
                    col_start = col;
                    col_end = col;
                }
                col_end = col;
            }
            //write the last run
            if(col_char != ' ')
            {
                out << row << ", " << col_start << ", " << col_char << ", " << col_end - col_start + 1 << "\n";
            }
        }
    }
}

StringVec Read(const std::string& filename)
{
    StringVec data;
    std::ifstream in(filename);
    if(in)
    {
        char dummy;

        size_t width;
        size_t height;
        if(in >> width >> dummy >> height)
        {
            data.resize(height, std::string(width, ' '));
        }
        if(!data.empty())
        {
            size_t row;
            size_t col;
            char ch;
            size_t len;
            while(in >> row >> dummy >> col >> dummy >> ch >> dummy >> len)
            {
                for(size_t i = col; i < col + len; ++i)
                {
                    data[row][i] = ch;
                }
            }
        }
    }
    return data;
}

void Print(const StringVec& data)
{
    for(const std::string& s : data)
    {
        std::cout << s << "\n";
    }
    std::cout << "\n";
}

int main()
{
    Write("cupid.art", shapeCupid);
    Print(Read("cupid.art"));
    Write("boop.art", boop);
    Print(Read("boop.art"));
    return 0;
}
相关问题