How do I pass variables between Ruby classes?

时间:2023-02-02 19:14:56

I'm creating a card game with multiple classes. Currently, I'm using global variables to hold the $shuffled_deck, $players_hand, and $dealers_hand variables, but I worry when using global variables (perhaps, needlessly) and would prefer to use instance variables.

我正在创建一个包含多个课程的纸牌游戏。目前,我正在使用全局变量来保存$ shuffled_deck,$ players_hand和$ dealers_hand变量,但我担心使用全局变量(可能是不必要的)并且更喜欢使用实例变量。

I've been reading around, but nothing is really clicking. Can anyone help point me in the right direction with this?

我一直在看,但没有什么是真的点击。任何人都可以帮助我指出正确的方向吗?

Using instance variables I haven't been able to save the @players_hand and @dealers_hand to be able to use them in other classes. For instance, I have @players_hand from the Player class. I have the Dealer class draw a card, but I can't pull that @players_hand into the Dealer class to add the two together.

使用实例变量我无法保存@players_hand和@dealers_hand以便能够在其他类中使用它们。例如,我有来自Player类的@players_hand。我让Dealer类画了一张卡片,但是我不能把@players_hand拉进Dealer类来把它们加在一起。

My current code is:

我目前的代码是:

class Blackjack

  def initialize
    @player = Player.new
    @dealer = Dealer.new
  end
end

class Dealer

  def initialize
    @deck = Deck.new
    $dealers_hand = 0
  end

  def hit_dealer
    @deck.hit_dealer
  end

  def hit_player
    @deck.hit_player
  end

  def draw_card
    @hit = $shuffled_deck
  end

  def shuffle
    @deck.suits
  end
end

class Player

  def initialize
    $players_hand = 0
  end   
end

class Deck

 def suits
   #code that shuffled the deck..
   $shuffled_deck = @shuffled_deck
 end

 def hit_player
   @hit = $shuffled_deck.pop
 end

 def hit_dealer
   @hit = $shuffled_deck.pop
 end

end

2 个解决方案

#1


4  

You want to use attr_reader, attr_writer, or attr_accessor. Here's how they work:

您想使用attr_reader,attr_writer或attr_accessor。以下是它们的工作原理:

  • attr_reader :players_hand: Allows you to write some_player.players_hand to get the value of that player's players_hand instance variable
  • attr_reader:players_hand:允许你编写some_player.players_hand来获取该玩家的players_hand实例变量的值

  • attr_writer :players_hand: Allows you to write some_player.players_hand = 0 to set the variable to 0
  • attr_writer:players_hand:允许你编写some_player.players_hand = 0来将变量设置为0

  • attr_accessor :players_hand: Allows you to both read and write, as though you'd used both attr_reader and attr_writer.
  • attr_accessor:players_hand:允许您同时读写,就像您使用了attr_reader和attr_writer一样。

Incidentally, all these do is write methods for you. If you wanted, you could do it manually like this:

顺便说一句,所有这些都是为您编写的方法。如果你愿意,你可以像这样手动完成:

class Player
  def initialize
    @players_hand = 0
  end  

  def players_hand
    @players_hand
  end

  def players_hand=(new_value)
    @players_hand = new_value
  end
end

#2


5  

using your example you can do it like this

使用你的例子,你可以这样做

class Blackjack
  attr_reader :player, :dealer

  def initialize
    @player = Player.new
    @dealer = Dealer.new
  end
end

class Dealer
  def dealers_hand #the long java way of a getter
    @dealers_hand
  end

  #and now the short ruby way
  attr_reader :dealers_hand #if you only need to read the attribute
  attr_writer :dealers_hand #if you only need to write the attribute
  attr_accessor: dealers_hand #if you need both

  def initialize
    @deck = Deck.new
    @dealers_hand = 5
  end

  def hit_dealer
    @deck.hit_dealer
  end

  def hit_player
    @deck.hit_player
  end

  def draw_card
    @hit = $shuffled_deck
  end

  def shuffle
    @deck.suits
  end
end

class Player
  attr_reader :players_hand
  def initialize
    @players_hand = 0
  end   
end

class Deck

 def suits
   attr_reader :shuffled_deck
   @shuffled_deck = @shuffled_deck
 end

 def hit_player
   @hit = $shuffled_deck.pop
 end

 def hit_dealer
   @hit = $shuffled_deck.pop
 end

end

game = Blackjack.new
p game.dealer.dealers_hand
game.dealer.dealers_hand = 4
p game.dealer.dealers_hand

#1


4  

You want to use attr_reader, attr_writer, or attr_accessor. Here's how they work:

您想使用attr_reader,attr_writer或attr_accessor。以下是它们的工作原理:

  • attr_reader :players_hand: Allows you to write some_player.players_hand to get the value of that player's players_hand instance variable
  • attr_reader:players_hand:允许你编写some_player.players_hand来获取该玩家的players_hand实例变量的值

  • attr_writer :players_hand: Allows you to write some_player.players_hand = 0 to set the variable to 0
  • attr_writer:players_hand:允许你编写some_player.players_hand = 0来将变量设置为0

  • attr_accessor :players_hand: Allows you to both read and write, as though you'd used both attr_reader and attr_writer.
  • attr_accessor:players_hand:允许您同时读写,就像您使用了attr_reader和attr_writer一样。

Incidentally, all these do is write methods for you. If you wanted, you could do it manually like this:

顺便说一句,所有这些都是为您编写的方法。如果你愿意,你可以像这样手动完成:

class Player
  def initialize
    @players_hand = 0
  end  

  def players_hand
    @players_hand
  end

  def players_hand=(new_value)
    @players_hand = new_value
  end
end

#2


5  

using your example you can do it like this

使用你的例子,你可以这样做

class Blackjack
  attr_reader :player, :dealer

  def initialize
    @player = Player.new
    @dealer = Dealer.new
  end
end

class Dealer
  def dealers_hand #the long java way of a getter
    @dealers_hand
  end

  #and now the short ruby way
  attr_reader :dealers_hand #if you only need to read the attribute
  attr_writer :dealers_hand #if you only need to write the attribute
  attr_accessor: dealers_hand #if you need both

  def initialize
    @deck = Deck.new
    @dealers_hand = 5
  end

  def hit_dealer
    @deck.hit_dealer
  end

  def hit_player
    @deck.hit_player
  end

  def draw_card
    @hit = $shuffled_deck
  end

  def shuffle
    @deck.suits
  end
end

class Player
  attr_reader :players_hand
  def initialize
    @players_hand = 0
  end   
end

class Deck

 def suits
   attr_reader :shuffled_deck
   @shuffled_deck = @shuffled_deck
 end

 def hit_player
   @hit = $shuffled_deck.pop
 end

 def hit_dealer
   @hit = $shuffled_deck.pop
 end

end

game = Blackjack.new
p game.dealer.dealers_hand
game.dealer.dealers_hand = 4
p game.dealer.dealers_hand