ruby - How to Take Turn in Game from Player1 to Player2 and keep code DRY -


i creating monopoly game. searched way create take_turn method (or class) can switch automatically between player1 , player2 (and in future player3 etc.).

looked answers here , found this full-on pdf on oop developing games haven't found answer specific question.

here's code i' tdded , built several other objects. works @ moment player1, automatically repeat steps player1 player2 automatically without having manually (dry).

class engine    attr_reader :dice, :player1, :player2, :move, :board    def initialize     @player1 = player.new     @board = board.new     @player2 = player.new     @dice = dice.new     @move = move.new   end    def run     3.times         roll_and_move         print_current_balance         player_action     end   end    def roll_and_move     dice.roll     move.move(player1, board, dice.value)   end    def print_current_balance     player1.balance   end    def player_action     player1.buy(board.tile(player1.position))   end end 

well, i'm not sure real problem is, maybe :

put players in array :

@players = [player.new,player.new] 

get indice current player

@current_player_indice = 1 def current_player   return @players[@current_player_indice] end 

advance turn simple :

def next_player   @current_player_indice = (@current_player_indice+1)%@players.size end 

the replace calls player1 current_player, , should pretty good.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -