Created
October 8, 2011 08:03
-
-
Save pasberth/1272003 to your computer and use it in GitHub Desktop.
テトリスの図形などの設定記述のための言語
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 文字列から、' ' を除くすべての文字をtrue、 ' ' のみを false に置き換えた二次元配列を作ります。 | |
| # コメントは '__' から始めます。そのあとから行末までのすべての文字は無視されます | |
| # テトリスとかの図形を作る時に使います | |
| module ShapeLang | |
| extend self | |
| # *** // | |
| # * * // | |
| # *** // | |
| # to | |
| # [ | |
| # [true, true, true], | |
| # [true, false, true], | |
| # [true, true, true] | |
| # ] | |
| def parse src | |
| src.split("\n").map do |line| | |
| line = $1 if line =~ /\A(.*?)\_\_.*\z/ | |
| Array.new(line.length) { |i| line[i] != ' ' } | |
| end | |
| end | |
| end | |
| if __FILE__ == $PROGRAM_NAME | |
| require "test/unit" | |
| class T < Test::Unit::TestCase | |
| def test_1 | |
| assert_equal [ | |
| [true, true, true], | |
| [true, false, true], | |
| [true, true, true] | |
| ], ShapeLang.parse(<<SRC) | |
| *** | |
| * * | |
| *** | |
| SRC | |
| end | |
| def test_comment | |
| assert_equal [ | |
| [true, true, true], | |
| [true, false, true], | |
| [true, true, true] | |
| ], ShapeLang.parse(<<SRC) | |
| ***__ comment | |
| * *__ * * | |
| ***__ * | |
| SRC | |
| end | |
| def test_tetoris | |
| assert_equal [ | |
| [true, true], | |
| [true, true] | |
| ], ShapeLang.parse(<<SRC) | |
| **__ | |
| **__ | |
| SRC | |
| assert_equal [ | |
| [true, false, false], | |
| [true, false, false], | |
| [true, true, false] | |
| ], ShapeLang.parse(<<SRC) | |
| * __ | |
| * __ | |
| ** __ | |
| SRC | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment