构造ASCII拱门


17

我喜欢ascii艺术,并且很无聊,所以我找到了一些ascii角色并开始制作随机物品,8位马里奥城堡,迷宫和拱门。我发现拱门可以轻松地以整齐的方式堆叠。

╔═══════╗
║╔═════╗║
║║╔═══╗║║
║║║╔═╗║║║
╨╨╨╨─╨╨╨╨

挑战

创建一个程序,函数或任何其他可接受大于或等于0的整数的标准格式(除非您正在做奖金),并输出具有指定足弓数量的ascii art。

测试用例

输入:

7

输出:

╔═════════════╗
║╔═══════════╗║
║║╔═════════╗║║
║║║╔═══════╗║║║
║║║║╔═════╗║║║║
║║║║║╔═══╗║║║║║
║║║║║║╔═╗║║║║║║
╨╨╨╨╨╨╨─╨╨╨╨╨╨╨

替代:

+-------------+
|+-----------+|
||+---------+||
|||+-------+|||
||||+-----+||||
|||||+---+|||||
||||||+-+||||||
||||||| |||||||
---------------

输入:

1

输出:

╔═╗
╨─╨

替代:

+-+
| |
---
  • 如果整数为0,则不输出任何内容
  • 这个问题将在utf-8中进行,每个字符都将计为一个“字节”
  • 这是因此最短的答案会获胜。
  • 您可以选择使用+-+代替╔═╗---代替╨─╨,和|代替

奖励(尚未决定是否允许在替代版本上使用,因为它不会那么难)

如果程序支持负数并像这样翻转拱门,则为-10%

╥╥╥╥─╥╥╥╥
║║║╚═╝║║║
║║╚═══╝║║
║╚═════╝║
╚═══════╝

3
AFAIK不是ASCII字符。unicode-art
瑕疵的2016年

哭,@ flawr你是对的。现在如何...
JuanPotato

世界将要崩溃!不用担心,也许只是提一下它们不是标准ASCII的一部分,但仍然使用ascii-art标签(unicode标签是个玩笑。)
瑕疵的2016年

不过,它看起来像扩展的ASCII,因此您可能还可以。
Mama Fun Roll

2
@ՊՓԼՃՐՊՃՈԲՍԼ没有扩展的ASCII的标准版本en.wikipedia.org/wiki/Extended_ASCII最接近的是codepage 437 en.wikipedia.org/wiki/Code_page_437,这在美国和许多其他国家都是标准的,但是我发现了将其复制并粘贴到代码页437编辑器中,然后再返回到Windows中,它“解释”为+---+顶部,侧面|和底部的拱形,-----对我来说不错。Juanpotato,如果要使用非ASCII字符,请在问题中指出编码。就目前情况而言,我投票决定关闭尚不清楚。
Level River St

Answers:



2

Python 2,106个字节(94个字符)

n=input();j=1
exec"s=j/2*'║';print s+'╔'+'═'*(2*n-j)+'╗'+s;j+=2;"*n
if n:t='╨'*n;print t+'─'+t

非常简单。逐行打印水平和垂直条数不断变化的图像。最后一行是单独打印的。

我觉得我缺少一些优化。字符是多个字节这一事实意味着您无法执行类似的操作'║╨'[n>0],因此我找不到在循环中打印最后一行的好方法。柜台上有如此多的操纵是很丑陋的。我想直接更新字符串,例如s+='║',但是索引也用于单杠。


现在,您可以+-|用来构建拱门,有关示例,请参见op。
JuanPotato

2
@JuanPotato OP代表原始海报。你是说这个问题吗?
Addison Crump

1
@flagasspam是的,我刚刚看过一些用法,它意味着原始帖子
JuanPotato

2

Perl,78个 82个字符

$n='─';$_='══'x pop;while(s/══//){print"$s╔═$_╗$s\n";$s.="║";$n="╨$n╨"}$s&&print$n

可悲的是,我想不出一种方法来利用奖金,而又不将奖金增加10%以上。我可能仍然会占上风。

不打高尔夫球

确实很简单。╨$n╨逐渐增加底线(),同时将顶线(══)缩短两个字符,并在无法再缩短时结束,因此我不必弄乱计数器。

 $n = '─'; # Bottom line
 $_ = '══'x pop; # "Top" line, length from commandline argument
 while (s/══//) { # Shorten top line by two characters
     print "$s╔═$_╗$s\n"; # Print current line with $s (sides)
     $s .= "║";           # Append vertical bar to sides
     $n  = "╨$n╨";        # Widen bottom line
 }
 $s && print $n; # Print bottom line if input is not 0

我认为这会打印一个n = 0 的单,但它应该什么也不打印。
林恩

@Mauris我刚运行它,而您是正确的
JuanPotato

1
@Mauris Dang!你是绝对正确的。我的原始版本很好,但是我丢失了支票。已修复,费用为4个字符。感谢您发现这一点。
type_outcast,2016年

我知道这很旧,但是要添加到@Abigail的注释中,您也可以使用以下方法保存字节-n在线尝试!
Dom Hastings

1

Bash,124个字节(112个字符)

printf -vh %$1s
b=${h// /╨}
h=${h// /═}
for((n=$1;n--;)){
echo $v$h${h:1}╗$v
h=${h#?}
v+=║
}
(($1))&&echo $b$b

样品运行:

bash-4.3$ bash ascii-arch.sh 7
╔═════════════╗
║╔═══════════╗║
║║╔═════════╗║║
║║║╔═══════╗║║║
║║║║╔═════╗║║║║
║║║║║╔═══╗║║║║║
║║║║║║╔═╗║║║║║║
╨╨╨╨╨╨╨─╨╨╨╨╨╨╨

bash-4.3$ bash ascii-arch.sh 1
╔═╗
╨─╨

bash-4.3$ bash ascii-arch.sh 0

1

Japt -R,29个字节

用途+-。牺牲4个字节来处理流血的输入验证!

©Æ'+²¬q-p´UÑÄÃpS û| p-pNÑÄ)ªP

尝试一下


说明

                                  :Implicit input of integer U
©                                 :Logical AND with U
 Æ                                :Map the range [0,U)
  '+                              :  Literal "+"
    ²                             :  Repeat twice
     ¬                            :  Split
      q                           :  Join with
       -                          :   Literal "-"
        p                         :   Repeat
         ´U                       :    Decrement U
           Ñ                      :    Multiply by 2
            Ä                     :    Add 1
             Ã                    :End mapping
              pS                  :Push a space
                 û|               :Centre pad each element with "|" to the length of the longest element
                    p     )       :Push
                     -            : Literal "-"
                      p           : Repeat
                       N          :  The array of inputs (which will be cast to an integer if we perform a mathematical operation on it)
                        ÑÄ        :  Multiply by 2 and add 1
                           ª      :Logical OR
                            P     :The empty string
                                  :Implicitly join with newlines and output

输入失败0
dzaima

@dzaima,你是什么意思?你怎么能得到一个0号的拱门?
毛茸茸的

If the integer is 0 then don't output anything来自挑战:/
dzaima

@dzaima,哦,我错过了。谢谢。首先:Boo缸输入验证!其次,Japt无法输出任何内容-我可以输出0false或者输出空字符串,但要花费一些字节,但是我不知道其中任何一个是否可以接受,除了空字符串,这可能会花费我5个字节(0只会花费我1)。
毛茸茸的

0

JavaScript(ES6),101个字符

f=(n,i=0)=>n?i-n?(b="║"[r="repeat"](i))+`╔${"═"[r]((n-i)*2-1)}╗${b}
`+f(n,i+1):(g="╨"[r](n))+"─"+g:""

说明

打印每一行的递归函数

f=(n,i=0)=>              // f = recursive function, i = current line (default = 0)
  n?                     // if n != 0
    i-n?                 // if we are not in the last line, print the line
      (b="║"[r="repeat"](i))+`╔${"═"[r]((n-i)*2-1)}╗${b}
`+f(n,i+1)               // add the output of the next line
    :(g="╨"[r](n))+"─"+g // if we ARE in the last line, print the last line
  :""                    // print nothing if n = 0

测试

测试不使用默认参数来实现浏览器兼容性。


0

PHP(109个字符)

$s='';for($b=($n=$argv[1])?'─':'';$n--;){echo$s.'╔═'.str_repeat('══',$n)."╗$s\n";$s.='║';$b="╨{$b}╨";}echo$b;

仍然需要摆脱该str_repeat,但是大多数替代方法都不能处理多字节字符。

$s = '';
// Initialise $b (bottom) to '─' or '' for n==0
for ($b = ($n = $argv[1]) ? '─' : ''; $n--;) {
    // Echo sides + arch + sides
    echo $s . '╔═' . str_repeat('══', $n) . "╗$s\n";
    // Growing sides
    $s .= '║';
    // Growing bottom
    $b = "╨{$b}╨";
}
// Show bottom
echo $b;

0

视网膜,79个字符

.+
$0$*═$0$*═╗
^═
╔
+`(║*)╔═(═+)═╗║*$
$0¶$1║╔$2╗║$1
(\S+)$
$0¶$1
T`═╔╗║`─╨`\S+$

在线尝试。

这使用了Retina中的一项新功能,该功能将十进制数字替换\d+为包含许多字符的列表$0$*═


0

迅捷(209字节)

Swift可能不是最好的语言,这是我第一次尝试进行代码高尔夫挑战:

func *(l:String,r: Int)->String{return r>0 ?l+(l*(r-1)):""}
let n=Int(readLine()!)!
for i in 0...(n-1){let a=("║"*i)+"╔═";let b=a+("══"*(n-1-i))+"╗"+("║"*i);print(b)};print("╨"*n+"─"+"╨"*n)

0

Ruby,90个字节(74个字符)

->n{n.times{|i|puts ?║*i+?╔+?═*((n-i)*2-1)+?╗+?║*i}>0&&puts(?╨*n+?─+?╨*n)}

样品运行:

2.1.5 :001 > ->n{n.times{|i|puts ?║*i+?╔+?═*((n-i)*2-1)+?╗+?║*i}>0&&puts(?╨*n+?─+?╨*n)}[7]
╔═════════════╗
║╔═══════════╗║
║║╔═════════╗║║
║║║╔═══════╗║║║
║║║║╔═════╗║║║║
║║║║║╔═══╗║║║║║
║║║║║║╔═╗║║║║║║
╨╨╨╨╨╨╨─╨╨╨╨╨╨╨
 => nil 

2.1.5 :002 > ->n{n.times{|i|puts ?║*i+?╔+?═*((n-i)*2-1)+?╗+?║*i}>0&&puts(?╨*n+?─+?╨*n)}[1]
╔═╗
╨─╨
 => nil 

2.1.5 :003 > ->n{n.times{|i|puts ?║*i+?╔+?═*((n-i)*2-1)+?╗+?║*i}>0&&puts(?╨*n+?─+?╨*n)}[0]
 => false 

0

哈斯克尔(151) 162字节

r=replicate
c=concat
f n=putStr$unlines[c[r i '║',"╔",r(2*(n-i)-1)'═',"╗",r i '║']|i<-[0..n-1]]++c[r n '╨',r(signum n)'─',r n '╨']
main=readLn>>=f

编辑:我忘了0作为输入处理


0

𝔼𝕊𝕄𝕚𝕟,54个字符/ 95个字节

⩥ïⓜᵖ⟮ ⍘|ď⟯$+`+⦃⟮⍘-ď (ï⟯-$)*2-1)}+`+Ⅰ$;ï⅋ᵖⅠï+⬭+Ⅰï,Ⅱ*2+1

Try it here (Firefox only).

说明

⩥ïⓜᵖ⟮ ⍘|ď⟯$+`+⦃⟮⍘-ď (ï⟯-$)*2-1)}+`+Ⅰ$;ï⅋ᵖⅠï+⬭+Ⅰï,Ⅱ*2+1 // implicit: ï=input, $=mapped item
                                                       // PHASE 1
⩥ïⓜ                                                   // create a range to map over
    ᵖ                                                  // push to stack:
     ⟮ ⍘|ď⟯$                                            // | repeated $ times
           +`+⦃⟮⍘-ď (ï⟯-$)*2-1)}+`                      // & +[- repeated 2$-1 times]+
                                 +Ⅰ$;                  // & | repeated $ times
                                                       // PHASE 2
                                     ï⅋                // if ï>0
                                       ᵖ               // push to stack 2 items:
                                        Ⅰï+⬭+Ⅰï,      // | repeated $ times & [space] & | repeated $ times
                                                 Ⅱ*2+1 // and - repeated 2ï+1
                                                       // implicit stack output, newline-separated

注意:这可以利用良好的ol'复制块来到达普通变量声明无法达到的位置。


0

Sed,97个字节(81个字符)

(96个字节(80个字符)的代码+ 1个字符的命令行选项)

s/.(.*)/2&\13/
t
:
H
s/(.+)11(.+)/4\1\24/
t
y/1234/─╨╨╨/
H
g
s/\n//
y/1234/═╔╗║/

输入应为一元整数。

样品运行:

bash-4.3$ sed -r 's/.(.*)/2&\13/;t;:;H;s/(.+)11(.+)/4\1\24/;t;y/1234/─╨╨╨/;H;g;s/\n//;y/1234/═╔╗║/' <<< '1111111'
╔═════════════╗
║╔═══════════╗║
║║╔═════════╗║║
║║║╔═══════╗║║║
║║║║╔═════╗║║║║
║║║║║╔═══╗║║║║║
║║║║║║╔═╗║║║║║║
╨╨╨╨╨╨╨─╨╨╨╨╨╨╨

bash-4.3$ sed -r 's/.(.*)/2&\13/;t;:;H;s/(.+)11(.+)/4\1\24/;t;y/1234/─╨╨╨/;H;g;s/\n//;y/1234/═╔╗║/' <<< '1'
╔═╗
╨─╨

bash-4.3$ sed -r 's/.(.*)/2&\13/;t;:;H;s/(.+)11(.+)/4\1\24/;t;y/1234/─╨╨╨/;H;g;s/\n//;y/1234/═╔╗║/' <<< ''

Sed,105个字节(75个字符)

(104字节(74个字符)的代码+ 1个字符的命令行选项)

y/1/═/
s/.(.*)/╔&\1╗/
t
:
H
s/(.+)══(.+)/║\1\2║/
t
y/╔║╗═/╨╨╨─/
H
g
s/\n//

输入应为一元整数。

样品运行:

bash-4.3$ sed -r 'y/1/═/;s/.(.*)/╔&\1╗/;t;:;H;s/(.+)══(.+)/║\1\2║/;t;y/╔║╗═/╨╨╨─/;H;g;s/\n//' <<< '1111111'
╔═════════════╗
║╔═══════════╗║
║║╔═════════╗║║
║║║╔═══════╗║║║
║║║║╔═════╗║║║║
║║║║║╔═══╗║║║║║
║║║║║║╔═╗║║║║║║
╨╨╨╨╨╨╨─╨╨╨╨╨╨╨

bash-4.3$ sed -r 'y/1/═/;s/.(.*)/╔&\1╗/;t;:;H;s/(.+)══(.+)/║\1\2║/;t;y/╔║╗═/╨╨╨─/;H;g;s/\n//' <<< '1'
╔═╗
╨─╨

bash-4.3$ sed -r 'y/1/═/;s/.(.*)/╔&\1╗/;t;:;H;s/(.+)══(.+)/║\1\2║/;t;y/╔║╗═/╨╨╨─/;H;g;s/\n//' <<< ''

0

画布,15 字节

-*+∔]⤢:↷±n│L-×∔

在这里尝试!

说明:

{    ]            map over 1..input
 -*                 repeat "-" counter times
   +∔               append "+" to that
      ⤢           transpose
       :          create a duplicate of that
        ↷±        rotated 90°, then reversed horizontally
          n       overlap the 2
           |      and palindromize horizontally with 1 overlap
            L     get the with of that
             -×   repear "-" that many times
               ∔  and add vertically to the rest of the output
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.