gpt4 book ai didi

r - 尝试在 R 中随机化石头、剪刀、布游戏

转载 作者:行者123 更新时间:2023-12-01 18:00:17 29 4
gpt4 key购买 nike

我创建了一个 RPS 函数,可以使计算机的移动随机化一回合。例如,如果您输入 rps("rock"),该函数将随机吐出石头、布、剪刀以及结果(赢、输、平)。

rps = function(move){
options = c("rock", "paper", "scissors")
comp.move = sample(options, size = 1)
if(move == "rock" & comp.move == "rock"){
names(comp.move) = "tie"
}else
if(move == "rock" & comp.move == "scissors"){
names(comp.move) = "loss"
}else
if(move == "rock" & comp.move == "paper"){
names(comp.move) = "win"
}else
if(move == "paper" & comp.move == "paper"){
names(comp.move) = "tie"
}else
if(move == "paper" & comp.move == "scissors"){
names(comp.move) = "win"
}else
if(move == "paper" & comp.move == "rock"){
names(comp.move) = "loss"
}else
if(move == "scissors" & comp.move == "scissors"){
names(comp.move) = "tie"
}else
if(move == "scissors" & comp.move == "rock"){
names(comp.move) = "win"
}else
if(move == "scissors" & comp.move == "paper"){
names(comp.move) = "loss"
}
return(comp.move)
}

现在,我想玩一个发生了 50 次这种情况的游戏版本。我已经创建了一个长度为 50 的向量,其中包含预先记录的人类 Action 。

human.move = c("rock", "paper", "scissors", "paper", "paper", "rock", "scissors", "rock", "rock", "paper", "paper", "scissors", "rock", "rock", "paper", "paper", "paper", "scissors", "paper", "rock", "paper", "rock", "rock", "scissors", "scissors", "paper", "rock", "paper", "scissors", "rock", "paper", "paper", "scissors", "rock", "paper", "rock", "paper", "paper", "scissors", "scissors", "paper", "rock", "rock", "scissors", "scissors", "rock", "paper", "scissors", "scissors", "rock")

如何让计算机对该向量中的每个元素运行 rps 函数,并给出结果?目前,我的函数仅采用人类移动向量的第一个元素(即岩石),并且仅将其随机移动与其进行比较。

rps.random=function(move,n=50){
comp.moves = vector("character")

for(i in human.move){
comp.move=rps(move[i])
comp.moves = append(comp.moves,comp.move, after = length(comp.moves))
}
return(comp.moves)
}

最佳答案

这里有一些建议。首先,更新您的功能。您可以通过使用一些带有 | 运算符的“或”语句来减少所有这些 if() 语句,以表示获胜或失败,并且使用 identical( ) 平局函数。另外,据我了解,像这样的随机游戏应该一次玩一轮,并重复一轮以上。因此,该函数检查 move 中给出的单长度向量。

rps <- function (move) {
opts <- c("rock", "paper", "scissors")
stopifnot(length(move) == 1L, move %in% opts)
cpu <- sample(opts, 1)
names(cpu) <- if (identical(move, cpu)) {
"tie"
}
else if (move == "rock" & cpu == "scissors" | move == "paper" &
cpu == "rock" | move == "scissors" & cpu == "paper") {
"win"
}
else {
"loss"
}
cpu
}

这里有几个运行示例 -

rps("paper")
# win
# "rock"
rps("rock")
# loss
# "paper"

现在您无需手动创建包含 50 个项目的向量。我们可以通过使用 move 参数中的示例来重复该过程 50 次。

replicate(50, rps(sample(c("rock", "paper", "scissors"), 1)))
# win win loss win loss tie tie
# "scissors" "paper" "paper" "paper" "rock" "paper" "scissors"
# win loss loss tie loss win win
# "rock" "paper" "paper" "paper" "paper" "scissors" "scissors"
# tie loss tie win win tie loss
# "scissors" "rock" "scissors" "paper" "paper" "scissors" "rock"
# tie tie loss tie win win win
# "rock" "scissors" "scissors" "scissors" "rock" "scissors" "scissors"
# win win loss win tie tie win
# "scissors" "scissors" "paper" "paper" "paper" "paper" "paper"
# win tie win tie tie win win
# "paper" "rock" "rock" "paper" "scissors" "paper" "scissors"
# loss tie tie loss tie win win
# "rock" "rock" "paper" "scissors" "scissors" "rock" "rock"
# win
# "paper"

当然,如果您确实有输入向量,我们可以通过 sapply() 运行它,以查看与上面相同的结果(名称中包含或不包含输入向量)。

sapply(c("rock", "paper", "scissors"), rps)
# rock.loss paper.win scissors.tie
# "paper" "rock" "scissors"
sapply(c("rock", "paper", "scissors"), rps, USE.NAMES = FALSE)
# tie loss win
# "rock" "scissors" "paper"

更新:我突然意识到,在上述情况下,我们从 replicate() 获得的输入是不知道的,所以这里有一个使固定。如果我们将函数的最后一行更改为矩阵(或列表),我们还可以包含输入内容。我们可以在最后一行使用类似的内容 =

matrix(c(move, cpu), 1, dimnames = list(wlt, c("move", "cpu")))

然后我们会得到以下结果 -

rps("paper")
# move cpu
# loss "paper" "scissors"
rps("paper")
# move cpu
# tie "paper" "paper"

现在我们知道我们输入了什么作为输入,而无需做任何额外的工作。我们还可以与我们的老 friend do.call(rbind, ...) 一起将复制放入一个矩阵中。

关于r - 尝试在 R 中随机化石头、剪刀、布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113184/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com