I make a card game in ruby.
I have a Game class that 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
Each player also has access to the game, so that they can access important bits of the game, thus
self.game.last_card_dealt
Each player also has cards (Player.cards), and I want to make sure that players cannot access other cards. However, the player needs access to the cards, so I don’t think private is suitable, and the players need access to some other information, so I don’t think I want it to be private either ...
Basically, I want them 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 fails:
self.hand.players[0].cards #=> Nice try sucker! Cheating is for losers.
How are more complex permissions, for example, handled? Thanks.
source share