加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby私有实例变量,有异常

发布时间:2020-12-17 02:50:48 所属栏目:百科 来源:网络整理
导读:我正在用ruby制作纸牌游戏. 我有Game类,它有一个Player对象数组. array_of_players = Array[ Player.new("Ben"),Player.new("Adam"),Player.new("Peter"),Player.new("Fred"),] my_game = Game.new(array_of_players) puts my_game.players[2].name #= Peter
我正在用ruby制作纸牌游戏.

我有Game类,它有一个Player对象数组.

array_of_players = Array[
  Player.new("Ben"),Player.new("Adam"),Player.new("Peter"),Player.new("Fred"),]
 my_game = Game.new(array_of_players)

 puts my_game.players[2].name #=> Peter

每个玩家也可以访问游戏,这样他们就可以像这样访问游戏的重要部分

self.game.last_card_dealt

每个玩家也有牌(Player.cards),我想确保玩家不能访问彼此的牌.但是,游戏确实需要访问卡片,所以我认为使用私有是不合适的,并且玩家需要访问彼此的一些信息,所以我认为我不希望它是私人的……

基本上,我希望这些工作.

self.cards #where self is a Player object
self.players[0].cards #where self is the Game
self.game.players[0].name #where self is a Player object

而这失败了:

self.hand.players[0].cards  #=> Nice try sucker! Cheating is for losers.

如何处理更复杂的权限?
谢谢.

解决方法

这比我的其他答案更实用,并使用Game对象作为游戏本身(玩家,牌等)中所有信息的委托.请注意,您仍然需要信任调用者自己传递,但是请认真地在哪里画线?

class Player
  attr_reader :name

  def initialize(name)
    @name = name
  end
end

class Cards
  attr_accessor :cards
end

class Game
  attr_reader :name,:players

  def initialize(players)
    @name = "Game Master"
    @hands = []
    @players = players.each do |p|
      puts "Added %s to game." % p.name
      @hands << {:player => p,:cards => Cards.new}
    end
  end

  def view_hand(player,caller)
    @hands.each do |hand|
      if hand[:player] == player
        if hand[:player] == caller or caller == self
          puts "%s: You can access all these cards: %s" % [caller.name,hand[:cards]]
        else
          # Do something to only display limited cards depending on this caller's view capabilities
          puts "%s: You can only access the cards I will let you see: %s" % [caller.name,hand[:cards]]
        end
      end
    end
  end

  def my_cards(player)
    @hands.each do |hand|
      puts "%s's cards: %s" % [player.name,hand[:cards]] if hand[:player] == player
    end
  end

end

g = Game.new([Player.new('Bob'),Player.new('Ben')])

puts "nCalling each Player's cards as each Player:nn"
g.players.each do |gp|
  g.players.each do |p|
    g.view_hand(gp,p)
  end
end

puts "nCalling each Player's cards as Game:nn"
g.players.each do |p|
  g.view_hand(p,g)
end

puts "nEach Player calls for their own cards:nn"
g.players.each do |p|
  g.my_cards(p)
end

输出:

Added Bob to game.
    Added Ben to game.

    Calling each Player's cards as each Player:

    Bob: You can access all these cards: #<Cards:0x100121c58>
    Ben: You can only access the cards I will let you see: #<Cards:0x100121c58>
    Bob: You can only access the cards I will let you see: #<Cards:0x100121bb8>
    Ben: You can access all these cards: #<Cards:0x100121bb8>

    Calling each Player's cards as Game:

    Game Master: You can access all these cards: #<Cards:0x100121c58>
    Game Master: You can access all these cards: #<Cards:0x100121bb8>

    Each Player calls for their own cards:

    Bob's cards: #<Cards:0x100121c58>
    Ben's cards: #<Cards:0x100121bb8>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读