0

目標:最終的に、ユーザーが一連の「ゲーム」を実行できるようにする R パッケージを作成します。このゲームでは、2 人のプレイヤーが互いに対戦し、各ラウンド (合計 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]

私がいる場所:

C と D のベクトルを組み合わせて、C と D のペアのマトリックスにする部分を説明しました。

コード:

   #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
4

0 に答える 0