Class: TenpaiWakaruMan::Meld

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles) ⇒ Meld

Returns a new instance of Meld.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tenpai_wakaru_man/meld.rb', line 8

def initialize(tiles)
  case tiles
  when String
    @msp_notation = tiles
    @tiles = Parser.split(@msp_notation)
  when Array
    @tiles = tiles.sort_by {|tile| TILES[tile] }
    @msp_notation = to_msp_notation
  end

  @unique_count = @tiles.uniq.count
  @count = @tiles.count
end

Instance Attribute Details

#msp_notationObject

Returns the value of attribute msp_notation.



6
7
8
# File 'lib/tenpai_wakaru_man/meld.rb', line 6

def msp_notation
  @msp_notation
end

#tilesObject

Returns the value of attribute tiles.



6
7
8
# File 'lib/tenpai_wakaru_man/meld.rb', line 6

def tiles
  @tiles
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
# File 'lib/tenpai_wakaru_man/meld.rb', line 26

def <=>(other)
  msp_notation <=> other.msp_notation
end

#==(other) ⇒ Object



22
23
24
# File 'lib/tenpai_wakaru_man/meld.rb', line 22

def ==(other)
  @msp_notation == other.msp_notation
end

#closed?Boolean

Returns:

  • (Boolean)


39
# File 'lib/tenpai_wakaru_man/meld.rb', line 39

def closed?; !open? end

#closed_quad?Boolean

Returns:

  • (Boolean)


58
# File 'lib/tenpai_wakaru_man/meld.rb', line 58

def closed_quad?;    closed? && quad? end

#closed_triplet?Boolean

Returns:

  • (Boolean)


56
# File 'lib/tenpai_wakaru_man/meld.rb', line 56

def closed_triplet?; closed? && triplet? end

#exclude_terminal_or_honor?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/tenpai_wakaru_man/meld.rb', line 83

def exclude_terminal_or_honor?
  !include_terminal_or_honor?
end

#include_honor?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/tenpai_wakaru_man/meld.rb', line 71

def include_honor?
  !!@msp_notation[/[#{Parser::HONOR_SYMBOLS}]/]
end

#include_terminal?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/tenpai_wakaru_man/meld.rb', line 75

def include_terminal?
  !!@msp_notation[/[19]/]
end

#include_terminal_or_honor?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/tenpai_wakaru_man/meld.rb', line 79

def include_terminal_or_honor?
  include_honor? || include_terminal?
end

#inspectObject



30
31
32
# File 'lib/tenpai_wakaru_man/meld.rb', line 30

def inspect
  "#<#{self.class.name}:\"#{@msp_notation}\">"
end

#open?Boolean

Returns:

  • (Boolean)


38
# File 'lib/tenpai_wakaru_man/meld.rb', line 38

def open?; !!@msp_notation[/[#{Parser::OPEN_MELDED_SYMBOLS}]/] end

#open_quad?Boolean

Returns:

  • (Boolean)


57
# File 'lib/tenpai_wakaru_man/meld.rb', line 57

def open_quad?;      open?   && quad? end

#open_triplet?Boolean

Returns:

  • (Boolean)


55
# File 'lib/tenpai_wakaru_man/meld.rb', line 55

def open_triplet?;   open?   && triplet? end

#pair?Boolean

Returns:

  • (Boolean)


42
# File 'lib/tenpai_wakaru_man/meld.rb', line 42

def pair?;    @unique_count == 1 && @count == 2 end

#quad?Boolean

Returns:

  • (Boolean)


44
# File 'lib/tenpai_wakaru_man/meld.rb', line 44

def quad?;    @unique_count == 1 && @count == 4 end

#revealed?Boolean

Returns:

  • (Boolean)


40
# File 'lib/tenpai_wakaru_man/meld.rb', line 40

def revealed?; @revealed ||= open? || closed_quad? end

#run?Boolean

Returns:

  • (Boolean)


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

def run?
  return false unless @tiles.map {|tile| tile[-1] }.uniq.count == 1

  chow_candidates = @tiles.map {|tile| TILES[tile] }.select {|tile| tile > 6 }

  return false unless chow_candidates.uniq.count == 3

  [chow_candidates[0] + 2,  chow_candidates[1] + 1, chow_candidates[2]].uniq.count == 1
end

#to_sObject



34
35
36
# File 'lib/tenpai_wakaru_man/meld.rb', line 34

def to_s
  @msp_notation
end

#triplet?Boolean

Returns:

  • (Boolean)


43
# File 'lib/tenpai_wakaru_man/meld.rb', line 43

def triplet?; @unique_count == 1 && @count == 3 end

#typeObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/tenpai_wakaru_man/meld.rb', line 60

def type
  case
  when pair?;           :pair
  when run?;            :run
  when open_triplet?;   :open_triplet
  when closed_triplet?; :closed_triplet
  when open_quad?;      :open_quad
  when closed_quad?;    :closed_quad
  end
end