C64 代码艺术的 Bash 版本:10 PRINT CHR$(205.5+RND(1));: 转到 10
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13611501/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash Version of C64 Code Art: 10 PRINT CHR$(205.5+RND(1)); : GOTO 10
提问by spex
I picked up a copy of the book 10 PRINT CHR$(205.5+RND(1)); : GOTO 10
我拿起一本书10 PRINT CHR$(205.5+RND(1)); : 转到 10
This book discusses the art produced by the single line of Commodore 64 BASIC:
本书讨论了Commodore 64 BASIC单行产生的艺术:
10 PRINT CHR$(205.5+RND(1)); : GOTO 10
This just repeatedly prints randomly character 205 or 206 to the screen from the PETSCII set:
这只是重复地从 PETSCII 集中将字符 205 或 206 随机打印到屏幕上:
I'm not sure why the original uses the characters 205 and 206 instead of the identical 109 and 110. Also, I prefer to add a clear at the beginning. This is what I usually type into the C64:
我不确定为什么原始使用字符 205 和 206 而不是相同的 109 和 110。另外,我更喜欢在开头添加一个清除。这是我通常在 C64 中输入的内容:
1?CHR$(147)
2?CHR$(109.5+RND(1));:GOTO2
RUN
You can try this all for yourself in an emulator, such as this one using Flash or JavaScript:
您可以在模拟器中亲自尝试这一切,例如使用 Flash 或 JavaScript 的模拟器:
- http://codeazur.com.br/stuff/fc64_final/
- http://www.kingsquare.nl/jsc64 - When inputting the above code into the emulators listed, you'll need to realize that - ( is *
- ) is (
- + is ]
 
- http://codeazur.com.br/stuff/fc64_final/
- http://www.kingsquare.nl/jsc64 - 将上述代码输入到列出的模拟器中时,您需要意识到 - ( 是 *
- ) 是 (
- + 是 ]
 
I decided it would be amusing to write a bash line to do something similar.
我决定写一个 bash 行来做类似的事情会很有趣。
I currently have:
我目前有:
clear; while :; do [ $(($RANDOM%2)) -eq 0 ] && (printf "\") || (printf "/"); done;
Two questions:
两个问题:
- Any suggestions for making this more concise?
- Any suggestions for a better output character? The forward and backward slash are not nearly as beautiful since their points don't line up. The characters used from PETSCII are special characters, not slashes. I didn't see anything in ASCII that could work as well, but maybe you can suggest a way to pull in a character from UTF-8 or something else?
- 有什么建议可以使这更简洁吗?
- 对更好的输出字符有什么建议吗?正斜杠和反斜杠几乎没有那么漂亮,因为它们的点不对齐。PETSCII 中使用的字符是特殊字符,而不是斜杠。我在 ASCII 中没有看到任何可以正常工作的东西,但是也许您可以建议一种从 UTF-8 或其他东西中提取字符的方法?
Best ANSWERS So Far
迄今为止最好的答案
Shortest for bash (40 characters):
bash 最短(40 个字符):
yes 'c=(╱ ╲);printf ${c[RANDOM%2]}'|bash
Here is a short one for zsh (53 characters):
以下是 zsh 的简短代码(53 个字符):
c=(╱ ╲);clear;while :;do printf ${c[RANDOM%2+1]};done
Here is an alias I like to put in my .bashrc or .profile
这是我喜欢放在 .bashrc 或 .profile 中的别名
alias art='c=(╱ ╲);while :;do printf "%s" ${c[RANDOM%2]};done'
Funny comparing this to the shortest I can do for C64 BASIC (23 characters):
将此与我可以为 C64 BASIC(23 个字符)所做的最短时间进行比较很有趣:
1?C_(109.5+R_(1));:G_1
The underscores are shift+H, shift+N, and shift+O respectively. I can't paste the character here since they are specific to PETSCII. Also, the C64 output looks prettier ;)
下划线分别是 shift+H、shift+N 和 shift+O。我无法在此处粘贴该字符,因为它们特定于 PETSCII。此外,C64 输出看起来更漂亮;)
You can read about the C64 BASIC abbreviations here:
您可以在此处阅读有关 C64 BASIC 缩写的信息:
采纳答案by gniourf_gniourf
How about this?
这个怎么样?
# The characters you want to use
chars=( $'\xe2\x95\xb1' $'\xe2\x95\xb2' )
# Precompute the size of the array chars
nchars=${#chars[@]}
# clear screen
clear
# The loop that prints it:
while :; do
    printf -- "${chars[RANDOM%nchars]}"
done
As a one-liner with shorter variable names to make it more concise:
作为具有较短变量名称的单行代码,使其更简洁:
c=($'\xe2\x95\xb1' $'\xe2\x95\xb2'); n=${#c[@]}; clear; while :; do printf -- "${c[RANDOM%n]}"; done
You can get rid of the loop if you know in advance how many characters to print (here 80*24=1920)
如果您事先知道要打印多少个字符,则可以摆脱循环(此处为 80*24=1920)
c=($'\xe2\x95\xb1' $'\xe2\x95\xb2'); n=${#c[@]}; clear; printf "%s" "${c[RANDOM%n]"{1..1920}"}"
Or, if you want to include the characters directly instead of their code:
或者,如果您想直接包含字符而不是它们的代码:
c=(╱? ╲); n=${#c[@]}; clear; while :; do printf "${c[RANDOM%n]}"; done
Finally, with the size of the array cprecomputed and removing unnecessary spaces and quotes (and I can't get shorter than this):
最后,c预先计算数组的大小并删除不必要的空格和引号(我不能比这更短):
c=(╱? ╲);clear;while :;do printf ${c[RANDOM%2]};done
Number of bytes used for this line:
此行使用的字节数:
$ wc -c <<< 'c=(╱? ╲);clear;while :;do printf ${c[RANDOM%2]};done'
59
Edit.A funny way using the command yes:
编辑。使用命令的一种有趣方式yes:
clear;yes 'c=(╱ ╲);printf ${c[RANDOM%2]}'|bash
It uses 50 bytes:
它使用 50 个字节:
$ wc -c <<< "clear;yes 'c=(╱ ╲);printf ${c[RANDOM%2]}'|bash"
51
or 46 characters:
或 46 个字符:
$ wc -m <<< "clear;yes 'c=(╱ ╲);printf ${c[RANDOM%2]}'|bash"
47
回答by paultag
After looking at some UTF stuff:
在查看了一些 UTF 内容之后:
2571 BOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT
2572 BOX DRAWINGS LIGHT DIAGONAL UPPER LEFT TO LOWER RIGHT
(╱? and ╲) seem best.
(╱?和╲)似乎最好。
f="╱╲";while :;do print -n ${f[(RANDOM % 2) + 1]};done
also works in zsh (thanks Clint on OFTC for giving me bits of that)
也适用于 zsh(感谢 OFTC 上的 Clint 给了我一些)
回答by deltaray
Here is my 39 character command line solution I just posted to @climagic:
这是我刚刚发布到@climagic 的39 个字符的命令行解决方案:
grep -ao "[/\]" /dev/urandom|tr -d \n
In bash, you can remove the double quotes around the [/\] match expression and make it even shorter than the C64 solution, but I've included them for good measure and cross shell compatibility. If there was a 1 character option to grep to make grep trim newlines, then you could make this 27 characters.
在 bash 中,您可以删除 [/\] 匹配表达式周围的双引号,使其比 C64 解决方案更短,但我将它们包含在内是为了更好的度量和跨 shell 兼容性。如果 grep 有一个 1 个字符的选项来使 grep 修剪换行符,那么您可以创建 27 个字符。
I know this doesn't use the Unicode characters so maybe it doesn't count. It is possible to grep for the Unicode characters in /dev/urandom, but that will take a long time because that sequence comes up less often and if you pipe it the command pipeline will probably "stick" for quite a while before producing anything due to line buffering.
我知道这不使用 Unicode 字符,所以可能不算数。可以对 /dev/urandom 中的 Unicode 字符进行 grep,但这将需要很长时间,因为该序列出现的频率较低,如果您通过管道传输它,命令管道可能会“坚持”一段时间,然后才会产生任何到期的东西到线路缓冲。
回答by philcolbourn
Bash supports Unicode now, so we don't need to use UTF-8 character sequences such as $'\xe2\x95\xb1'.
Bash 现在支持 Unicode,所以我们不需要使用 $'\xe2\x95\xb1' 等 UTF-8 字符序列。
This is my most-correctversion: it loops, prints either / or \ based on a random number as others do.
这是我最正确的版本:它循环,像其他人一样基于随机数打印 / 或 \ 。
for((;;x=RANDOM%2+2571)){ printf "\U$x";}
41
My previous best was:
我以前最好的是:
while :;do printf "\U257"$((RANDOM%2+1));done
45
And this one 'cheats' using embedded Unicode (I think for obviousness, maintainability, and simplicity, this is my favourite).
而这个使用嵌入式 Unicode 的“作弊”(我认为为了显而易见性、可维护性和简单性,这是我的最爱)。
Z=╱╲;for((;;)){ printf ${Z:RANDOM&1:1};}
40
My previous best was:
我以前最好的是:
while Z=╱╲;do printf ${Z:RANDOM&1:1};done
41
And here are some more.
还有一些。
while :;do ((RANDOM&1))&&printf "\U2571"||printf "\U2572";done
while printf -v X "\\U%d" $((2571+RANDOM%2));do printf $X;done
while :;do printf -v X "\\U%d" $((2571+RANDOM%2));printf $X;done
while printf -v X '\U%d' $((2571+RANDOM%2));do printf $X;done
c=('\U2571' '\U2572');while :;do printf ${c[RANDOM&1]};done
X="\U257";while :;do printf $X$((RANDOM%2+1));done
Now, this one runs until we get a stack overflow (not another one!) since bash does not seem to support tail-call elimination yet.
现在,这个会一直运行直到出现堆栈溢出(不是另一个!),因为 bash 似乎还不支持尾调用消除。
f(){ printf "\U257"$((RANDOM%2+1));f;};f
40
And this is my attempt to implement a crude form of tail-process elimination. But when you have had enough and press ctrl-c, your terminal will vanish.
这是我尝试实现一种粗略形式的尾部过程消除。但是当你玩够了并按下 ctrl-c 后,你的终端就会消失。
f(){ printf "\U257"$((RANDOM%2+1));exec bash -c f;};export -f f;f
UPDATE:
更新:
And a few more.
还有一些。
X=(╱ ╲);echo -e "\b${X[RANDOM&1]"{1..1000}"}" 46
X=("\U2571" "\U2572");echo -e "\b${X[RANDOM&1]"{1..1000}"}" 60
X=(╱ ╲);while :;do echo -n ${X[RANDOM&1]};done 46
Z=╱╲;while :;do echo -n ${Z:RANDOM&1:1};done 44
回答by lierdakil
Sorry for necroposting, but here's bash version in 38 characters.
抱歉发布了 necroposting,但这里是 38 个字符的 bash 版本。
yes 'printf \u$[2571+RANDOM%2]'|bash
using forinstead of yesinflates this to 40 characters:
使用for而不是将其yes膨胀为 40 个字符:
for((;;)){ printf \u$[2571+RANDOM%2];}
回答by lierdakil
#!/usr/bin/python3
import random
import sys
while True:
    if random.randrange(2)==1:sys.stdout.write("\u2571")
    else:sys.stdout.write("\u2572")
    sys.stdout.flush()
回答by Baby Birb
109 chr for Python 3 Which was the smallest I could get it.
109 chr for Python 3 这是我能得到的最小的。
#!/usr/bin/python3
import random
while True:
    if random.randrange(2)==1:print('\u2572',end='') 
    else:print('\u2571',end='')

