0

我想将 Go Fish Card Game 中的玩家列表传递给向玩家发牌的方法。我想将已经制作的玩家数组作为参数/参数传递给 deal 方法。

我知道我必须使用 splat 运算符来传递可变数量的参数,但是如何传递一个预先制作可变元素数组?

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
4

2 回答 2

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
于 2013-12-23T22:57:58.883 回答
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
于 2013-12-23T22:57:43.953 回答