mardi 9 juillet 2019

Initialization error expecting one amount seeing another

I'm trying to complete the card shuffle assignment and must use initialize and self in the code. I have defined what rank and suit mean but the program does not recognize that the variables are used, which they are. Then in the initialization, it says I have the wrong number of arguments

I tried changing the name of the items in the initialize code. I split @cards << (self.rank, self.suit) into 2 lines but made no difference. I moved the variable definitions inside the initialize code which helped with some messages but not the all.

This is the whole program

class Card
  attr_accessor :rank, :suit
  rank = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
  suit = ["Hearts", "Diamond", "Clubs", "Spades"]

  def initialize(rank, suit)

    self.rank = rank
    self.suit = suit

  end

  def output_card
    puts "#{self.rank} of #{self.suit}"
  end

  #def self.random_card
   # Card.new(rand(rank, suit))
  #end
end

class Deck
  attr_accessor :rank, :suit

  #Generates the 4 suits of cards in each denomination
  def initialize(cards)
    cards = Card.new("rank", "suit")
    @cards = []
    @cards << Card.new(self.rank, self.suit)

  end

  #shuffles the deck of cards
  def shuffle
    unless @cards.empty?
      @cards.shuffle!
    end
  end


  #def self.top_card
    #Card.new(self.rank, self.suit)
  #end

  #Deals the cards from the top of the virtual deck.
  def deal
    @cards.each do |card|
      card.output
    end
  end  
end

deck = Card.new
deck.shuffle
deck.deal

This is the current error message I get.

Shannons-MacBook-Air:Desktop leighrachal$ Ruby -w Card.rb
Card.rb:3: warning: assigned but unused variable - rank
Card.rb:4: warning: assigned but unused variable - suit
Card.rb:6:in `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)
    from Card.rb:53:in `new'
    from Card.rb:53:in `<main>'

Aucun commentaire:

Enregistrer un commentaire