Code Golf:玩俄罗斯方块 [英] Code Golf: Playing Tetris

查看:91
本文介绍了Code Golf:玩俄罗斯方块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下四联骨牌和空白的比赛场地:

Consider the following tetrominoes and empty playing field:


                                            0123456789
    I   O    Z    T    L    S    J         [          ]
                                           [          ]
    #   ##   ##   ###  #     ##   #        [          ]
    #   ##    ##   #   #    ##    #        [          ]
    #                  ##        ##        [          ]
    #                                      [          ]
                                           [==========]

比赛场地的尺寸是固定的.顶部的数字就在这里 指示列号(另请参见输入).

The dimensions of the playing field are fixed. The numbers at the top are just here to indicate the column number (also see input).

1 .您将获得一个特定的比赛场地(基于上述内容),已经可以部分填充 带有tetrominoes(可以在单独的文件中,也可以通过stdin提供).

1. You are given a specific playing field (based on the above) which can already be filled partly with tetrominoes (this can be in a separate file or provided via stdin).

示例输入:


[          ]
[          ]
[          ]
[          ]
[ #    #  #]
[ ## ######]
[==========]

2 .系统会为您提供一个字符串,该字符串描述(以空格分隔)要插入的Tetromino(和 下拉)在哪一列. Tetrominoes不需要旋转.输入可以从标准输入中读取.

2. You are given a string which describes (separated by spaces) which tetromino to insert (and drop down) at which column. Tetrominoes don't need to be rotated. Input can be read from stdin.

示例输入:

T2 Z6 I0 T7

您可以假定输入是格式正确的"(或者在输入不正确时会产生未定义的行为).

You can assume input is 'well-formed' (or produce undefined behaviour when it's not).

渲染结果字段(完整"行必须消失)并打印分数计数 (每条下线占10点).

Render the resulting field ('full' lines must disappear) and print the score count (every dropped line accounts for 10 points).

基于上述示例输入的示例输出:


[          ]
[          ]
[          ]
[#      ###]
[#     ### ]
[##### ####]
[==========]
10

优胜者:

最短的解决方案(按代码字符计数).用法示例很好.打高尔夫球吧!

Winner:

Shortest solution (by code character count). Usage examples are nice. Have fun golfing!

编辑:添加了+500声誉的赏金,以吸引更多注意力到应答者已经做出的出色努力(以及可能对此问题的一些新解决方案)...

Edit: added a bounty of +500 reputation to draw some more attention to the nice efforts the answerers already made (and possibly some new solutions to this question)...

推荐答案

GolfScript-181个字符

不需要换行符.输出在标准输出中,尽管stderr中存在一些错误.
\10应该用相应的ASCII字符代替,程序应为181个字符.

GolfScript - 181 characters

Newlines are not necessary. Output is in standard output, although some errors are present in stderr.
\10 should be replaced by the corresponding ASCII character for the program to be 181 characters.

{):X!-{2B{" #"=}%X" ":f*+-1%}%:P;:>.{\!:F;>P{\(@{3&\(@.2$&F|:F;|}%\+}%\+F![f]P+:P
;}do;{"= "&},.,7^.R+:R;[>0="#"/f*]*\+}0"R@1(XBc_""~\10"{base}:B/3/~4*"nIOZTLSJR "
";:"*~;n%)n*~ 10R*+n*

I/O示例:

$ cat inp
[          ]
[          ]
[          ]
[          ]
[ #    #  #]
[ ## ######]
[==========]
T2 Z6 I0 T7
$ cat inp|golfscript tetris.gs 2>/dev/null
[          ]
[          ]
[          ]
[#      ###]
[#     ### ]
[##### ####]
[==========]
10

Temino压缩:
件以三个基数8位存储.这是一个简单的二进制表示形式,例如T=[7,2,0], S=[6,3,0], J=[2,2,3]. [1]用于压缩中的I片段,但稍后将其显式设置为[1,1,1,1](即代码中的4*).所有这些数组都串联到一个数组中,该数组先转换为整数,然后转换为字符串(以126为基数,以最大程度地减少不可打印的字符,长度,并且不会遇到utf8).该字符串非常短:"R@1(XBc_".

Tetromino compression:
Pieces are stored as three base 8 digits. This is a simple binary representation, e.g.T=[7,2,0], S=[6,3,0], J=[2,2,3]. [1] is used for the I piece in compression, but this is explicitly set to [1,1,1,1] later (i.e. the 4* in the code). All of these arrays are concatenated into a single array, which is converted into an integer, and then a string (base 126 to minimize non-printable characters, length, and not encounter utf8). This string is very short: "R@1(XBc_".

然后减压很简单.我们首先进行以​​126为基的转换,然后以8为基的转换( "~\10"{base}/ ,即遍历 "~\10" 并对每个元素进行基础转换).结果数组分为3组,I的数组是固定的( 3/~4* ).然后,我们将每个元素转换为以2为基数,并(在除去零之后)将每个二进制数字替换为字符串" #"( 2base{" #"=}%...-1% 中的索引的字符)-注意,我们需要反转数组否则2将会变成"# "而不是" #").

Decompression is then straightforward. We first do a base 126 conversion followed by a base 8 conversion ("~\10"{base}/, i.e. iterate through "~\10" and do a base conversion for each element). The resulting array is split into groups of 3, the array for I is fixed (3/~4*). We then convert each element to base 2 and (after removing zeros) replace each binary digit with the character of that index in the string " #" (2base{" #"=}%...-1% - note that we need to reverse the array otherwise 2 would become "# " instead of " #").

木板/棋子格式,丢下棋子
电路板只是一个字符串数组,每行一个.最初没有完成任何工作,因此我们可以在输入中使用 n/( 生成它.片段也是字符串的数组,在它们的X位置的左侧填充空格,但不带空格.通过在数组前面放置并连续测试是否存在碰撞来丢弃碎片.

Board/piece format, dropping pieces
The board is simply an array of strings, one for each line. No work is initially done on this, so we can generate it with n/( on the input. Pieces are also arrays of strings, padded with spaces to the left for their X position, but without trailing spaces. Pieces are dropped by prepending to the array, and continuously testing whether there is a collision.

碰撞测试是通过迭代棋子中的所有字符并与板上相同位置的字符进行比较来完成的.我们希望将# + =# + #视为冲突,因此我们测试((piecechar& 3)& boardchar)是否为非零.在进行此迭代时,我们还使用((piecechar& 3)| boardchar)更新了板的副本(副本),从而正确设置了对# + + # + [.如果将木板向下移动另一排后发生碰撞,我们将使用此更新的木板.

Collision testing is done by iterating through all characters in the piece, and comparing against the character of the same position on the board. We want to regard #+= and #+# as collisions, so we test whether ((piecechar&3)&boardchar) is nonzero. While doing this iteration, we also update (a copy of) the board with ((piecechar&3)|boardchar), which correctly sets the value for pairs #+, +#, +[. We use this updated board if there is a collision after moving the piece down another row.

删除填充行非常简单.我们删除 "= "& 返回false的所有行.填充的行将没有=,因此连接将是一个空白字符串,等同于false.然后,我们计算已删除的行数,将计数添加到分数中,并添加许多"[ ... ]".我们通过获取网格的第一行并将#替换为,来紧凑地生成此代码.

Removing filled rows is quite simple. We remove all rows for which "= "& return false. A filled row will have neither = or , so the conjunction will be a blank string, which equates to false. Then we count the number of rows that have been removed, add the count to the score and prepend that many "[ ... ]"s. We generate this compactly by taking the first row of the grid and replacing # with .

奖金
由于我们可以计算板在跌落时每个位置的样子,因此我们可以将其保留在堆栈中,而不是删除它们!对于总共三个字符,我们可以输出所有这些位置(如果板状态为单个间距,则可以输出两个字符).

Bonus
Since we compute what the board would look like in each position of the piece as it falls, we can keep these on the stack instead of deleting them! For a total of three characters more, we can output all these positions (or two characters if we have the board states single spaced).

{):X!-{2B{" #"=}%X" ":f*+-1%}%:P;:>.{>[f]P+:P(!:F;{\(@{3&\(@.2$&F|:F;|}%\+}%\+F!}
do;{"= "&},.,7^.R+:R;[>0="#"/f*]*\+}0"R@1(XBc_""~\10"{base}:B/3/~4*"nIOZTLSJR "
";:"*~;n%)n*~ ]{n*n.}/10R*

这篇关于Code Golf:玩俄罗斯方块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆