4

Rubyでカードゲームを作っています。

Player オブジェクトの配列を持つ Game クラスがあります。

 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) を持っており、プレイヤーがお互いのカードにアクセスできないようにしたいと考えています。ただし、ゲームはカードにアクセスする必要があるため、使用するのは適切ではないと思いますprivate。また、プレイヤーはお互いの情報にアクセスする必要があるため、それも望んでいないと思いprivateます...

基本的に、私はこれらが機能することを望んでいます。

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.

このようなより複雑なアクセス許可はどのように処理されますか? ありがとう。

4

4 に答える 4

3

これは私の他の回答よりも実用的であり、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:\n\n"
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:\n\n"
g.players.each do |p|
  g.view_hand(p, g)
end

puts "\nEach Player calls for their own cards:\n\n"
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>
于 2010-03-04T13:49:11.827 に答える
2

Game.playerプレイヤーが配列を介して他のプレイヤーにアクセスできないようにするには、非公開にしてください。

例: When selfis a player,self.game.players[0].nameはばかげています。

Game.player_namesおそらく、プレーヤー名の配列を返すだけのパブリックメソッドが必要ですか?

Players.opponentsその上で、パブリックメソッドを作成できます。

Game.player_names

class Game
  # ...
  def player_names
    self.players.collect { |p| p.name }
  end

  private
  # private game methods
end

Player.opponents

class Player
  # ...
  def opponents(i=nil)
    return i.nil? ? self.game.player_names : self.game.player_names[i]
  end
end
于 2010-03-04T09:21:40.517 に答える
2

これは遊んでいて楽しかったです。これが最善の答えかどうかはわかりませんが、うまくいきます。重要なのは、呼び出し元のオブジェクトを Player.cards(obj) に渡し、それが Player 自体であるか、タイプが Game であるかを確認することです。どちらも正当なアクセス権を持っています。

class Player
  attr_accessor :name, :game
  attr_writer :cards

  def initialize(name)
    @name = name
    @game = nil
    @cards = nil
  end

  def cards(caller)
    puts "%s cards called by %s." % [self, caller]
    if caller.kind_of?(Game) or caller == self
      puts "Here's your cards %s." % @cards
    else
      puts "Nice try sucker!  Cheating is for losers."
    end
  end
end

class Cards
  def initialize
    @cards = [1, 2, 3]
  end
end

class Game
  attr_reader :players

  def initialize(players)
    @players = players.each do |p|
      puts "Added %s to game." % p.name
      p.game = self
      p.cards = Cards.new
    end
  end
end

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

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

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

そして出力:

    Added Bob to game.
    Added Ben to game.

    Calling each Player's cards as each Player:

    #<Player:0x100122b30> cards called by #<Player:0x100122b30>.
    Here's your cards #<Cards:0x1001229c8>.
    #<Player:0x100122ae0> cards called by #<Player:0x100122b30>.
    Nice try sucker!  Cheating is for losers.
    #<Player:0x100122b30> cards called by #<Player:0x100122ae0>.
    Nice try sucker!  Cheating is for losers.
    #<Player:0x100122ae0> cards called by #<Player:0x100122ae0>.
    Here's your cards #<Cards:0x100122928>.

    Calling each Player's cards as Game:

    #<Player:0x100122b30> cards called by #<Game:0x100122ab8>.
    Here's your cards #<Cards:0x1001229c8>.
    #<Player:0x100122ae0> cards called by #<Game:0x100122ab8>.
    Here's your cards #<Cards:0x100122928>.
于 2010-03-04T09:41:33.273 に答える
1

ご回答ありがとうございます。

最終的に、承認されたオブジェクトに、メソッドの本体へのアクセスを許可するために使用されるキーを与えることができると考えました。

Game オブジェクトには @auth_object があり、シークレット メソッドにアクセスするプレイヤー オブジェクトに設定されます。プレイヤー シークレット メソッドは、 hand.auth_object が であるかどうかをチェックselfし、そうでない場合は何もしません。その後、@auth_object は nil に戻されます。attr_reader はありますが、@auth_object のライターはありません。

それはうまくいきます。

于 2010-03-17T15:45:42.767 に答える