<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/celebrate-halloween-with-creepy-computer-games-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://lucidmanager.org/creepy-computer-games-in-r/"> The Devil is in the Data – The Lucid Manager</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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 <a rel="nofollow" href="https://lucidmanager.org/pong/" target="_blank">line and a dot</a> become a game of tennis, and a computer that was able to play <a rel="nofollow" href="https://lucidmanager.org/tic-tac-toe-minimax-algorithm/" target="_blank">Tic Tac Toe</a> seemed like your machine had come alive.</p>
<p>This article translates some games from the 1983 <a rel="nofollow" href="https://usborne.com/browse-books/features/computer-and-coding-books/" target="_blank">Creepy Computer Games</a> book to celebrate Halloween.  This article is part of my series on <a rel="nofollow" href="https://lucidmanager.org/tag/r-games/" target="_blank">gaming with the R language</a>.</p>
<h2>Creepy Computer Games in R</h2>
<p>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 <a rel="nofollow" href="https://atari800.github.io/" target="_blank">atari800 emulator</a> to test what it does, after which I converted it to the R language.</p>
<p>Let’s step into the creepy world of computer games as imagined by Usborne Publishing.</p>
<figure class="wp-caption"><a rel="nofollow" href="https://usborne.com/browse-books/features/computer-and-coding-books/" target="_blank"><img data-orig-file="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/Creepy_Computer_Games.jpg?fit=4874%2C4073&amp;ssl=1" data-orig-size="4874,4073" data-medium-file="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/Creepy_Computer_Games.jpg?fit=300%2C251&amp;ssl=1" data-attachment-id="7408" data-image-meta="{" data-image-description="" src="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/Creepy_Computer_Games.jpg?resize=525%2C439&ssl=1" data-permalink="https://lucidmanager.org/creepy-computer-games-in-r/creepy_computer_games/" data-large-file="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/Creepy_Computer_Games.jpg?fit=525%2C439&amp;ssl=1" alt="Reynold, Colin and McCaig, Rob, Creepy Computer Games (Usborne, London)." data-image-title="Creepy_Computer_Games" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" width="450" data-comments-opened="1" class="wp-image-7408" data-recalc-dims="1"></a><figcaption class="wp-caption-text">Reynold, Colin and McCaig, Rob, Creepy Computer Games (Usborne, London).</figcaption></figure>
<h2>Gravedigger</h2>
<p>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 <a rel="nofollow" href="https://drive.google.com/file/d/0Bxv0SsvibDMTRUl3SFRONGN0MFk/view" target="_blank">publisher ‘s Google drive</a>. The <em>Gravedigger</em> listing starts on page 10. The lyrical description of the game provides the instructions:</p>
<blockquote>
<p>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 </p>
<pre>+</pre>
<p>) and  the walls of the graveyard (marked </p>
<pre>:</pre>
<p>) block your way. The holes you digs are marked </p>
<pre>O</pre>
<p>, you are </p>
<pre>*</pre>
<p> and the skeletons are </p>
<pre>X</pre>
<p>. See if you can escape.</p>
<figure class="wp-caption"><img data-orig-file="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/graveyard.jpg?fit=2000%2C1200&amp;ssl=1" data-orig-size="2000,1200" data-medium-file="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/graveyard.jpg?fit=300%2C180&amp;ssl=1" data-attachment-id="7423" data-image-meta="{" data-image-description="" src="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/graveyard.jpg?resize=525%2C315&ssl=1" data-permalink="https://lucidmanager.org/creepy-computer-games-in-r/graveyard/" data-large-file="https://i1.wp.com/lucidmanager.org/wp-content/blogs.dir/4/files/sites/4/2018/10/graveyard.jpg?fit=525%2C315&amp;ssl=1" alt="Gravedigger code snippet" data-image-title="graveyard" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" width="450" data-comments-opened="1" class="wp-image-7423" data-recalc-dims="1"><figcaption class="wp-caption-text">Partial page of the Gravedigger game in BASIC.</figcaption></figure>
</blockquote>
<p>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 <a rel="nofollow" href="https://lucidmanager.org/tic-tac-toe-minimax-algorithm/" target="_blank">Tic Tac Toe</a> game.</p>
<p>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.</p>
<p>You can view the code below, or download it from <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/tree/master/Rcade" target="_blank">GitHub</a>. I leave it up to you to deconstruct the program and safely work your way through the graveyard.</p>
<p>Happy Halloween!</p>

<pre>## 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 &lt;- matrix(ncol = 20, nrow = 10)
A[,] &lt;- &quot; &quot;

## Starting variables
W &lt;- 0 # Move number
X &lt;- 5 # Remaining holes
death &lt;- 0

## Initiate pieces
Y &lt;- &quot;*&quot;
B &lt;- &quot;+&quot;
C &lt;- &quot;O&quot; 
D &lt;- &quot;:&quot;
E &lt;- &quot;X&quot;
Z &lt;- &quot; &quot;

## Draw board
## Add borders
A[c(1, 10), ] &lt;- D
A[10, ] &lt;- D
A[, 1] &lt;- D
A[1:8, 20] &lt;- D
## Add graves
for (i in 1:20){
    A[floor(runif(1) * 7 + 2), floor(runif(1) * 15 + 3)] &lt;- B
}

## Starting positions
## Player
M &lt;- 2
N &lt;- 2
## Skeletons
S &lt;- c(4, 19, 3, 19, 2, 19)

## Game play
repeat{
    ## Position player
    A[N, M] &lt;- Y
    ## Position skeletons
    for (J in seq(1, 5, by = 2)) {
        A[S[J], S[J + 1]] &lt;- J
    }
    W &lt;- W + 1 ## Move counter
    if (W > 60) {
        print(&quot;The clock's struck midnight&quot;)
        print(&quot;Aghhhhh!!!!&quot;)
        break
    }
    ## Clear screen
    #cat(rep(&quot;\n&quot;, 50))
    ## Print board
    v &lt;- paste(as.vector(t(A)), collapse = &quot;&quot;)        
    for (i in 1:10)
        print(substr(v, (i - 1) * 20 + 1, (i - 1) * 20 + 20))
    ## Enter move
    A1 &lt;- toupper(readline(paste0(&quot;Enter move &quot;, W, &quot; (You can go N, S, E or W): &quot;)))
    ## Move player
    T &lt;- N
    U &lt;- M
    if (A1 == &quot;N&quot;) {
        T &lt;- N - 1
    }
    if (A1 == &quot;E&quot;) {
        U &lt;- M + 1
    }
    if (A1 == &quot;S&quot;) {
        T &lt;- N + 1
    }
    if (A1 == &quot;W&quot;) {
        U &lt;- M - 1
    }
    ## Collission detection
    if (A[T, U] == D | A[T, U] == B) { # Edge or grave
        print(&quot;That way's blocked&quot;)
    }
    if (A[T, U] == C) { # Hole
        print(&quot;You've fallen into one of your own holes&quot;)
        break
    }
    if (A[T, U] == E) { # Skeleton
        death &lt;- 1
    }
    if (T == 9 &amp; U == 20) { # Escaped
        print(&quot;You're free!&quot;)
        print(paste0(&quot;Your performance rating is &quot;,
                    floor((60 - W) / 60 * (96 + X)), &quot;%&quot;))
        break
    }
    ## Move player and dig hole
    if (A[T, U] == Z) { # Move into empty space
        A [N, M] &lt;- Z
        if (X != 0) {
            B1 &lt;- toupper(readline(&quot;Would you like to dig a hole (Y or N): &quot;))
            if (B1 == &quot;Y&quot;) {
                X &lt;- X - 1
                A[N, M] &lt;- C
            }
        }
        N &lt;- T
        M &lt;- U
    }
    ## Move skeletons
    for (J in seq(1, 5, by = 2)) {
        T &lt;- S[J]
        U &lt;- 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 &lt;- 1
        }
        if (A1 == &quot;S&quot; &amp; A[T + 1, U] == Z){
            S[J] &lt;- S[J] + 1 # Follow player
        }
        if (A1 == &quot;N&quot; &amp; A[T - 1, U] == Z){
            S[J] &lt;- S[J] - 1 # Follow player
        }
        if (A1 == &quot;E&quot; &amp; A[T, U - 1] == Z &amp; M &lt; U){
            S[J + 1] &lt;- S[J + 1] - 1 # Move towards player
        }
        if (A1 == &quot;E&quot; &amp; A[T, U + 1] == Z &amp; M > U) {
            S[J + 1] &lt;- S[J + 1] + 1 # Reverse direction
        }
        if (A1 %in% c(&quot;S&quot;, &quot;N&quot;, &quot;E&quot;)) {
            A[T, U] &lt;- Z
        }
    }
    if (death == 1) {
        print(&quot;Urk! You've been scared to death by a skeleton.&quot;)
        break
    }
}</pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://lucidmanager.org/creepy-computer-games-in-r/"> The Devil is in the Data – The Lucid Manager</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/M8wRE7Myo2o" height="1"></a></div>