**The Devil is in the Data – The Lucid Manager**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the 1980s I spent my time writing code on my 8-bit ZX81 and Atari computers. I learnt everything I know about programming from copying and modifying printed code listings from books with computer games. The games in these books are mostly simple text-based games, but the authors gave them enticing names, often imaginatively illustrated to visualise the virtual world they represent. A line and a dot become a game of tennis, and a computer that was able to play Tic Tac Toe seemed like your machine had come alive.

This article translates some games from the 1983 Creepy Computer Games book to celebrate Halloween. This article is part of my series on gaming with the R language.

## Creepy Computer Games in R

The old books by Usborne publishing are unique because it contains several versions of each program to ensure compatibility with some of the many dialects of the BASIC language. I first entered this code into the atari800 emulator to test what it does, after which I converted it to the R language.

Let’s step into the creepy world of computer games as imagined by Usborne Publishing.

## Gravedigger

Gravedigger by Alan Ramsey is a typical example of the games listed in the books of the early days of home computing. You can download the original book for free from the publisher ‘s Google drive. The *Gravedigger* listing starts on page 10. The lyrical description of the game provides the instructions:

It’s dark and windy—not the kind of night to be lost in a graveyard, but that’s where you are. You have until midnight to find your way out. Skeletons lurk in the shadows waiting to scare you to death should you come to close. You can dig holes to help keep them away but digging is tiring work and you cannot manage more than five in one game. You have to be careful not to fall down the holes you have dug. Grave stones (marked

+) and the walls of the graveyard (marked

:) block your way. The holes you digs are marked

O, you are

*and the skeletons are

X. See if you can escape.

I translated the BASIC code as close to the original as possible. This game is not pretty code, but it works. Some of the variable names have been changed because, in BASIC, single variables and vectors can have the same name and names of character vectors end in $. A future version of this game could use graphics as I did in the Tic Tac Toe game.

The game is quite tricky, and I have only managed to escape the graveyard once. It looks like the likelihood of success very much depends on the random distribution of the graves. Perhaps we need some machine learning to optimise strategy.

You can view the code below, or download it from GitHub. I leave it up to you to deconstruct the program and safely work your way through the graveyard.

Happy Halloween!

## Creepy Computer Games ## Reynold, Colin and McCaig, Rob, Creepy Computer Games (Usborne, London). ## https://archive.org/details/Creepy_Computer_Games_1983_Usborne_Publishing/ ## Gravedigger by Alan Ramsey ## Initiate board A <- matrix(ncol = 20, nrow = 10) A[,] <- " " ## Starting variables W <- 0 # Move number X <- 5 # Remaining holes death <- 0 ## Initiate pieces Y <- "*" B <- "+" C <- "O" D <- ":" E <- "X" Z <- " " ## Draw board ## Add borders A[c(1, 10), ] <- D A[10, ] <- D A[, 1] <- D A[1:8, 20] <- D ## Add graves for (i in 1:20){ A[floor(runif(1) * 7 + 2), floor(runif(1) * 15 + 3)] <- B } ## Starting positions ## Player M <- 2 N <- 2 ## Skeletons S <- c(4, 19, 3, 19, 2, 19) ## Game play repeat{ ## Position player A[N, M] <- Y ## Position skeletons for (J in seq(1, 5, by = 2)) { A[S[J], S[J + 1]] <- J } W <- W + 1 ## Move counter if (W > 60) { print("The clock's struck midnight") print("Aghhhhh!!!!") break } ## Clear screen #cat(rep("\n", 50)) ## Print board v <- paste(as.vector(t(A)), collapse = "") for (i in 1:10) print(substr(v, (i - 1) * 20 + 1, (i - 1) * 20 + 20)) ## Enter move A1 <- toupper(readline(paste0("Enter move ", W, " (You can go N, S, E or W): "))) ## Move player T <- N U <- M if (A1 == "N") { T <- N - 1 } if (A1 == "E") { U <- M + 1 } if (A1 == "S") { T <- N + 1 } if (A1 == "W") { U <- M - 1 } ## Collission detection if (A[T, U] == D | A[T, U] == B) { # Edge or grave print("That way's blocked") } if (A[T, U] == C) { # Hole print("You've fallen into one of your own holes") break } if (A[T, U] == E) { # Skeleton death <- 1 } if (T == 9 & U == 20) { # Escaped print("You're free!") print(paste0("Your performance rating is ", floor((60 - W) / 60 * (96 + X)), "%")) break } ## Move player and dig hole if (A[T, U] == Z) { # Move into empty space A [N, M] <- Z if (X != 0) { B1 <- toupper(readline("Would you like to dig a hole (Y or N): ")) if (B1 == "Y") { X <- X - 1 A[N, M] <- C } } N <- T M <- U } ## Move skeletons for (J in seq(1, 5, by = 2)) { T <- S[J] U <- S[J + 1] ## Collision detection if (any(c(A[T, U + 1], A[T, U - 1], A[T - 1, U], A[T + 1, U]) %in% Y)) { death <- 1 } if (A1 == "S" & A[T + 1, U] == Z){ S[J] <- S[J] + 1 # Follow player } if (A1 == "N" & A[T - 1, U] == Z){ S[J] <- S[J] - 1 # Follow player } if (A1 == "E" & A[T, U - 1] == Z & M < U){ S[J + 1] <- S[J + 1] - 1 # Move towards player } if (A1 == "E" & A[T, U + 1] == Z & M > U) { S[J + 1] <- S[J + 1] + 1 # Reverse direction } if (A1 %in% c("S", "N", "E")) { A[T, U] <- Z } } if (death == 1) { print("Urk! You've been scared to death by a skeleton.") break } }

**leave a comment**for the author, please follow the link and comment on their blog:

**The Devil is in the Data – The Lucid Manager**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.