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

#<=>(o) ⇒ Object



397
398
399
400
401
402
403
# File 'lib/manasimu/card.rb', line 397

def <=>(o)
  a = @contents[0]
  b = o.contents[0]
  d = a.set_code <=> b.set_code
  return d if d != 0
  a.number <=> b.number
end

#color_identityObject



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/manasimu/card.rb', line 233

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

#color_identity_sizeObject



246
247
248
# File 'lib/manasimu/card.rb', line 246

def color_identity_size
  color_identity.length
end

#converted_mana_costObject



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

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

#count(turn = nil) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
# File 'lib/manasimu/card.rb', line 385

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



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/manasimu/card.rb', line 354

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)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/manasimu/card.rb', line 282

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.include? "Land"
  end.length > 0
  return @is_land
end

#mana_costObject



207
208
209
210
211
212
213
214
215
# File 'lib/manasimu/card.rb', line 207

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

#mana_sourceObject



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

def mana_source
  @mana_source ||= @contents.map { |c| 
      c.color_identity ? c.color_identity : []
    }.flatten.uniq
end

#mana_source?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/manasimu/card.rb', line 203

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

#mana_source_sizeObject



199
200
201
# File 'lib/manasimu/card.rb', line 199

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

#max_flow(lands, capas) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/manasimu/card.rb', line 317

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

#name_ja_splitObject



302
303
304
305
306
307
308
# File 'lib/manasimu/card.rb', line 302

def name_ja_split
  return @name_ja_split if @name_ja_split
  c = @contents[0]
  return "" if not c.names
  return "" if not c.names[0]
  @name_ja_split = c.names[0].split(' // ')[0]
end

#namesObject



297
298
299
# File 'lib/manasimu/card.rb', line 297

def names
  @contents[0].names
end

#numberObject



161
162
163
# File 'lib/manasimu/card.rb', line 161

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

#playable?(lands, capas) ⇒ Boolean

Returns:

  • (Boolean)


310
311
312
313
314
315
# File 'lib/manasimu/card.rb', line 310

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



274
275
276
# File 'lib/manasimu/card.rb', line 274

def price
  converted_mana_cost
end

#set_codeObject



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

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

#step_in_hands(turn, card) ⇒ Object



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

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



172
173
174
175
176
177
178
179
180
181
# File 'lib/manasimu/card.rb', line 172

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



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/manasimu/card.rb', line 250

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



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

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

#to_factoryObject



409
410
411
# File 'lib/manasimu/card.rb', line 409

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

#to_sObject



405
406
407
# File 'lib/manasimu/card.rb', line 405

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

#typeObject



225
226
227
# File 'lib/manasimu/card.rb', line 225

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

#typesObject



229
230
231
# File 'lib/manasimu/card.rb', line 229

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