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

C++ 如何将.art文件中的值转换为C++;,c++,ascii-art,C++,Ascii Art,在一个项目中,我应该根据用户输入加载一个文件,将该文件中的数据转换为窗口中的坐标,然后使用ASCII字符绘制图片 文件为.art格式,从所需窗口的宽度和高度开始,随后的每一行都是需要绘制的行、列、字符和计数。所以基本上,这个特殊的角色应该从什么位置开始,然后应该画多少次。 我正在努力解决的是如何将数据导入到可用的东西中,以便绘制所需的图像。我最初的想法是将数据作为一个4-D数组导入,但随后我对应该从那里开始的位置画了一个空白 .art文件的示例行: 50 x 25 2, 15, *, 9

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

文件为.art格式,从所需窗口的宽度和高度开始,随后的每一行都是需要绘制的行、列、字符和计数。所以基本上,这个特殊的角色应该从什么位置开始,然后应该画多少次。 我正在努力解决的是如何将数据导入到可用的东西中,以便绘制所需的图像。我最初的想法是将数据作为一个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;
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串形状PID[]=
{"                                      ",
"          ..                          ",
“$,o$$$o.”,
“$$$$$$o…”,
“$.$”$$$$,o”“,
“$'$'$$$,o','.oo'”,
“$.”.$$$”,.o.“,
“$''$o.'o$…ooo'',oo'”,
“$”.o$“$$”,o“,
“%$,,,,ooO”“,,o”“,
“$o,o'$o..o'”,
“‘O’、‘O’、‘O’、‘O’”;
字符串shapeFly=“Fly”;
字符串shapeHeart=“Heart”;
字符串shapeimpable=“不可能”;
字符串形状uss=“Seuss”;
string shapeWorry=“担心”;
int用户输入;
cout用户输入;
if(UserInput==1){

cout这是一个快速而肮脏的版本,可能会有所帮助。我没有添加大量的错误检查,但它适用于我使用的两个示例。如果您确实计划将数据作为字符串文本嵌入,您将需要确保转义任何特殊字符,如反斜杠和双引号。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;
}
#包括
#包括
#包括
#包括
使用StringVec=std::vector;
StringVec形状PID=
{
"                                      ",
"          ..                          ",
“$,o$$$o.”,
“$$$$$$o…”,
“$.$”$$$$,o”“,
“$'$'$$$,o','.oo'”,
“$.”.$$$”,.o.“,
“$''$o.'o$…ooo'',oo'”,
“$”.o$“$$”,o“,
“%$,,,,ooO”“,,o”“,
“$o,o'$o..o'”,
“O”“O”“O”“O”
};
//借来http://www.chris.com/ascii/index.php?art=cartoons/betty%20boop
StringVec boop=
{
"        _(,__           __),          ",
“(uu,D8888B,D8888B),
“D8888888/8888888888 B_”,
“('u888888 p'\“\'`Y8Y''\“\'\''\'\'\'\'\'y8888b”,
“Y8888P.''''-.Y8888b_'”,
“,Y88P((())uuD88Y,”,
“Y88b,(o)(o)d8888P”,
“`Y888'-'''`88Y`”,
,d/O\\c/O\\b,
“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,
"         .-`             `-.",
“/,d88b d88b\\”,
“//888 BD88888`\\\\\”,
“//\\Y8888Y\\\”,
"      \\  \\   \\ 88888888    /  /",
“\\\\\\\\\\\\\\\\\\D888B,/\\\\\\\\/”,
“`./.D8888B;|”,
“|/D8888B/”,
“D8888888888B”,
“,”d88p\“\”Q8888P\“\”q888b\”,
"        `\"\"'`\\    \"`|    /`'\"\"`",
"              `.    |===/",
"                >   |   |",
"                /   |   |",
"               |    |   |",
“|Y/”,
"               \\   /  /",
“jgs |/|/”,
"               / / / |",
"              /=/  |=/"
};
无效写入(常量std::string和filename、StringVec和data)
{
if(data.empty())
{
返回;
}
std::流输出(文件名);
如果(出)
{

out听起来像是一个有趣的项目。我会从变量开始,比如
大小
符号
,然后从中“计数”生成一个结构。然后你可以将数据拉到结构中,然后用
cout
解析它,也许只需一些指针就可以了。告诉我们你尝试了什么。我可能会使用字符串向量来表示缓冲区,并在解析文件时将其提取出来,然后逐行输出缓冲区。有很多方法可以解决这个问题。老实说,我还没有尝试过任何代码,编写了一些伪代码,但不确定如何实现它。Hellyue,我喜欢这个想法,看看我能从中得到什么。
int width、 
int height;
字符调色板[255][255];
将是一个很好的起点。