<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/a-maze-solving-minecraft-robot-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2018/04/minecraft-robot-in-r.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>Last week at the New York R Conference, I gave a presentation on using <a rel="nofollow" href="https://www.slideshare.net/RevolutionAnalytics/r-in-minecraft" target="_blank">R in Minecraft</a>. (I've embedded the slides below.) The demo gods were not kind to me, and while I was able to show building a randomly-generated maze in the Minecraft world, my attempt to have the player solve it automatically was stymied by some server issues. In this blog post, I'll show you how you can write an R function to build a maze, <em>and</em> use the left-hand rule to solve it automatically.</p>
<p><iframe allowfullscreen="" src="http://www.slideshare.net/slideshow/embed_code/key/3kH37G6A0aPh4" width="450" height="420"> </iframe></p>
<p>If you want to play along, you'll need to launch a Spigot server with the RaspberryJuice plugin. An easy way to do this is to <a rel="nofollow" href="https://ropenscilabs.github.io/miner_book/installation-and-configuration.html#docker" target="_blank">use a Dockerfile to launch the Minecraft server</a>. I used a Ubuntu instance of the <a rel="nofollow" href="https://azuremarketplace.microsoft.com/en-us/marketplace/apps/microsoft-ads.linux-data-science-vm-ubuntu" target="_blank">Data Science Virtual Machine</a> to do this, mainly because it comes with Docker already installed. Once you have your server running, you can join the multiplayer with from the <a rel="nofollow" href="https://minecraft.net/en-us/download/" target="_blank">Minecraft Java Edition</a> using its IP address.</p>
<p>The R script <a rel="nofollow" href="https://github.com/revodavid/minecraft-maze/blob/master/mazebot.R" target="_blank">mazebot.R</a> steps through the process of connecting to the server from R, and building and solving a maze. It uses the <a rel="nofollow" href="https://github.com/ropenscilabs/miner" target="_blank">miner package</a> (which you will need to install from Github), which provides functions to interface between R and Minecraft. After connecting to the server, the first step is to get the ID code used to identify the player in Minecraft. (It's best to have just one player in the Minecraft world for this step, so you're guaranteed to get the active player's ID.)</p>
<pre>id &lt;- getPlayerIds()
</pre>
<p>You'll use that <code>id</code> code to identify the player later on. Next, we use the <code>make_maze</code> function (in the script <a rel="nofollow" href="https://github.com/revodavid/minecraft-maze/blob/master/genmaze.R" target="_blank">genmaze.R</a>) to design a random maze. This uses a simple <a rel="nofollow" href="http://weblog.jamisbuck.org/2010/12/27/maze-generation-recursive-backtracking" target="_blank">recursive backtracking algorithm</a> algorithm: explore in random directions until you can go no further, and then retreat until a new route is available to explore. Once we've generated the maze, we use <code>print_maze</code> to convert it to a matrix of characters, and place a marker <code>&quot;!&quot;</code> for the exit.</p>
<pre>## Maze dimensions (we'll create a square maze)
mazeSize &lt;- 8

## using the functions in genmaze.R:
m &lt;- print_maze(make_maze(mazeSize,mazeSize), print=FALSE)
nmaze &lt;- ncol(m) # dimensions
m[nmaze,nmaze-1] &lt;- &quot;!&quot; ## end of the maze. 
</pre>
<p>Now for the fun bit: building the maze in the world. This is a simple matter of looping over the character matrix representing the maze, and building a stack of three blocks (enough so you can't see over the top while playing) where the walls should go. The function <code>build_maze</code> (in <a rel="nofollow" href="https://github.com/revodavid/minecraft-maze/blob/master/solvemaze.R" target="_blank">solvemaze.R</a>) does this, so all we need to do is provide a location for the maze. Find a clear spot of land, and the code below builds the maze nearby:</p>
<pre>## get the current player position 
v &lt;- getPlayerPos(id, TRUE)
altitude &lt;- -1 ## height offset of maze
pos &lt;- v+c(3, altitude, 3) # corner

## Build a maze near the player
buildMaze(m, pos, id)
</pre>
<p>You can try solving the maze yourself, just by moving the player in Minecraft. It's surprisingly how difficult even small mazes can be, if you don't cheat by looking at the layout of the maze from above! A simple way to solve this and many other mazes is by using the <em>left hand rule</em>: follow the wall on your left until you find the exit. This is something we can also code in R to solve the maze automatically, to check the positions of walls around the player, and move the player according to the left hand rule. Unfortunately, you can't actually make the player avatar turn using Spigot, so we track the direction the player should be facing with the <span><code>botheading</code> variable in the <a rel="nofollow" href="https://github.com/revodavid/minecraft-maze/blob/master/solvemaze.R" target="_blank">solveMaze function</a>, which we use like this:</span></p>
<pre>## Move the player into the start of the maze, and then
solveMaze(id)
</pre>
<p>And here's what it looks like from a third-person view (use F5 in the game to change the view):</p>
<p><a rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b0224e0353446200d-pi" target="_blank"><img border="0" alt="Solvedmaze" title="Solvedmaze" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b0224e0353446200d-800wi"></a></p>
<p>You can find all of the R code to implement the maze-building and maze-solving at the Github repository below. For more on using R in Minecraft, check out the online book <a rel="nofollow" href="https://ropenscilabs.github.io/miner_book/index.html" target="_blank">R Programming in Minecraft</a>, which has lots of other ideas for building and playing in Minecraft with R.</p>
<p>Github (revodavid): <a rel="nofollow" href="https://github.com/revodavid/minecraft-maze" target="_blank">minecraft-maze</a></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2018/04/minecraft-robot-in-r.html"> Revolutions</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/7X2n-N1eUX0" height="1"></a></div>