目标:最终生成一个R包,允许用户运行一系列“游戏”。在游戏中,两个玩家互相攻击,并且在每一轮(总共100个)中玩家选择玩C或D.这些是通过移动集来完成的,其形式为
(1)“C”或“D”之间的预定义选择列表,即50“C”的矢量,然后是50“D”或
(2)一组逻辑指令,如第1轮播放“C”,R2播放“C”如果对手在R1中播放“C”则播放“D”,R3-R99随机播放,R100始终播放“D” ”
每轮都有4种可能的结果。
P1的选择| P2的选择== [P1得分] [P2得分]
C|C==[3][3]
C|D==[0][5]
D|C==[5][0]
D|D==[1][1]
我在哪里:
我已经将Cs和Ds的矢量组合成一个成对的Cs和Ds矩阵。
代码:
#Sample Strategies#
p1 <- c("C","D","C","D","D")
p2 <- c("D","D","D","D","D")
p3 <- c("C","C","C","C","C")
p4 <-c("D","D","C","D","C")
#Combining into a matrix#
gameboard<-cbind(p1, p2, p3, p4, deparse.level = 1)
#First Part of Function#
my_game <- function(trial, n_samples, dat) {
# as per my comment, generate the game result and name using the colnames directly
game <- sample(colnames(dat), n_samples)
list_name <- paste0("", paste(game, collapse=" V "))
game_result <- paste(dat[, game[1]],
dat[, game[2]],
sep='')
# return both the name and the data in the format desired to parse out later
return(list(list_name, game_result))
}
#Second Part of the Function#
my_game_wrapper <- function(trials, n_samples, dat) {
# for multiple trials we create a list of lists of the results and desired names
results <- lapply(1:trials, my_game, n_samples, dat)
# extract the names and data
result_names <- sapply(results, '[', 1)
result_values <- sapply(results, '[', 2)
# and return them once the list is pretty.
names(result_values) <- result_names
return(result_values)
}
#Applying the function#
result<-my_game_wrapper(10, 2, gameboard)
Dataframing it
coolresults<-as.data.frame(do.call(rbind, result))
我缺少什么
分数功能的统计。
新矩阵应该是什么样的。
P1 P2
Round1 [CD] 0 5
Round2 [DD] 1 1
Etc
但是,让我们说有30名球员,每个球员都会打对方球员和自己。我想这意味着900个矩阵,对吗?因此,在100轮之后创建每个玩家总分的超级矩阵会更有帮助。
分数是排球员在比赛时的总得分:
P1 P2 P3 P4 P5
P1 462 453 252 560 600
P2 301 242 437 555 439
P3 232 522 555 232 527
P4 etc
P5