Class: CardType
- Inherits:
-
Object
- Object
- CardType
- Defined in:
- lib/manasimu/card.rb
Instance Attribute Summary collapse
-
#can_plays ⇒ Object
Returns the value of attribute can_plays.
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#drawed(turn) ⇒ Object
Returns the value of attribute drawed.
-
#mana_sources ⇒ Object
Returns the value of attribute mana_sources.
-
#name ⇒ Object
Returns the value of attribute name.
-
#played(turn) ⇒ Object
Returns the value of attribute played.
Class Method Summary collapse
Instance Method Summary collapse
- #color_identity ⇒ Object
- #color_identity_size ⇒ Object
- #converted_mana_cost ⇒ Object
- #count(turn = nil) ⇒ Object
- #edges(lands, capas) ⇒ Object
-
#initialize(contents) ⇒ CardType
constructor
A new instance of CardType.
- #is_land?(side = nil) ⇒ Boolean
- #mana_cost ⇒ Object
- #mana_source ⇒ Object
- #mana_source? ⇒ Boolean
- #mana_source_size ⇒ Object
- #max_flow(lands, capas) ⇒ Object
- #number ⇒ Object
- #playable?(lands, capas) ⇒ Boolean
- #price ⇒ Object
- #set_code ⇒ Object
- #step_in_hands(turn, card) ⇒ Object
- #step_in_plays(turn, card) ⇒ Object
- #symbols ⇒ Object
- #text ⇒ Object
- #to_factory ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
- #types ⇒ Object
Constructor Details
Instance Attribute Details
#can_plays ⇒ Object
Returns the value of attribute can_plays.
131 132 133 |
# File 'lib/manasimu/card.rb', line 131 def can_plays @can_plays end |
#contents ⇒ Object
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_sources ⇒ Object
Returns the value of attribute mana_sources.
131 132 133 |
# File 'lib/manasimu/card.rb', line 131 def mana_sources @mana_sources end |
#name ⇒ Object
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_identity ⇒ Object
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.split(",").each do |color| @memo_colors << color if not @memo_colors.include? color end end end @memo_colors end |
#color_identity_size ⇒ Object
246 247 248 |
# File 'lib/manasimu/card.rb', line 246 def color_identity_size color_identity.length end |
#converted_mana_cost ⇒ Object
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
372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/manasimu/card.rb', line 372 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
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/manasimu/card.rb', line 341 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
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 == "Land" end.length > 0 return @is_land end |
#mana_cost ⇒ Object
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| c.types != "Land"}.first if spell @mana_cost = spell.mana_cost else @mana_cost = '0' end end |
#mana_source ⇒ Object
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.split(',') : [] }.flatten.uniq end |
#mana_source? ⇒ 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_size ⇒ Object
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
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 332 333 334 335 336 337 338 339 |
# File 'lib/manasimu/card.rb', line 304 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 |
#number ⇒ Object
161 162 163 |
# File 'lib/manasimu/card.rb', line 161 def number @number ||= @contents[0].number end |
#playable?(lands, capas) ⇒ Boolean
297 298 299 300 301 302 |
# File 'lib/manasimu/card.rb', line 297 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 |
#price ⇒ Object
274 275 276 |
# File 'lib/manasimu/card.rb', line 274 def price converted_mana_cost end |
#set_code ⇒ Object
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 |
#symbols ⇒ Object
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 |
#text ⇒ Object
221 222 223 |
# File 'lib/manasimu/card.rb', line 221 def text @text ||= @contents.map {|c| c.text}.flatten.join('') end |
#to_factory ⇒ Object
388 389 390 |
# File 'lib/manasimu/card.rb', line 388 def to_factory @contents.map {|c| c.to_factory} end |
#to_s ⇒ Object
384 385 386 |
# File 'lib/manasimu/card.rb', line 384 def to_s @contents.map {|c| c.to_s}.join(",") end |
#type ⇒ Object
225 226 227 |
# File 'lib/manasimu/card.rb', line 225 def type @type ||= @contents.map {|c| c.type}.flatten.uniq end |
#types ⇒ Object
229 230 231 |
# File 'lib/manasimu/card.rb', line 229 def types @types ||= @contents.map {|c| c.types} end |