Class: Elus::Piece

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/elus/piece.rb

Constant Summary collapse

VALID =

These constants are used for translating between the external string representation of a Game and the internal representation.

"01sbgycdr=!."
SORT =

Valid (meaningful) chars in code string (downcase)

"01stghcdd=!."
FINAL =

Special unique chars good for sorting (B)ig->(T)itanic, (Y)ellow->(H)ellow, ®hombus->(D)iamond

"010101011=!."
INVALID =

Final (place-dependent) representation as 0/1 digits

Regexp.new "[^#{VALID}]"
PATTERN =

Correct code pattern for Piece creation

/^[01st=!.][01gh=!.][01cd=!.]$/
NAMES =

Names for Piece characteristics (based on code)

[ {'0'=>'Small', '1'=>'Big', '='=>'Same size', '!'=>'Different size'},
{'0'=>'Green', '1'=>'Yellow', '='=>'Same color', '!'=>'Different color'},
{'0'=>'Circle', '1'=>'Diamond', '='=>'Same shape', '!'=>'Different shape'}  ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_code) ⇒ Piece

Assumes valid code (should be pre-filtered by Piece.create)



44
45
46
# File 'lib/elus/piece.rb', line 44

def initialize(input_code)
  @code = input_code
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



14
15
16
# File 'lib/elus/piece.rb', line 14

def code
  @code
end

Class Method Details

.anyObject

Returns Any Piece (should be a singleton object)



39
40
41
# File 'lib/elus/piece.rb', line 39

def Piece.any
  @@any ||= Piece.create('...')  
end

.convert_code(input) ⇒ Object

Pre-processes string into valid code for Piece creation



24
25
26
27
28
29
30
31
# File 'lib/elus/piece.rb', line 24

def Piece.convert_code(input)
  # Remove all invalid chars from input and transcode it into sort chars
  input_code = input.downcase.gsub(INVALID, '').tr(VALID, SORT)
  # Remove dupes and sort unless code contains digits or special chars (place-dependent)
  input_code = input_code.scan(/\w/).uniq.sort.reverse.join unless input_code =~ /[01!=.]/ 
  # Translate sort chars into final chars
  input_code.tr!(SORT, FINAL) if input_code =~ PATTERN 
end

.create(input) ⇒ Object

Factory method: takes an input string and tries to convert it into valid Piece code. Returns new Piece if successful, otherwise nil



18
19
20
21
# File 'lib/elus/piece.rb', line 18

def Piece.create(input)
  return nil unless String === input
  if input_code = convert_code(input) then new(input_code) else nil end 
end

.different(string) ⇒ Object

Finds different (opposite, complimentary) code character



34
35
36
# File 'lib/elus/piece.rb', line 34

def Piece.different(string)
  string.tr('01!=.', '10=!.')
end

Instance Method Details

#*(other) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/elus/piece.rb', line 58

def * (other)
  new_code = case other
          when nil then nil
          when String then self * Piece.create(other)
          when Piece then
           (0..2).map do |i| 
            case other.code[i]
              when '=' then code[i]
              when '!' then Piece.different code[i]
            else other.code[i]
            end
          end.join
        else raise(ArgumentError, 'Piece compared with a wrong type')
        end
  Piece.create(new_code)
end

#<=>(other) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/elus/piece.rb', line 77

def <=> (other)
  case other
    when nil then 1
    when String then self <=> Piece.create(other)
    when Piece then
    return 0 if code == other.code
    return 0 if code =~ Regexp.new(other.code)
    return 0 if other.code =~ Regexp.new(code)
  else raise(ArgumentError, 'Piece compared with a wrong type')
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/elus/piece.rb', line 89

def eql? (other)
  (Piece === other) ? @code.eql?(other.code) : false
end

#nameObject

Returns full text name of this Piece



49
50
51
52
53
54
# File 'lib/elus/piece.rb', line 49

def name
 (0..2).map {|i| NAMES[i][@code[i]]}.compact.join(' ').
  gsub(Regexp.new('^$'), 'Any').
  gsub(Regexp.new('Same size Same color Same shape'), 'All Same').
  gsub(Regexp.new('Different size Different color Different shape'), 'All Different')
end

#to_sObject



56
# File 'lib/elus/piece.rb', line 56

def to_s; name end