Class: CardType

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ CardType

Returns a new instance of CardType.



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

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

Instance Attribute Details

#can_playsObject

Returns the value of attribute can_plays.



131
132
133
# File 'lib/manasimu/card.rb', line 131

def can_plays
  @can_plays
end

#contentsObject

Returns the value of attribute contents.



131
132
133
# File 'lib/manasimu/card.rb', line 131

def contents
  @contents
end

#drawed(turn) ⇒ Object

Returns the value of attribute drawed.



131
132
133
# File 'lib/manasimu/card.rb', line 131

def drawed
  @drawed
end

#mana_sourcesObject

Returns the value of attribute mana_sources.



131
132
133
# File 'lib/manasimu/card.rb', line 131

def mana_sources
  @mana_sources
end

#nameObject

Returns the value of attribute name.



131
132
133
# File 'lib/manasimu/card.rb', line 131

def name
  @name
end

#played(turn) ⇒ Object

Returns the value of attribute played.



131
132
133
# File 'lib/manasimu/card.rb', line 131

def played
  @played
end

Class Method Details

.create(card_type, name) ⇒ Object



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

def self.create(card_type, name)
  ret = card_type.dup
  ret.contents = card_type.contents
  ret.played = {}
  ret.drawed = {}
  ret.can_plays = {}
  ret.mana_sources = {}
  ret.name = name
  ret
end

Instance Method Details

#color_identityObject



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/manasimu/card.rb', line 225

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

#color_identity_sizeObject



238
239
240
# File 'lib/manasimu/card.rb', line 238

def color_identity_size
  color_identity.length
end

#converted_mana_costObject



209
210
211
# File 'lib/manasimu/card.rb', line 209

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

#count(turn = nil) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
# File 'lib/manasimu/card.rb', line 364

def count(turn = nil)
  turn ||= converted_mana_cost
  played_count = @played[turn] || 0
  drawed_count = 0
  (turn+1).times do |i|
    drawed_count += @drawed[i] || 0
  end
  can_played_count = @can_plays[turn] || 0
  mana_sources_count = @mana_sources[turn] || {}
  [played_count, drawed_count, can_played_count, mana_sources_count]
end

#edges(lands, capas) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/manasimu/card.rb', line 333

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

  # lands and mana_cost connect to each symbols
  lands.each_with_index do |land, i|
    next if capas[i].to_i == 0
    land_colors = land.mana_source
    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

#is_land?(side = nil) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/manasimu/card.rb', line 274

def is_land?(side = nil)
  return @is_land if @is_land
  arr = if side == 'a'
          [0]
        elsif side == 'b'
          [1]
        else
          [0,1]
        end
  @is_land = arr.select do |i|
    @contents[i] and @contents[i].types == "Land"
  end.length > 0
  return @is_land
end

#mana_costObject



199
200
201
202
203
204
205
206
207
# File 'lib/manasimu/card.rb', line 199

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_sourceObject



185
186
187
188
189
# File 'lib/manasimu/card.rb', line 185

def mana_source
  @mana_source ||= @contents.map { |c| 
      c.color_identity ? c.color_identity.split(',') : []
    }.flatten.uniq
end

#mana_source?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/manasimu/card.rb', line 195

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

#mana_source_sizeObject



191
192
193
# File 'lib/manasimu/card.rb', line 191

def mana_source_size
  (a = mana_source) ? a.size : 0
end

#max_flow(lands, capas) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/manasimu/card.rb', line 296

def max_flow(lands, capas)
  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, capas)
  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)

  land_symbols = Array.new(lands.length)
  for edges in g.G do
    for edge in edges do
      if edge.cap == 0 and edge.from.between?(1, x) and edge.to.between?(x+1, x+y)
        land_index = edge.from - 1
        spell_index = edge.to - x - 1
        land_symbols[land_index] = symbols[spell_index] 
      end
    end
  end

  [ret, obj.used, land_symbols]
end

#playable?(lands, capas) ⇒ Boolean

Returns:

  • (Boolean)


289
290
291
292
293
294
# File 'lib/manasimu/card.rb', line 289

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

#priceObject



266
267
268
# File 'lib/manasimu/card.rb', line 266

def price
  converted_mana_cost
end

#set_codeObject



270
271
272
# File 'lib/manasimu/card.rb', line 270

def set_code
  @contents[0].set_code
end

#step_in_hands(turn, card) ⇒ Object



157
158
159
160
161
162
# File 'lib/manasimu/card.rb', line 157

def step_in_hands(turn, card)
  if card.can_play?
    @can_plays[turn] ||= 0
    @can_plays[turn] += 1
  end
end

#step_in_plays(turn, card) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/manasimu/card.rb', line 164

def step_in_plays(turn, card)
  if card.mana_source? and not card.tapped?
    @mana_sources[turn] ||= {}
    size = card.mana_source.length
    card.mana_source.each do |c|
      @mana_sources[turn][c] ||= 0
      @mana_sources[turn][c] += 1.0 / size
    end
  end
end

#symbolsObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/manasimu/card.rb', line 242

def symbols
  return @symbols if @symbols
  @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
  @symbols
end

#textObject



213
214
215
# File 'lib/manasimu/card.rb', line 213

def text
  @text ||= @contents.map {|c| c.text}.flatten.join('')
end

#to_factoryObject



380
381
382
# File 'lib/manasimu/card.rb', line 380

def to_factory
  @contents.map {|c| c.to_factory}
end

#to_sObject



376
377
378
# File 'lib/manasimu/card.rb', line 376

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

#typeObject



217
218
219
# File 'lib/manasimu/card.rb', line 217

def type
  @type ||= @contents.map {|c| c.type}.flatten.uniq
end

#typesObject



221
222
223
# File 'lib/manasimu/card.rb', line 221

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