Skip to content

Instantly share code, notes, and snippets.

@tyabe
Created October 14, 2012 16:17
Show Gist options
  • Save tyabe/3889064 to your computer and use it in GitHub Desktop.
Save tyabe/3889064 to your computer and use it in GitHub Desktop.
module Tetoromino
def self.parse(coordinate)
before = coordinate.split(',').sort
mx, my = before.map{|c|c.split(//)}.transpose.map(&:sort).map(&:first)
coordinate = before.map { |o| "#{o[0].to_i - mx.to_i}#{o[1].to_i - my.to_i}" }.join(',')
ptn_l = %w[
00,01,02,12
00,01,10,20
00,10,11,12
01,11,20,21
02,10,11,12
00,01,11,21
00,01,02,10
00,10,20,21
]
ptn_i = %w[
00,01,02,03
00,10,20,30
]
ptn_t = %w[
00,10,11,20
01,10,11,12
01,10,11,21
00,01,02,11
]
ptn_o = %w[
00,01,10,11
]
ptn_s = %w[
00,01,11,12
01,10,11,20
01,02,10,11
00,10,11,21
]
return 'L' if ptn_l.include?(coordinate)
return 'I' if ptn_i.include?(coordinate)
return 'T' if ptn_t.include?(coordinate)
return 'O' if ptn_o.include?(coordinate)
return 'S' if ptn_s.include?(coordinate)
'-'
end
end
require 'minitest/autorun'
require 'minitest/spec'
begin
require 'turn/autorun'
Turn.config.format = :outline
rescue LoadError
end
MiniTest::Spec.class_eval do
def self.shared_examples
@shared_examples ||= {}
end
end
module MiniTest::Spec::SharedExamples
def shared_examples_for(desc, &block)
MiniTest::Spec.shared_examples[desc] = block
end
def it_behaves_like(desc, *args)
self.instance_eval do
MiniTest::Spec.shared_examples[desc].call(args)
end
end
end
Object.class_eval { include(MiniTest::Spec::SharedExamples) }
shared_examples_for 'parse' do |coordinate, pattern|
describe "parse" do
it("#{coordinate}"){ Tetoromino.parse(coordinate).must_equal pattern }
end
end
describe Tetoromino do
it_behaves_like "parse", "55,55,55,55", "-"
it_behaves_like "parse", "07,17,06,05", "L"
it_behaves_like "parse", "21,41,31,40", "L"
it_behaves_like "parse", "62,74,73,72", "L"
it_behaves_like "parse", "84,94,74,75", "L"
it_behaves_like "parse", "48,49,57,47", "L"
it_behaves_like "parse", "69,89,79,68", "L"
it_behaves_like "parse", "90,82,91,92", "L"
it_behaves_like "parse", "13,23,03,24", "L"
it_behaves_like "parse", "24,22,25,23", "I"
it_behaves_like "parse", "51,41,21,31", "I"
it_behaves_like "parse", "64,63,62,65", "I"
it_behaves_like "parse", "49,69,59,79", "I"
it_behaves_like "parse", "12,10,21,11", "T"
it_behaves_like "parse", "89,99,79,88", "T"
it_behaves_like "parse", "32,41,43,42", "T"
it_behaves_like "parse", "27,16,36,26", "T"
it_behaves_like "parse", "68,57,58,67", "O"
it_behaves_like "parse", "72,62,61,71", "O"
it_behaves_like "parse", "25,24,15,14", "O"
it_behaves_like "parse", "43,54,53,42", "S"
it_behaves_like "parse", "95,86,76,85", "S"
it_behaves_like "parse", "72,73,84,83", "S"
it_behaves_like "parse", "42,33,32,23", "S"
it_behaves_like "parse", "66,57,67,58", "S"
it_behaves_like "parse", "63,73,52,62", "S"
it_behaves_like "parse", "76,68,77,67", "S"
it_behaves_like "parse", "12,11,22,01", "S"
it_behaves_like "parse", "05,26,06,25", "-"
it_behaves_like "parse", "03,11,13,01", "-"
it_behaves_like "parse", "11,20,00,21", "-"
it_behaves_like "parse", "84,95,94,86", "-"
it_behaves_like "parse", "36,56,45,35", "-"
it_behaves_like "parse", "41,33,32,43", "-"
it_behaves_like "parse", "75,94,84,95", "-"
it_behaves_like "parse", "27,39,28,37", "-"
it_behaves_like "parse", "45,34,54,35", "-"
it_behaves_like "parse", "24,36,35,26", "-"
it_behaves_like "parse", "27,27,27,27", "-"
it_behaves_like "parse", "55,44,44,45", "-"
it_behaves_like "parse", "70,73,71,71", "-"
it_behaves_like "parse", "67,37,47,47", "-"
it_behaves_like "parse", "43,45,41,42", "-"
it_behaves_like "parse", "87,57,97,67", "-"
it_behaves_like "parse", "49,45,46,48", "-"
it_behaves_like "parse", "63,63,52,72", "-"
it_behaves_like "parse", "84,86,84,95", "-"
it_behaves_like "parse", "61,60,62,73", "-"
it_behaves_like "parse", "59,79,69,48", "-"
it_behaves_like "parse", "55,57,77,75", "-"
end
@tyabe
Copy link
Author

tyabe commented Oct 14, 2012

2012-10-13の Yokohama.rb でやっていたヤツ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment