Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created October 8, 2011 08:03
Show Gist options
  • Select an option

  • Save pasberth/1272003 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1272003 to your computer and use it in GitHub Desktop.
テトリスの図形などの設定記述のための言語
# 文字列から、' ' を除くすべての文字を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