## SET YOUR WORKING DIRECTORY TO THE FOLDER gamefor202a/images ## For me this is setwd("/Users/rickpaikschoenberg/Desktop/game/images") install.packages("beepr") install.packages("png") install.packages("profvis") install.packages("stringr") library(beepr) library(png) library(profvis) library(stringr) x = scan("categories.txt",sep=",",what="CHAR") choosie = function(i,w,topper){ plot(c(0,1),c(0,1),type="n",xlab="",ylab="",axes=F) bgcol = sample(8)[1] rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = bgcol) abline(v=.5,lty=2) mtext(s=3,topper,cex=2) d = paste(str_replace_all(w[i,1],"\\s",""),".png",sep="") j = readPNG(d,native=TRUE) rasterImage(j,0,.2,.5,1) d = paste(str_replace_all(w[i,2],"\\s",""),".png",sep="") j = readPNG(d,native=TRUE) rasterImage(j,.5,.2,1,1) text(.25,.1,w[i,1],cex=2,col=bgcol + sample(7)[1]) text(.75,.1,w[i,2],cex=2,col=bgcol + sample(7)[1]) a = locator(1)[1] beep(2) if(a <= .5) b = w[i,1] if(a>.5) b = w[i,2] b } plotbig = function(w,topper){ plot(c(0,1),c(0,1),type="n",xlab="",ylab="",main=topper,axes=F) rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = 1+sample(7)[1]) text(.1,.9,w[1,1]) text(.1,.8,w[1,2]) text(.1,.7,w[2,1]) text(.1,.6,w[2,2]) text(.1,.5,w[3,1]) text(.1,.4,w[3,2]) text(.1,.3,w[4,1]) text(.1,.2,w[4,2]) text(.9,.9,w[5,1]) text(.9,.8,w[5,2]) text(.9,.7,w[6,1]) text(.9,.6,w[6,2]) text(.9,.5,w[7,1]) text(.9,.4,w[7,2]) text(.9,.3,w[8,1]) text(.9,.2,w[8,2]) lines(c(.2,.25,.2,.25,.3),c(.9,.85,.8,.85,.85)) lines(c(.2,.25,.2,.25,.3),c(.9,.85,.8,.85,.85)-.2) lines(c(.2,.25,.2,.25,.3),c(.9,.85,.8,.85,.85)-.4) lines(c(.2,.25,.2,.25,.3),c(.9,.85,.8,.85,.85)-.6) lines(c(.8,.75,.8,.75,.7),c(.9,.85,.8,.85,.85)) lines(c(.8,.75,.8,.75,.7),c(.9,.85,.8,.85,.85)-.2) lines(c(.8,.75,.8,.75,.7),c(.9,.85,.8,.85,.85)-.4) lines(c(.8,.75,.8,.75,.7),c(.9,.85,.8,.85,.85)-.6) } firstround = function(v){ text(.28,.87,v[1]) text(.28,.67,v[2]) text(.28,.47,v[3]) text(.28,.27,v[4]) text(.73,.87,v[5]) text(.73,.67,v[6]) text(.73,.47,v[7]) text(.73,.27,v[8]) } semis = function(v2) { text(.4,.77,v2[1]) text(.4,.37,v2[2]) text(.6,.77,v2[3]) text(.6,.37,v2[4]) lines(c(.3,.38,.3,.38,.43,.45,.5),c(.85,.75,.65,.75,.75,.5,.5)) lines(c(.3,.38,.3,.38,.43,.45,.5),c(.85,.75,.65,.75,.75,.9,.9)-.4) lines(c(.7,.65,.7,.65,.6,.55,.53),c(.85,.75,.65,.75,.75,.42,.42)) lines(c(.7,.65,.7,.65,.6,.55,.53),c(.45,.35,.25,.35,.35,.42,.42)) } fins = function(v4){ text(.47,.5,v4[1],cex=1.5) text(.54,.47,v4[2],cex=1.5) } readin = function(fname) matrix(scan(paste(fname,".txt",sep=""),what="char",sep="\n"),nrow=31) findcor = function(fname1,fname2){ ans = readin(fname1) ans2 = readin(fname2) z = intersect(unique(c(ans)),unique(c(ans2))) if(length(z) == 0){ cat("Never had the same choices.\n") return(0) } a = c() b = c() for(i in 1:length(z)){ a[i] = (sum(ans == z[i]) - sum(ans[1:16,] == z[i]))/sum(ans[1:16,] == z[i]) ## average number of wins. b[i] = (sum(ans2 == z[i]) - sum(ans2[1:16,] == z[i]))/sum(ans2[1:16,] == z[i]) } cor(a,b) } game = function(s=sample(200)[1]){ set.seed(s) name = readline("What is your name? ") fileit = paste(name,".txt",sep="") plot(c(0,1),c(0,1),type="n",xlab="",ylab="",axes=F) rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = sample(8)[1]) abline(v=.5,lty=2) x2 = matrix(x,ncol=2,byrow=T) y = sample(nrow(x2))[1:8] w = x2[y,] for(i in 1:8){ write(file=fileit,w[i,],append=T) #write(file=fileit,c(w[i,1],"\n",w[i,2]),append=T) } v = rep(0,8) topper = expression(italic("Round One")) plotbig(w,topper) locator(1) for(i in 1:8){ v[i] = choosie(i,w,topper) write(file=fileit,v[i],append=T) } topper = expression(italic("Round Two")) plotbig(w,topper) firstround(v) locator(1) u = matrix(v,ncol=2) v2 = rep(0,4) v3 = v v = matrix(v,byrow=T,ncol=2) for(i in 1:4) v2[i] = choosie(i,v,topper) write(file=fileit,v2,append=T) topper = expression(italic("Semifinals")) plotbig(w,topper) firstround(v3) semis(v2) locator(1) w2 = matrix(v2,ncol=2,byrow=T) v4 = rep(0,2) for(i in 1:2) v4[i] = choosie(i,w2,topper) write(file=fileit,v4,append=T) plot(c(0,1),c(0,1),type="n",xlab="",ylab="",axes=F) rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = sample(8)[1]) plotbig(w,topper) firstround(v3) semis(v2) fins(v4) locator(1) topper = expression(italic("Finals")) w2 = matrix(v4,ncol=2) v = 1 v = choosie(1,w2,topper) write(file=fileit,v,append=T) plot(c(0,1),c(0,1),type="n",xlab="",ylab="",axes=F) bgcol = sample(8)[1] rect(par("usr")[1],par("usr")[3]+.1,par("usr")[2],par("usr")[4]+.1,col = bgcol) d = paste(str_replace_all(v,"\\s",""),".png",sep="") d2 = readPNG(d,native=TRUE) rasterImage(d2,.1,.2,.9,.9) text(.5,.12,v,cex=3,col=bgcol+sample(7)[1]) mtext(s=3, "Winner!",col="blue",cex=2)   n = 200 x1 = runif(n) w1 = sample(n) beep(8) if(v == "animalopoly") print(.1,.1,"You love animals and like board games. A good profession for you would be a businessperson or a vet. You do not like school, and you like food.") j = sample(8)[1]+1 r = paste("When you grow up you will be a",v,"expert. \n", "You will eat only",v,"for breakfast, lunch, and dinner,\n", "and you will have",j,"children who will all look like",v,".\n") mtext(s=1,l=2,r) for(i in 1:n){ text(runif(1),.1+runif(1)*.1,col=w1[i],cex=2,"*") text(-.1+runif(1)*.2,.1+.9*runif(1),col=w1[i],cex=2,"*") text(runif(1),.9+runif(1)*.2,col=w1[i],cex=2,"*") text(.9+runif(1)*.2,.1+.9*runif(1),col=w1[i],cex=2,"*") pause(.008) } } game(75) ## change this 75 to whatever you like, or just do game() ## the possible advantage of setting the number is if you want all of the players ## to get exactly the same questions.