On Tue, Jul 10, 2012 at 3:18 PM, Sebastjan H. <lists / ruby-forum.com> wrote:
> Jan E. wrote in post #1068119:
>> Sebastjan H. wrote in post #1068117:
>>> On the other hand, I am still considering this to be a small scale game
>>> with predefined let's say only 15 to 20 cards. However, If I were to
>>> expand it I need this adaptation.
>>
>> Well, even for 1000 objects you probably won't notice an actual
>> difference in speed. But personally I find it more "clean" to only
>> create objects you actually need.
>
> One more thing, are we talking about the same "selection"? I was
> referring to selection of one of the beasts from the deck for each turn
> of the battle.
>
> And I think you're referring to the deck selection.
>
> Obviously the game needs both, they're just different steps.
> And for the deck selection I only wanted the player input on the card
> types (beasts, spells,...). One may want to play only with let's say
> beasts while others prefer spells. And that's the deck selection. Once
> the deck is determined, players select cards for each turn.

What Jan is saying is that you should defer the creation of the actual
Ruby object until the last time, to avoid creating objects that are
not going to be used.
In my opinion, the last responsible time is the one where you need
some value from the object. So if you need to actually create the
objects or not for the deck selection depends on if the "key" (the
name) is enough for the user to choose or not. For example:

cards = %w{derimor barador fireball invisibility} # all the "keys" of
the possible cards
deck_card_names = []
while deck_card_names.size < 15 # or 20
  puts "Choose cards for deck"
  cards.each_with_index {|c,i| "#{i+1}.- #{c}"}
  card = cards[gets.to_i - 1]
  print "quantity: "
  qty = gets.to_i
  qty.times {deck_card_names << card}
end

while true
  puts "Choose card to play"
  deck_cards_name.each_with_index {|c,i| "#{i+1}.- #{c}"}
  card = Blue_Scale_DRAGON.new deck_cards_name[gets.to_i - 1]
end

Of course if you need the info of the card at choosing time, you might
need to instantiate the cards when choosing, for example if you need
to know the type or whatever. Depending on how you plan to generate
and use the cards, there might be better choices than an array. If you
might have a number of identical cards, maybe a hash of "name" => qty
or even "name" => {"qty": 4, "type":spell} or more complex stuff might
be more appropriate.

Jesus.