Class: Genome

Inherits:
Object
  • Object
show all
Defined in:
lib/kittyverse/genome.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Genome

Returns a new instance of Genome.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kittyverse/genome.rb', line 5

def initialize( arg )
  if arg.is_a? Integer   ## use Integer (Fixnum+Bignum??) - why? why not?
    num = arg
    kai = Kai.encode( num )
  else
    if arg.downcase.start_with?( '0x' )  ## assume hexstring( base16 )
      kai = Kai.encode( arg.to_i(16) )
    else # else assume string in kai/base32 format
      kai = arg
      kai = kai.gsub( ' ', '' )   ## allow spaces (strip/remove)
    end
  end
  ## puts "Genome.initialize #{kai}"

  @kai   = kai    ## note: store/save kai without any spaces ("compact" format)
  @genes = build_genes( kai )  ## array of (sliced) genes (block of four genes)
end

Instance Method Details

#[](key) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/kittyverse/genome.rb', line 164

def [](key)
  if key.is_a? Integer   ## assume 0,1,2,3,.. index
    q , r = key.divmod(4)   ## q=quotient, r=rest/modulus
    ##  e.g.   3.divmod(4) => [0,3]
    ##         4.divmod(4) => [1,0]
    ##         5.divmod(4) => [1,1]  etc.
    @genes[q][r]
  else   ## assume trait type key / symbol
    @genes[index(key)]
  end
end

#binaryObject Also known as: bin



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kittyverse/genome.rb', line 33

def binary
  @kai.chars.each_slice(4).map do |slice|
    buf = ""
    buf << Kai::BINARY[slice[0]]
    buf << "-"
    buf << Kai::BINARY[slice[1]]
    buf << "-"
    buf << Kai::BINARY[slice[2]]
    buf << "-"
    buf << Kai::BINARY[slice[3]]
    buf
  end.join( " " )
end

#bodyObject Also known as: fu

Fur (FU)



74
# File 'lib/kittyverse/genome.rb', line 74

def body()           @genes[0]; end

#build_genes(kai) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kittyverse/genome.rb', line 49

def build_genes( kai )
  kai = kai.reverse    ## note: reserve for easy left-to-right access
  genes = []   ## array of (sliced) genes (block of four genes)
  ## fix/todo: use as_json for "official" api order
  ## note: use insert order from "official" api

##    genes << Gene::Slice.new( :body, kai[0],
##                                     kai[1],
##                                     kai[2],
##                                     kai[3] )
##    genes << Gene::Slice.new( :pattern, kai[4+0],
##                                        kai[4+1],
##                                        kai[4+2],
##                                        kai[4+3]] )

  keys.each_with_index do |key,i|
     genes << Gene::Slice.new( key, kai[4*i+0],
                                    kai[4*i+1],
                                    kai[4*i+2],
                                    kai[4*i+3])
  end
  genes
end

#bytesObject



25
# File 'lib/kittyverse/genome.rb', line 25

def bytes()  Kai.bytes( @kai ); end

#coloreyesObject Also known as: ec

Eyes Color (EC)



76
# File 'lib/kittyverse/genome.rb', line 76

def coloreyes()      @genes[2]; end

#colorprimaryObject Also known as: color1, bc

Base Color (BC)



78
# File 'lib/kittyverse/genome.rb', line 78

def colorprimary()   @genes[4]; end

#colorsecondaryObject Also known as: color2, hc

Highlight Color (HC)



79
# File 'lib/kittyverse/genome.rb', line 79

def colorsecondary() @genes[5]; end

#colortertiaryObject Also known as: color3, ac

Accent Color (AC)



80
# File 'lib/kittyverse/genome.rb', line 80

def colortertiary()  @genes[6]; end

#eachObject Also known as: each_slice



107
# File 'lib/kittyverse/genome.rb', line 107

def each()            @genes.each { |slice| yield(slice) }; end

#each_geneObject



112
113
114
115
116
117
118
119
# File 'lib/kittyverse/genome.rb', line 112

def each_gene
  @genes.each do |slice|
    yield(slice.p)
    yield(slice.r1)
    yield(slice.r2)
    yield(slice.r3)
  end
end

#each_gene_with_indexObject



120
121
122
123
124
125
126
127
# File 'lib/kittyverse/genome.rb', line 120

def each_gene_with_index
  @genes.each_with_index do |slice,i|
    yield(slice.p,  4*i+0)
    yield(slice.r1, 4*i+1)
    yield(slice.r2, 4*i+2)
    yield(slice.r3, 4*i+3)
  end
end

#each_with_indexObject Also known as: each_slice_with_index



108
# File 'lib/kittyverse/genome.rb', line 108

def each_with_index() @genes.each_with_index { |slice,i| yield(slice,i) }; end

#electrologicaObject Also known as: codes

return formatted in electrologica base32 format



30
# File 'lib/kittyverse/genome.rb', line 30

def electrologica() Electrologica.fmt( Electrologica.encode( num ) ); end

#environmentObject Also known as: en

Environment (EN)



83
# File 'lib/kittyverse/genome.rb', line 83

def environment()    @genes[9]; end

#eyesObject Also known as: es

Eyes Shape (ES)



77
# File 'lib/kittyverse/genome.rb', line 77

def eyes()           @genes[3]; end

#index(key) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/kittyverse/genome.rb', line 144

def index( key )
  if key.size == 2 && key =~ /^[A-Za-z]{2}$/ ## check for codes e.g. FU, PA, ... (or fu, pa,...)
    key = key.upcase.to_sym
    @@codes_by_index ||= %w(FU PA EC ES BC HC AC WE MO EN SE PU)
                         .each_with_index.reduce({}) do |h,(code,i)|
                                                       h[code.to_sym]=i; h
                                                     end
    @@codes_by_index[ key ]
  else
    key = key.downcase.to_sym
    key = ALT_TRAIT_TYPE_KEYS[ key ]  if ALT_TRAIT_TYPE_KEYS[ key ]

    @@keys_by_index ||= keys.each_with_index.reduce({}) do |h,(key,i)|
                                                          h[key]=i; h
                                                        end
    @@keys_by_index[ key ]
  end
end

#kaiObject

return formatted in blocks of 4



24
# File 'lib/kittyverse/genome.rb', line 24

def kai()    Kai.fmt( @kai ); end

#keysObject

rename to trait_type_keys - why? why not?



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/kittyverse/genome.rb', line 129

def keys  ##  rename to trait_type_keys - why? why not?
  [:body,      ### todo/fix: use TRAITS.keys or something - why? why not?
   :pattern,
   :coloreyes,
   :eyes,
   :colorprimary,
   :colorsecondary,
   :colortertiary,
   :wild,
   :mouth,
   :environment,
   :secret,
   :prestige]
end

#mouthObject Also known as: mo

Mouth (MO)



82
# File 'lib/kittyverse/genome.rb', line 82

def mouth()          @genes[8]; end

#numObject Also known as: to_i



27
# File 'lib/kittyverse/genome.rb', line 27

def num()    Kai.decode( @kai ); end

#patternObject Also known as: pa

Pattern (PA)



75
# File 'lib/kittyverse/genome.rb', line 75

def pattern()        @genes[1]; end

#prestigeObject Also known as: pu

Purrstige (PU)



85
# File 'lib/kittyverse/genome.rb', line 85

def prestige()       @genes[11]; end

#secretObject Also known as: se

Secret (SE)



84
# File 'lib/kittyverse/genome.rb', line 84

def secret()         @genes[10]; end

#wildObject Also known as: we

Wild Element (WE)



81
# File 'lib/kittyverse/genome.rb', line 81

def wild()           @genes[7]; end