I am making a card game in ruby.
I have the Game class, which has an array of Player objects.
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
Each player also has access to the Game, so that they can access the important bits of the game like so
self.game.last_card_dealt
Each player also has cards (Player.cards), and I want to make sure that players can't access each other's cards. However, the Game does need access to the cards, so I don't think using private
is appropriate, and the players need access to some of each other's information, so I don't think I want that to be private
either...
Basically, I want these to work.
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
And this to fail:
self.hand.players[0].cards #=> Nice try sucker! Cheating is for losers.
How are more complex permissions like this handled? Thanks.