简体   繁体   English

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

[英]How to convert values from .art file into ASCII art in C++

Working on a project where I am supposed to load a file based on user input, convert the data in that file into coordinates in the window and then use ASCII characters to draw a picture. 在一个我应该根据用户输入来加载文件的项目上工作,将该文件中的数据转换为窗口中的坐标,然后使用ASCII字符绘制图片。

Files are in .art and start with the width and height of the window needed and each subsequent line is the ROW, COLUMN, CHARACTER, and COUNT that needs to be drawn. 文件位于.art中,并从所需窗口的宽度和高度开始,随后的每一行都是需要绘制的ROW,COLUMN,CHARACTER和COUNT。 So basically the position the particular character should be started at and then how many times it should be drawn. 因此,基本上应该从特定角色开始的位置开始,然后绘制多少次。 What I am struggling with is how to import that data into something usable so as I can draw the requested image. 我正在努力的是如何将数据导入可用的东西,以便绘制所需的图像。 My initial thought is to import the data as a 4-D array but then I draw a blank as to where I should go from there. 我最初的想法是将数据导入为4-D数组,但是随后我对应该从那里去的位置画了一个空白。

Example lines for a .art file: .art文件的示例行:

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

UPDATE: Given up on trying to load the files since I just can't wrap my head around that so I've changed to just drawing the art by hand and calling a string but I'm even having issues with that. 更新:放弃尝试加载文件,因为我无法解决这个问题,所以我已更改为仅手工绘制图稿并调用字符串,但是我什至遇到了问题。 With my code below, if I select option 1 the output is 001FFA80 rather than outputting the ASCII art that is in the string. 在下面的代码中,如果我选择选项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;

} }

Here's a quick and dirty version that might help. 这是一个快速而肮脏的版本,可能会有所帮助。 I didn't add a ton of error checking, but it works for the 2 examples I used. 我没有添加大量错误检查,但是它适用于我使用的2个示例。 If you do plan to embed the data as string literals you'll want to be sure to escape any special characters like backslash and double quotes. 如果确实打算将数据嵌入为字符串文字,则需要确保转义任何特殊字符,例如反斜杠和双引号。 The Boop example has both. Boop示例兼有。

For the embedded string literal data the first line must be padded to be as long as the longest line since it is used to write the width. 对于嵌入的字符串文字数据,第一行必须填充为最长的行,因为它用于写入宽度。 An enhancement would be to iterate through all lines to get the max width and use that. 一种增强功能是遍历所有行以获得最大宽度并使用该最大宽度。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM