Class: TenpaiWakaruMan::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/tenpai_wakaru_man/parser.rb

Constant Summary collapse

SUITED_SYMBOLS =
"msp"
DRAGON_SYMBOL =
"d"
WIND_SYMBOL =
"w"
OPEN_MELDED_SYMBOLS =
"lar"
HONOR_SYMBOLS =
DRAGON_SYMBOL + WIND_SYMBOL
MELD_SYMBOLS =
SUITED_SYMBOLS + HONOR_SYMBOLS + OPEN_MELDED_SYMBOLS
MELD_REGEXP =
/((\d+[#{SUITED_SYMBOLS}]|[PFC]+#{DRAGON_SYMBOL}|[ESWN]+#{WIND_SYMBOL})[#{OPEN_MELDED_SYMBOLS}]?)/

Class Method Summary collapse

Class Method Details

.parse(tile_str) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/tenpai_wakaru_man/parser.rb', line 27

def parse(tile_str)
  args = scan(tile_str).map {|tiles| Meld.new(tiles) }.chunk {|meld| meld.revealed? }.each_with_object({melds: [], tiles: []}) {|(bool, value), hash|
    bool ? hash[:melds] += value : hash[:tiles] += value.map(&:tiles).flatten
  }

  Hand.build(args)
end

.scan(tile_str) ⇒ Object



16
17
18
# File 'lib/tenpai_wakaru_man/parser.rb', line 16

def scan(tile_str)
  tile_str.scan(MELD_REGEXP).map {|tile_with_suite, _| tile_with_suite }
end

.split(tile_str) ⇒ Object



20
21
22
23
24
25
# File 'lib/tenpai_wakaru_man/parser.rb', line 20

def split(tile_str)
  scan(tile_str).map {|tile|
    meld_type = tile[/[#{MELD_SYMBOLS}]/]
    tile.delete(MELD_SYMBOLS).chars.map {|str| str + meld_type }
  }.flatten.sort_by {|tile| TILES[tile] }
end