如何在 Ruby 类之间传递变量?

发布于 2025-01-08 11:15:07 字数 1125 浏览 0 评论 0原文

我正在创建一个具有多个类别的纸牌游戏。目前,我使用全局变量来保存 $shuffled_deck$players_hand$dealers_hand 变量,但在使用全局变量时我担心(也许,不必要)并且更喜欢使用实例变量。

我一直在阅读周围的内容,但没有什么是真正令人感兴趣的。谁能帮我指出正确的方向?

使用实例变量,我无法保存 @players_hand@dealers_hand 以便能够在其他类中使用它们。例如,我有来自 Player 类的 @players_hand 。我让 Dealer 类抽一张牌,但我无法将 @players_hand 拉入 Dealer 类中以将两者添加在一起。

我当前的代码是:

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

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2025-01-15 11:15:07

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

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

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
谁的新欢旧爱 2025-01-15 11:15:07

您想要使用 attr_readerattr_writerattr_accessor。它们的工作原理如下:

  • 的值
  • attr_reader :players_hand:允许您编写 some_player.players_hand 来获取该玩家的 players_hand 实例变量attr_writer :players_hand:允许您编写 some_player.players_hand = 0 将变量设置为 0
  • attr_accessor :players_hand:允许您读取和写入,就像您同时使用了 attr_readerattr_writer 一样。

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

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

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

  • 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_writer :players_hand: Allows you to write some_player.players_hand = 0 to set the variable to 0
  • attr_accessor :players_hand: Allows you to both read and write, as though you'd used both attr_reader and 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文