--- title: "Fortnite Simulation" author: "Laurent DELSOL" output: html_document: code_folding: "hide" params: N: 100 NP: 1000 Skill_t: TRUE Skill_init: rep(1,params$N) Agress: c(1,rep(1,params$N-1)) --- ```{r setup, include=FALSE} require(knitr) require(kableExtra) knitr::opts_chunk$set(echo = TRUE) ``` Simulations of duels between `r params$N` individuals. The results presented correspond to `r params$NP` simulated games. The following parameters were used: - Initial skill levels: `r eval(parse(text=params$Skill_init))` - Aggressiveness levels: `r eval(parse(text=params$Agress))` - Transfer of skill to the winner: `r if (params$Skill_t) {' YES'} else {' NO'}` ```{r Simulation of games} ###### R code used to simulate the games ### Number of players N=params$N ### Number of games NP=params$NP ### Initial skill level of players Skill_init=eval(parse(text=params$Skill_init)) #c(1,rep(0,N-1)) if player 1 is stronger than all others #rep(1/N,N) if everyone has the same skill level ### Skill of the defeated player transferred to the winner Skill_t=params$Skill_t ### Aggressiveness (probability of being chosen for a duel) Agress= eval(parse(text=params$Agress)) #rep(1,N) # c(2,rep(1,N-1)) if player 1 is twice as aggressive as all the others ### Saving results # Number of victories for each player NVs=matrix(NA,NP,N) # and for the winner NVs_G=rep(NA,NP) # Skill of the winner Skill_G=rep(NA,NP) # Aggressiveness of the winner Agress_G=rep(NA,NP) # Winner of the round Gagnant=rep(NA,NP) # History of matches Hist=array(NA,c(NP,N-1,2)) for (s in 1:NP) { # Player numbers JA=1:N # Initial skill levels Skill=Skill_init # Initialize the number of victories for each player NV=rep(0,N) for (i in 1:(N-1)) { # Selection of duelists JC=sample(JA,2,replace=FALSE,prob=Agress[JA]) # probability # Winning probability for duelist number 2 if (sum(Skill[JC])!=0) {p2=Skill[JC[2]]/sum(Skill[JC])} else {p2=.5} # Determining the winner PF=rbinom(1,1,p2)+1 # Updating the number of victories, and identifying the winner and loser of the duel NV[JC[PF]]=NV[JC[PF]]+1 JG=JC[PF] JP=JC[-PF] Hist[s,i,]=c(JG,JP) # Transferring skills if (Skill_t==TRUE) { Skill[JG]=Skill[JG]+Skill[JP] Skill[JP]=0 } # Updating the list of players still alive JA=JA[(JA!=JP)] } # Saving the results of the round Skill_G[s]=Skill[JA] Agress_G[s]=Agress[JA] Gagnant[s]=JA NVs[s,]=NV NVs_G[s]=NVs[s,JA] } # Maximum number of victories per game Max_v=apply(NVs,1,max) ``` # Which statistic should be displayed? {.tabset .tabset-fade .tabset-pills} ## Number of kills by the winner + The mean is `r mean(NVs_G)` + The standard deviation across the simulated games is `r sd(NVs_G)`. ```{r Winner Stats, fig.height=3.5, table.width=10} # Frequency chart library(knitr) library(kableExtra) moda=sort(unique(NVs_G)) E=table(NVs_G) f=as.numeric(E/sum(E)) plot(moda,f,type='h',ylab='frequency',xlab='Number of kills by the winner') kable(t(E),col.names=moda,striped=TRUE,caption='Results obtained (values ​​- frequencies)')%>% kable_styling()%>% scroll_box(width = "100%") ``` ## Maximum number of kills (across all players) + The mean is `r mean(Max_v)` + The standard deviation across the simulated games is `r sd(Max_v)`. ```{r Stats - Top Killer, fig.height=3.5} # Frequency chart library(knitr) library(kableExtra) moda = sort(unique(Max_v)) E = table(Max_v) f = as.numeric(E/sum(E)) plot(moda, f, type='h', ylab='frequency', xlab='Maximum number of kills') kable(t(E), col.names=moda, striped=TRUE, caption='Results obtained (values ​​- frequencies)') %>% kable_styling() %>% scroll_box(width = "100%") ``` ## All results ```{r All stats, fig.height=3.5} # Frequency chart library(knitr) library(kableExtra) M = cbind(1:NP, NVs_G, Max_v, Skill_G, Agress_G, Gagnant) colnames(M) = c('Game', 'Winner\'s kill count', 'Maximum kill count', 'Skill accumulated by the winner', 'Winner\'s aggressiveness', 'Winner\'s identity') kable(M, 'html', striped=TRUE, caption='Results obtained', width='100%', height='10cm') %>% kable_styling() %>% scroll_box(width = "100%", height='10cm') ```