Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

銀座Rails版「点字メーカープログラム」の提出 #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 34 additions & 38 deletions lib/tenji_maker.rb
@@ -1,43 +1,39 @@
# frozen_string_literal: true

class TenjiMaker
REGULAR_ARRANGEMENTS_OF_DOTS = {
A: '1', I: '12', U: '14', E: '124', O: '24',
K: '6', S: '56', T: '35', N: '3', H: '36', M: '356', R: '5'
}.transform_keys(&:to_s).freeze

IRREGULAR_ARRANGEMENTS_OF_DOTS = {
YA: '34', YU: '346', YO: '345', WA: '3', N: '356'
}.transform_keys(&:to_s).freeze

def to_tenji(text)
# 以下はサンプルの仮実装なので、このcase文は全部消して自作ロジックに書き直すこと
case text
when 'A HI RU'
<<~TENJI.chomp
o- o- oo
-- o- -o
-- oo --
TENJI
when 'KI RI N'
<<~TENJI.chomp
o- o- --
o- oo -o
-o -- oo
TENJI
when 'SI MA U MA'
<<~TENJI.chomp
o- o- oo o-
oo -o -- -o
-o oo -- oo
TENJI
when 'NI WA TO RI'
<<~TENJI.chomp
o- -- -o o-
o- -- oo oo
o- o- o- --
TENJI
when 'HI YO KO'
<<~TENJI.chomp
o- -o -o
o- -o o-
oo o- -o
TENJI
when 'KI TU NE'
<<~TENJI.chomp
o- oo oo
o- -o o-
-o o- o-
TENJI
romanized_letters = text.split("\s")
tenjis_rows =
romanized_letters.map do |letter|
arrangement = to_arrangement(letter)
dots = create_dots(arrangement)
dots.each_slice(3).to_a.transpose.map(&:join)
end
tenji_lines = tenjis_rows.transpose.map { _1.join("\s") }
tenji_lines.join("\n")
end

def to_arrangement(letter)
if (arrangement = IRREGULAR_ARRANGEMENTS_OF_DOTS[letter])
arrangement
else
arrangements = letter.each_char.map { REGULAR_ARRANGEMENTS_OF_DOTS[_1] }
arrangements.join.squeeze
end
end

def create_dots(arrangement)
(1..6).map do |number|
arrangement.include?(number.to_s) ? 'o' : '-'
end
end
end