2013-12-23 43 views
0

我想通过一个Go Fish Card Card游戏中我的玩家列表来给玩家发牌。我想传入已经制作好的玩家数组作为交易方法的参数/参数。将预制数组作为Ruby中的参数/参数传递?

我知道我不得不使用图示操作中的参数一个变量量通过,但我怎么传递一个预制的阵列的不同元素

def deal_to_players是我正在修改的功能。我想将@player数组传递给deal_to_players number_of_players

谢谢!代码:

require_relative 'FishDeck.rb' 
require_relative 'FishHand.rb' 
require_relative 'FishPlayers.rb' 


class FishGame 

    attr_accessor :player 
    attr_accessor :top_card_container, :next_turn #next turn stores who's turn it is next 
                #How will I use that with the server? 

    def initialize(number_of_fish_players) 
     @player = [] 
     i = 0 ##Revise so i can be 0? 
     number_of_fish_players.times do #like 5.times do 
     #puts "iteration i is: #{i}"  
      @player[i] = FishPlayer.new #Revise to PLAYER CLASS 
      i += 1 
     end 
     #puts "PLAYER ARRAY: #{@player}" 
     #puts "players 0: #{@player[0].player_hand.player_cards}, players 1: #{@player[1]}" 
    end 

    def deal_to_players(deck_name, number_of_players) #!!!!!!!!!!!!!!!!!Need to pass the @player array to nuber_of_players so I can perform .each on it 5 times 

     5.times do 
      top_card = deck_name.pop_top_card 
      @player1.player_cards << top_card 

      top_card = deck_name.pop_top_card 
      @player2.player_cards << top_card 

      top_card = deck_name.pop_top_card 
      @player3.player_cards << top_card 

      top_card = deck_name.pop_top_card 
      @player4.player_cards << top_card 

      top_card = deck_name.pop_top_card 
      @player5.player_cards << top_card 
     end 
    end 

    def deck_to_player(game_deck, player_to) 

     player_to.player_cards << top_card_container = game_deck.pop_top_card 
     #Pops top deck card and shovels onto player_to 's cards 
     player_to.looks_for_books 
    end 


    def player_to_player(game_deck, wanted_card, player_asked, player_asking) #player_asking wants "wanted_card" from player_asked 

     card_holder = player_asked.return_cards_requested(wanted_card) #player in game's return card method and stores 

     #puts "card holder[0] is: #{card_holder[0]}" 
     #puts "wanted card is #{wanted_card}" 


     if card_holder[0] == wanted_card #element 0 will be the wanted_card or hold nothing 

      player_asking.player_cards.concat(card_holder) 
      card_holder.clear 
      player_asking.looks_for_books 
      @next_turn = "player_asking" 
      #puts "next turn if player_asked has player_asking \'s wanted card" 
     else 

      card_from_deck = deck_to_player(game_deck, player_asking) 

      if card_from_deck == wanted_card 
       @next_turn = "player_asking" 
    #   puts "next turn if card from deck == card wanted: #{@next_turn}" 
      else 
       @next_turn = "NEXT PLAYER" 
    #   puts "next turn if card from deck did NOT == card wanted: #{@next_turn}" 
      end 
     end 
    end 
end 
+0

为什么你就不能说'@player.each {...}'在'deal_to_players'里面? –

+0

像史恩说的那样?因为它是类中的一个实例变量,我不需要通过它? – Yallo

+0

右,实例变量被附加到'self',以便它们始终可用。您可能希望在进入时将其重命名为“@ players”,它包含若干项内容,因此使用复数名称是有道理的。 –

回答

0

我只想补充其访问您@players实例变量的方法

def deal_to_players(deck_name) 
    5.times do 
     number_of_players.times do |i| 
      top_card = deck_name.pop_top_card 
      @players[i].player_card << top_card 
     end 
    end 
end 


def number_of_players 
    @players.count 
end 
+0

* *会很好,很花哨,而且实际上是一个非常简单的解决方案,但我试图一次处理1张牌给每个*玩家,就像在真正的纸牌游戏中一样。第一张牌给玩家1,第二张牌给玩家2等等。 我不知道如何用这种结构合理地实现它。 – Yallo

+0

好的,这应该可以满足你的需求,假设每个玩家都需要5张牌。 – wvm2008

1
def deal_to_players(deck_name) 
    5.times do 
    #Since @player is a instance variable (thanks to the @) you don't have to pass it: 
    #It's in plain sight (inside the instance). note @player is a clumsy name for a collection 
    @player.each do |pl| #I'd prefer 'player' over 'pl', but now it's confusing 
     pl.player_cards << deck_name.pop_top_card 
    end 
end