Forum: Ruby Question on a card game I'm making

9b7b90158e6bb466928f602fa9d523d0?d=identicon&s=25 Eli White (snugitup)
on 2016-09-07 00:10
I have a series of cards as classes. My deck class will generate a .new
version of all the cards a certain number of times to populate the deck
with cards. My player class then accesses the deck with .sample and
pushes them into an array called player_hand of size 5. The problem I'm
stuck on is finding a good way to display all of the card
information(atk, def, type, name, hp) to the player by expanding each
cards "traits" that they are currently holding in @player_hand.

example:

class Player
   include Battle
   attr_accessor :player_hp, player_hand

  def initialize(player_hp=P_MAX_HP)
   @player_hp = player_hp
   @player_name = player_name
   @player_hand = player_hand[]

   get_player_name

  def get_player_name
    @player_name = gets.chomp
  end

  def alive?(player_hp)
    @playerhp > 0
  end

  def new_hand
    new_hand = Array.new(5)
    5.times do new_hand.push(@@deck.sample); end
    @player_hand = new_hand
  end

  def draw
    until @player_hand.size == 5
      do
        @player_hand.push(@@deck.sample)
    end
  end
  def inspect_card

Here's some example cards(for now we will use 3) id like to display:

class FireBunny < Card
  traits :atk, :defense, :type, :num, :hp
  atk 4
  defense 2
  type "fire"
  num 1
  hp 10
end

class WaterBug < Card
   traits :atk, :defense, :type, :num, :hp
   atk 2
   defense 4
   type "water"
   num 2
   hp 14
end

class LandWorm < Card
  traits :atk, :defense, :type, :num, :hp
  atk 3
  defense 3
  type "land"
  num 3
  hp 12

(pardon my silly names)

After they are in the array, i will have to write a display method
corresponding to the cards by making the player type 1 , 2 , 3 , 4 , 5
for each card in their hand. my only question is is how to display their
traits
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2016-09-07 21:16
I can't see from this code how you're holding each card's attributes,
but the simplest way would be to store them in a Hash and then display
the Hash.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.