【问题标题】:Pass a Pre-made Array as an Arguement/Parameter in Ruby?在Ruby中将预制数组作为参数/参数传递?
【发布时间】:2014-01-12 04:23:22
【问题描述】:

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

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

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

【问题讨论】:

  • 你为什么不能在deal_to_players里面说@player.each { ... }
  • 就像 steen 所说的那样?因为是类内部的实例变量,所以不用传?
  • 对,实例变量附加到self,因此它们始终可用。您可能想在使用时将其重命名为 @players,它包含几项内容,因此使用复数名称是有意义的。
  • 顺便说一句,两个空格是相当标准的 Ruby 缩进。 “标准”不是“更好”的同义词,但它会让大多数程序员更容易阅读代码。

标签: ruby arrays methods variadic-functions splat


【解决方案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

【讨论】:

    【解决方案2】:

    我只想添加一个访问您的@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
    

    【讨论】:

    • would 一切都很好,花花公子,实际上是一个非常简单的解决方案,但我试图一次将 1 张牌发给 每个 玩家,就像在真正的纸牌游戏中一样。玩家 1 的第一张牌,玩家 2 的第二张牌,等等。我不确定如何使用这种结构合理地实现它。
    • 好吧,这应该能满足你的需要,假设每个玩家正好需要 5 张牌。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 2015-04-07
    • 1970-01-01
    • 2021-06-02
    • 2011-03-17
    • 2011-08-12
    • 2016-09-26
    相关资源
    最近更新 更多