Class: CardType

Inherits:
Object
  • Object
show all
Defined in:
lib/manasimu/card.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ CardType

Returns a new instance of CardType.



80
81
82
83
# File 'lib/manasimu/card.rb', line 80

def initialize(contents)
  return if not contents
  @contents = contents.map {|c| Content.new(c)}
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



78
79
80
# File 'lib/manasimu/card.rb', line 78

def contents
  @contents
end

Instance Method Details

#color_identityObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/manasimu/card.rb', line 113

def color_identity
  return @memo_colors if @memo_colors
  @memo_colors ||= []
  @contents.each do |c|
    c.color_identity.split(",").each do |color|
      @memo_colors << color if not @memo_colors.include? color
    end
  end
  @memo_colors
end

#color_identity_sizeObject



128
129
130
# File 'lib/manasimu/card.rb', line 128

def color_identity_size
  color_identity.length
end

#converted_mana_costObject



124
125
126
# File 'lib/manasimu/card.rb', line 124

def converted_mana_cost
  @converted_mana_cost ||= @contents.map {|c| c.converted_mana_cost}.min
end

#count(turn = nil) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/manasimu/card.rb', line 229

def count(turn = nil)
  turn ||= converted_mana_cost
  played = @played ? @played [turn] : 0
  drawed = 0
  if (@drawed)
    (turn+1).times do |i|
      next if not @drawed[i]
      drawed += @drawed[i]
    end
  end
  [played, drawed]
end

#drawed(turn) ⇒ Object



95
96
97
98
99
# File 'lib/manasimu/card.rb', line 95

def drawed(turn)
  @drawed ||= {}
  @drawed[turn] ||= 0
  @drawed[turn] += 1
end

#edges(lands) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/manasimu/card.rb', line 178

def edges(lands)
  result = []
  x = lands.length
  i_src = 0
  # source connect to lands
  x.times do |i|
    result << [i_src, i + 1]
  end

  # create symbol
  symbols = []
  mana_cost[1..-2].split('}{').each_with_index do |mana, j|
    spell_colors = mana.split('/')
    if spell_colors.length == 1 
      spell_color = spell_colors[0]
      if spell_color.to_i.to_s == spell_color
        # numeric symbol
        spell_color.to_i.times do |k|
          symbols << "1"
        end
      else
        # color symbol
        symbols << spell_color
      end
    else
      # multi symbol
      throw Exception.new('unprogramed exception')
    end
  end

  # lands and mana_cost connect to each symbols
  lands.each_with_index do |land, i|
    land_colors = land.color_identity
    symbols.each_with_index do |symbol, j|
      if symbol == "1" or land_colors.include? symbol
        result << [i + 1, x + 1 + j]
      end
    end
  end

  y = symbols.length 
  i_dst = x + y + 1

  # mana_cost connect to destination
  y.times do |i|
    result << [x + 1 + i, i_dst]
  end

  [x, y, result]
end

#manaObject



109
110
111
# File 'lib/manasimu/card.rb', line 109

def mana
  @mana ||= @contents.map {|content| content.color_identity }.flatten
end

#mana_costObject



132
133
134
135
136
137
138
139
140
# File 'lib/manasimu/card.rb', line 132

def mana_cost
  return @mana_cost if @mana_cost
  spell = @contents.select {|c| c.types != "Land"}.first
  if spell
    @mana_cost = spell.mana_cost
  else
    @mana_cost = '0'
  end
end

#mana_source?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/manasimu/card.rb', line 101

def mana_source?
  @mana_source ||= @contents.any? {|content| content.mana_source?}
end

#max_flow(lands) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/manasimu/card.rb', line 153

def max_flow(lands)
  obj = FordFulkersonSingleton.instance.obj
  # Graph has x+y+2 nodes
  # source       : 0
  # lands        : 1 - x 
  # mana_cost    : x+1 - x+y+1 
  # destination  : x+y+2
  #
  # image
  #         - land1  - mana4 
  # source0 - land2  - mana5 - destination6
  #         - land3 
  #

  # create edge
  x, y, e = edges(lands)
  g = Graph.new(x + y + 2)
  e.each do |s, d|
    g.add_edge(s, d, 1)
  end

  ret = obj.max_flow(g, 0, x + y + 1)
  [ret, obj.used]
end

#nameObject



85
86
87
# File 'lib/manasimu/card.rb', line 85

def name
  @name ||= @contents[0].name
end

#playable?(lands) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
# File 'lib/manasimu/card.rb', line 146

def playable?(lands)
  return [false, []] if lands.empty?
  return [false, []] if converted_mana_cost > lands.length
  mf, used = max_flow(lands)
  [mf == converted_mana_cost, used.to_a[1..lands.length]]
end

#played(turn) ⇒ Object



89
90
91
92
93
# File 'lib/manasimu/card.rb', line 89

def played(turn)
  @played ||= {}
  @played[turn] ||= 0
  @played[turn] += 1
end

#priceObject



142
143
144
# File 'lib/manasimu/card.rb', line 142

def price
  converted_mana_cost
end

#to_sObject



242
243
244
# File 'lib/manasimu/card.rb', line 242

def to_s
  @contents.map {|c| c.to_s}.join(",")
end

#typesObject



105
106
107
# File 'lib/manasimu/card.rb', line 105

def types
  @types ||= @contents.map {|c| c.types}
end