2

I'm trying to write a function (or the best R alternative), given a deck vector and a hand vector, that takes the first element from the deck and adds it to the hand.

# initialize the deck.  We don't care about suits, so modulus 13
deck <- sample(52, 52, replace = FALSE, prob = NULL) %% 13 + 1

# initilize the hand
hand <- vector(mode="numeric", length=0)

#deal a card from the deck to the hand.
# it's these two lines I'd like to put in something like a function 
#   and return the modified vectors
  phand <- c(hand, deck[1])
  deck <- deck[-1]

In something like Python, you might do it like this:

def dealcard(deck,hand)

   # do something to deck and hand
   return deck, hand

deck,hand = dealcard(deck,hand)

Looking at the first answer to this: modify variable within R function

There are ways as @Dason showed, but really - you shouldn't!

The whole paradigm of R is to "pass by value". @Rory just posted the normal way to handle it - just return the modified value...

So what is the most R-thonic way to accomplish this "modifying multiple arguments to a function"?

I could see accomplishing something similar with a matrix where one column uses numbers to indicate which hand a row/card is dealt to, but it would make the code much less intuitive. It's easy to imagine a function:

score <- ScoreFunction(DealerHand)

vs.:

score <- ScoreFunction(DeckMatrix, 1)
4

1 回答 1

2

简短的回答

你可以从你的函数中返回一个列表。

简单的例子

dealcard <- function(deck, hand) {
    # There needs to be a card to deal
    stopifnot(length(deck) > 0)

    # Add the card from the top of the deck to the hand
    hand <- c(hand, deck[1])

    # Remove the card from the deck
    deck <- deck[-1]

    # Create a named list from the modified inputs
    output <- list(deck=deck, hand=hand)

    return(output)
}

但请注意,这种方法实际上并没有修改全局环境中的deckhand对象。(您可以使用 来执行此操作<<-,但在大多数情况下您不应该这样做。)因此您可以这样做:

# Initialize deck and hand as a list
# Full deck, empty hand
deck.hand <- list(deck=sample(52, 52, replace=FALSE), hand=NULL)

# Deal a card
deck.hand <- with(deck.hand, dealcard(deck, hand))

现在,如果您查看deck.hand,您会看到deckhand项目已适当更新。

更多涉及的例子

当同一个列表中有多个玩家时,这可以很容易地扩展到与某个玩家交易。

# Initialize the game
# Three players, everyone starts with empty hands
game <- list(deck=sample(52, 52, replace=FALSE) %% 13 + 1,
             dealer=NULL, sally=NULL, john=NULL)


dealcard <- function(game, player, deck="deck") {
    # Make sure the player specified is in the game
    stopifnot(player %in% names(game) && deck %in% names(game))

    # There needs to be a card to deal
    stopifnot(length(game[[deck]]) > 0)

    # Give the next card to the specified player
    game[[player]] <- c(game[[player]], game[[deck]][1])

    # Remove the card from the deck
    game[[deck]] <- game[[deck]][-1]

    # Return the new state of the game
    return(game)
}

# Give everyone a card
game <- dealcard(game, "dealer")
game <- dealcard(game, "sally")
game <- dealcard(game, "john")

这使用语法list[["itemname"]]。列表项作为字符串传递。这相当于使用$, 即提取项目list$itemname

这里我们传入整个列表,只修改它的必要部分,并返回整个修改后的列表。将原始列表设置为等于返回值就像在原地修改列表一样。

现在,如果您查看 的内容game,每个玩家将拥有一张牌,而牌组将缺少已分发的牌。

有趣的扩展

我对此有点过分了。

想连续给每位玩家发五张牌?

players <- names(game)[names(game) != "deck"]

for (i in 1:5) {
    for (player in players) {
        game <- dealcard(game, player)
    }
}

再来看看game。每个玩家有 5 张牌,由于循环是以这种方式构建的,因此发牌顺序模仿了标准纸牌游戏的顺序。

"deck"您可以通过使用as player 和 player 作为卡组将卡返回卡组,如下所示:

# John returns his first card to the deck
game <- dealcard(game, player="deck", deck="john")

给定一个以某种方式得分的函数,您可以轻松获得每个玩家的得分。

# Example scoring
scorehand <- function(game, player) {
    return(sum(game[[player]]))
}

sapply(players, scorehand, game=game)

这将通过将函数应用于由球员姓名组成scorehand()的向量的每个元素,向您显示每个球员的当前得分。players

TL;博士

使用列表。

于 2015-04-15T23:16:45.053 回答