Class: TenpaiWakaruMan::FuCounter

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

Constant Summary collapse

HUTEI =
20
MENZEN_KAFU =
10
MELDS =
{
  pair: 0,
  run: 0,
  open_triplet: 2,
  closed_triplet: 4,
  open_quad: 8,
  closed_quad: 16
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hand) ⇒ FuCounter

Returns a new instance of FuCounter.



20
21
22
# File 'lib/tenpai_wakaru_man/fu_counter.rb', line 20

def initialize(hand)
  @hand = hand
end

Class Method Details

.count(hand, options) ⇒ Object



15
16
17
# File 'lib/tenpai_wakaru_man/fu_counter.rb', line 15

def count(hand, options)
  new(hand).count(options)
end

Instance Method Details

#count(options = {}) ⇒ Object

tsumo_or_ron: [:tsumo, :ron] prevailing_wind: [“Ew”, “Sw”, “Ww”, “Nw”] seat_wind: [“Ew”, “Sw”, “Ww”, “Nw”] waits: [:ryanmen, :kanchan, :penchan, :tanki, :shanpon]



28
29
30
31
32
33
34
35
36
# File 'lib/tenpai_wakaru_man/fu_counter.rb', line 28

def count(options = {})
  fu = HUTEI +
      count_by_win_kind(options[:tsumo_or_ron]) +
      count_by_melds +
      count_by_head(prevailing_wind: options[:prevailing_wind], seat_wind: options[:seat_wind]) +
      count_by_waits(options[:waits])

  (fu / 10.0).ceil * 10
end

#count_by_head(prevailing_wind:, seat_wind:) ⇒ Object



53
54
55
56
57
58
# File 'lib/tenpai_wakaru_man/fu_counter.rb', line 53

def count_by_head(prevailing_wind:, seat_wind:)
  fu = 0
  fu += 2 if @hand.head == prevailing_wind
  fu += 2 if @hand.head == seat_wind
  fu
end

#count_by_meldsObject



45
46
47
48
49
50
51
# File 'lib/tenpai_wakaru_man/fu_counter.rb', line 45

def count_by_melds
  @hand.melds.map {|meld|
    fu = MELDS[meld.type]
    fu *= 2 if meld.include_terminal_or_honor?
    fu
  }.inject(&:+)
end

#count_by_waits(waits) ⇒ Object



60
61
62
# File 'lib/tenpai_wakaru_man/fu_counter.rb', line 60

def count_by_waits(waits)
  %i(kanchan penchan tanki).include?(waits) ? 2 : 0
end

#count_by_win_kind(tsumo_or_ron) ⇒ Object



38
39
40
41
42
43
# File 'lib/tenpai_wakaru_man/fu_counter.rb', line 38

def count_by_win_kind(tsumo_or_ron)
  fu = 0
  fu += MENZEN_KAFU if tsumo_or_ron == :ron && @hand.closed?
  fu += 2 if tsumo_or_ron == :tsumo
  fu
end