This is a small R game testing your brain memory. Just click the numbers in the ascending order. Have fun.
library(plotrix) # Use draw.circle function in the package
draw.location <- function(n) {
# draw n random locations on a [0,1]X[0,1] region without overlap
locIndex <- sample(1:100, n, replace = FALSE)
rowNo <- ceiling(locIndex/10)
colNo <- locIndex %% 10
colNo[colNo == 0] <- 10
coord <- cbind((colNo/10 - 0.05), (rowNo/10 - 0.05))
return(list(locIndex = locIndex, coord = coord))
}
radius <- 0.04
n <- 3
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", main = "Train Your Brain", axes = FALSE)
text(x = 0.5, y = 0.7, labels = "Click the numbers in the ascending order", cex = 1.2)
rect(0.3, 0.2, 0.6, 0.4)
text(x = 0.45, y = 0.3, labels = "Start", cex = 1.5)
repeat { # Read the cursor location
loc <- locator(n = 1)
if ((loc$x <= 0.6) & (loc$x >= 0.3) & (loc$y >= 0.2) & (loc$y <= 0.4)) break
}
# start the program
for (tries in 1:8) {
curDraw <- draw.location(n)
coords <- curDraw$coord
locIndex <- curDraw$locIndex
nums <- sample(0:9, n, replace = FALSE)
sortedIndex <- locIndex[order(nums)]
for (j in 3:1) {
for (i in 360:1) {
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", main = "Train Your Brain", axes = FALSE)
text(0.5, 0.5, labels = j, cex = 7 * i/360)
Sys.sleep(0.0001)
}
}
for (i in 1:n) text(x = coords[i, 1], y = coords[i, 2], labels = nums[i], cex = 2)
Sys.sleep(1)
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", main = "Train Your Brain", axes = FALSE)
for (i in 1:n) draw.circle(x = coords[i, 1], y = coords[i, 2], radius, lty = 1, lwd = 1)
gotIt <- TRUE
clickedLoc <- NULL
for (ii in 1:n) {
repeat { # Read the cursor location
loc <- locator(n = 1)
locRow <- ceiling(loc$y * 10)
locCol <- ceiling(loc$x * 10)
jLoc <- (locRow - 1) * 10 + locCol
if (is.element(jLoc, locIndex) & !is.element(jLoc, clickedLoc)) {
break
}
}
jCoord <- which(jLoc == locIndex)
clickedLoc <- c(clickedLoc, jLoc)
text(x = coords[jCoord, 1], y = coords[jCoord, 2], labels = nums[jCoord], cex = 2)
Sys.sleep(1)
if (jLoc != sortedIndex[ii]) {
text(x = 0.5, y = 0.5, labels = "You are Wrong! Try again!", cex = 2)
Sys.sleep(1)
gotIt <- FALSE
break
}
}
if (gotIt) text(x = 0.5, y = 0.5, labels = "Correct", cex = 2)
Sys.sleep(1)
n <- ifelse(gotIt, n + 1, n)
}
draw.location <- function(n) {
# draw n random locations on a [0,1]X[0,1] region without overlap
locIndex <- sample(1:100, n, replace = FALSE)
rowNo <- ceiling(locIndex/10)
colNo <- locIndex %% 10
colNo[colNo == 0] <- 10
coord <- cbind((colNo/10 - 0.05), (rowNo/10 - 0.05))
return(list(locIndex = locIndex, coord = coord))
}
radius <- 0.04
n <- 3
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", main = "Train Your Brain", axes = FALSE)
text(x = 0.5, y = 0.7, labels = "Click the numbers in the ascending order", cex = 1.2)
rect(0.3, 0.2, 0.6, 0.4)
text(x = 0.45, y = 0.3, labels = "Start", cex = 1.5)
repeat { # Read the cursor location
loc <- locator(n = 1)
if ((loc$x <= 0.6) & (loc$x >= 0.3) & (loc$y >= 0.2) & (loc$y <= 0.4)) break
}
# start the program
for (tries in 1:8) {
curDraw <- draw.location(n)
coords <- curDraw$coord
locIndex <- curDraw$locIndex
nums <- sample(0:9, n, replace = FALSE)
sortedIndex <- locIndex[order(nums)]
for (j in 3:1) {
for (i in 360:1) {
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", main = "Train Your Brain", axes = FALSE)
text(0.5, 0.5, labels = j, cex = 7 * i/360)
Sys.sleep(0.0001)
}
}
for (i in 1:n) text(x = coords[i, 1], y = coords[i, 2], labels = nums[i], cex = 2)
Sys.sleep(1)
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", main = "Train Your Brain", axes = FALSE)
for (i in 1:n) draw.circle(x = coords[i, 1], y = coords[i, 2], radius, lty = 1, lwd = 1)
gotIt <- TRUE
clickedLoc <- NULL
for (ii in 1:n) {
repeat { # Read the cursor location
loc <- locator(n = 1)
locRow <- ceiling(loc$y * 10)
locCol <- ceiling(loc$x * 10)
jLoc <- (locRow - 1) * 10 + locCol
if (is.element(jLoc, locIndex) & !is.element(jLoc, clickedLoc)) {
break
}
}
jCoord <- which(jLoc == locIndex)
clickedLoc <- c(clickedLoc, jLoc)
text(x = coords[jCoord, 1], y = coords[jCoord, 2], labels = nums[jCoord], cex = 2)
Sys.sleep(1)
if (jLoc != sortedIndex[ii]) {
text(x = 0.5, y = 0.5, labels = "You are Wrong! Try again!", cex = 2)
Sys.sleep(1)
gotIt <- FALSE
break
}
}
if (gotIt) text(x = 0.5, y = 0.5, labels = "Correct", cex = 2)
Sys.sleep(1)
n <- ifelse(gotIt, n + 1, n)
}
No comments:
Post a Comment