如何为文本编写ASCII艺术(使用任何语言)?

时间:2016-10-11 17:58:16

标签: javascript node.js ascii-art

我想编写一个程序来打印以下文本模式" NUOSPIN":

* * *  *   *  * * *  * * *  * * *  *  * * *
*   *  *   *  *   *  *      * * *  *  *   *
*   *  *   *  *   *  * * *  *      *  *   *
*   *  * * *  * * *  * * *  *      *  *   *

我正在研究Javascript(NodeJS)。我必须在控制台上输出它。任何语言的实现都适用于我,包括java,C,javascript。我知道我可以通过使用预构建的npm包(比如asciify)来做到这一点,但我想这样做。我的方法应该是什么?我是否必须分别为每个角色的模式编写代码?然后通过逐个运行每个角色模式的逻辑来打印它们?

3 个答案:

答案 0 :(得分:2)

通常,您会在某个地方使用大型地图将字符映射到其外观,尤其是对于此类自定义字体。然后,您将查看输入字符串,查找图片中的字符,将其放入缓冲区,并在完成后打印该缓冲区。

您也可以通过逐行打印直接打印到屏幕,查找每个角色的第一行,打印,查找每个角色的第二行等等来完成此操作。

如果你想获得真正的创意,你也可以用特定的字体在文本的内存中绘制图像,然后用星号和空格将该图像绘制到控制台。

答案 1 :(得分:2)

您可以使用位图并将点组合成ASCII样式。

function getWord(s) {
    var ascii = [],
        font = {
            N: [7, 5, 5, 5],
            U: [5, 5, 5, 7],
            O: [7, 5, 5, 7],
            S: [7, 4, 7, 7],
            P: [7, 7, 4, 4],
            I: [1, 1, 1, 1]
        };

    s.split('').forEach(function (c) {
        var size =( font[c].reduce(function (r, a) {
            return r | a;
        }, 0)).toString(2).length;

        font[c].forEach(function (a, i) {
            var temp = a.toString(2).split('').map(function (c) {
                return +c ? '*' : ' ';
            });
            while (temp.length < size) {
                temp.unshift(' ');
            }
            ascii[i] = ascii[i] || [];
            ascii[i].push(temp.join(''));
        });
    });
    return ascii.map(function (a) {
        return a.join(' ');
    }).join('\n')
}

document.getElementById('tt').innerHTML = getWord('NUOSPIN');
<pre id="tt"></pre>

答案 2 :(得分:1)

每行文字都必须写几次。 首先是ASCII艺术字形的上行。

class Reminder < ActiveRecord::Base
  belongs_to :user
  before_save :create_mail_date
  after_create :send_reminder_emails
  extend FriendlyId
  friendly_id :name, use: [:slugged, :finders]

  private

  def create_mail_date
    mail_date = date - 7.days
  end

  # this method doesn't make sense! Does a reminder have multiple reminders?
  # maybe you are looking for something like this:
  def send_reminder_emails
    Reminder.all.each do |reminder|
        ReminderMailer.reminder_send(reminder.user, reminder).delay(run_at: reminder.mail_date)
        reminder.set_new_dates
    end
  end

  def set_new_dates
    case repeating
    when "Weekly"
        date = date + 1.week
    when "Monthly"
        date = date + 1.month
    when "Yearly"
        date = date + 1.year
    end
    create_mail_date
  end
end

给我

#!/usr/bin/python

import sys

# Each item in `font` is a list of `font_height` strings.
# The glyphs are not limited to asterisks and spaces.
font_height = 7
margin_left = 1
margin_right = 1
font = {
    'N': [
        "*   *",
        "*   *",
        "**  *",
        "* * *",
        "*  **",
        "*   *",
        "*   *",
    ],
    'U': [
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        " *** ",
    ],
    'O': [
        " *** ",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        " *** ",
    ],
    'S': [
        " *** ",
        "*   *",
        "*    ",
        " *** ",
        "    *",
        "*   *",
        " *** ",
    ],
    'P': [
        "**** ",
        "*   *",
        "*   *",
        "**** ",
        "*    ",
        "*    ",
        "*    ",
    ],
    'I': [
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
    ],
}

def printline(text):
    for row in range(font_height):
        for ch in text:
            sys.stdout.write(' ' * margin_left)
            sys.stdout.write(font[ch][row])
            sys.stdout.write(' ' * margin_right)
        sys.stdout.write('\n')

printline('NUOSPIN')