Last active
December 27, 2017 14:57
-
-
Save bernikr/b4482a33086da6c933fad8ae834b8b43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pairsGame <- function(cards, maxTries){ | |
unknown_fields <- sample(c(1:cards, 1:cards), 2*cards, replace=FALSE) | |
known_fields <- c() | |
won_pairs <- c() | |
playRound <- function(){ | |
# check if we already know a pair | |
if(anyDuplicated(known_fields) != 0){ | |
# Open both Card of the pair | |
card <- known_fields[duplicated(known_fields)] | |
won_pairs <<- c(won_pairs, card) | |
known_fields <<- known_fields[known_fields != card] | |
return(TRUE) # Exit with found pair | |
} | |
# Open the first unknown card | |
if(unknown_fields[1] %in% known_fields){ # If the card is known open it | |
won_pairs <<- c(won_pairs, unknown_fields[1]) | |
known_fields <<- known_fields[known_fields != unknown_fields[1]] | |
unknown_fields <<- unknown_fields[-1] | |
return(TRUE) # Exit with found pair | |
}else{ # If the card is new, move it to known cards... | |
known_fields <<- c(known_fields, unknown_fields[1]) | |
unknown_fields <<- unknown_fields[-1] | |
# ...and open the next one | |
known_fields <<- c(known_fields, unknown_fields[1]) | |
unknown_fields <<- unknown_fields[-1] | |
return(FALSE) # Exit without found pair | |
} | |
} | |
# | |
# Play until 3 errors | |
# | |
tries <- 0; | |
while(tries < maxTries && length(won_pairs) < cards){ | |
if(!playRound()){ | |
tries <- tries +1 | |
} | |
} | |
outcome <- -1 | |
if(tries == 0) outcome <- 3 | |
else if(length(won_pairs) >= 7) outcome <- 2 | |
else if(length(won_pairs) >= 5) outcome <- 1 | |
else if(length(won_pairs) >= 2) outcome <- 0 | |
return(c(outcome=outcome, found_pairs=length(won_pairs))) | |
} | |
runAnalysis <- function(x){ | |
print(paste("Spiele", x,"mal Memory...")) | |
data <- data.frame(t(replicate(x, pairsGame(8, 3)))) | |
print(paste("Erwartungswert (laut Gewinn-Schlüssel):", mean(data$outcome))) | |
print("Verteilung der Anzahl der gefundenen Paare:") | |
print(table(data$found_pairs)) | |
hist(data$found_pairs, col="gray", xlab="Gefundene Paare", main="Verteilung der gefundenen Paare", breaks=seq(-0.5, 8.5, by=1)) | |
} | |
printHists <- function(x){ | |
data3 <- data.frame(t(replicate(x, pairsGame(8, 3)))) | |
data4 <- data.frame(t(replicate(x, pairsGame(8, 4)))) | |
data5 <- data.frame(t(replicate(x, pairsGame(8, 5)))) | |
hist(data3$found_pairs, col=rgb(1,0,0,1/4), breaks=seq(-0.5, 8.5, by=1)) | |
hist(data4$found_pairs, col=rgb(0,1,0,1/4), breaks=seq(-0.5, 8.5, by=1), add=T) | |
hist(data5$found_pairs, col=rgb(0,0,1,1/4), breaks=seq(-0.5, 8.5, by=1), add=T) | |
title(xlab="Gefundene Paare", main="Verteilung der gefundenen Paare") | |
} | |
runAnalysis(10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment