Computers exist to solve pressing problems to improve the human condition. Although our world is full of urgent issues to address, the human need for play is a robust internal motivator. This general statement also applies to the R Project for Statistical Computing. Analysing data analysis is a serious business without much room for fun. This specialised computer language is not designed for games in R.
Hacking culture is one of the main reasons that computer science has evolved so fast. One of the many definitions of hacking is to use technology for a purpose it was not designed for, and thus writing games in the R language is an exciting challenge.
Not many R developers have written games. R has a games package in CRAN, but this library is about the mathematics of game theory. This article explains the R language’s possibilities and limitations for writing games, plus links to actual games others and myself have developed.
Fully functioning action or arcade games are not easy to create in the R language because it has no interrupt function to process input from a keyboard or joystick while the battle continues. R can process keyboard input and mouse clicks on a graph, but the game waits for input. This makes it impossible to have moving elements on a screen.
The other impediment in R to write arcade games is the lack of support for sound. The beepr package by Rasmus Bååth plays a sound after a process has completed. The
beep function in this package can play eleven different sounds. However, R can only do one thing at a time, so paying a sound stops any other gameplay.
The Gravedigger game from the 1980s book Creepy Computer Games is a pseudo arcade game that was initially developed for the venerable ZX81, my very first computer. In this game, the antagonist only moves once after the player moved. It is thus as much a strategy game as it is an action game.
MattSen has interpreted of the classic ZX81 game 3D Monster Maze in the R language. His version of this game is a cross between an action and an adventure game.
Although R is unsuitable for arcade games, I have managed to simulate the classic game of Pong. This code simulates a player that follows the path of the ball. Since I wrote this rudimentary Pong version, two people have developed exciting functionality to make arcade games possible. Chase Clarke expanded the Pong code and managed to create a rudimentary interactive version in Shiny.
Carsten Croonenbroek has developed the classic Snake game. This package uses tcltk to obtain user interaction via callback functions (almost as flexible as a real system-level message pump). Perhaps we can generalise the code in this package to create a joystick function in other games.
Stephen Kozak wrote a library that uses a Windows executable to constantly check for keyboard input. The KeyboardCheckR provides the
QueryKeyboard() function. This package is not on CRAN, and you will need to use devtools to install it.
I, unfortunately, have not yet had time to build a new game with these new developments.
Developing software to play strategic games has been an objective of artificial intelligence research from the early days of computing. Deep Blue defeating Gary Kasparov in 1997 was one of the significant milestones in the history of computing. More recently, Google’s AlphaGo beat the European Go champion, Fan Hui. Deep Blue and AlphaGo used quite different approaches. Whereas Deep Blue used traditional brute force, AlphaGo uses deep learning. Strategic games are ideal for the R language because of its specialised functionalities in statistical computing and machine learning.
The rchess package provides some functionality to play chess, but it is not a fully playable game. This package can plot beautiful chess boards, move pieces and detect check. However, this package is not able to generate moves by the computer and does not include a playable interface.
Tic Tac Toe, or Noughts and Crosses is one of the most accessible strategic board games. The minimax algorithm can easily play a perfect game, which means that the computer can never beat itself. The Tic Tac Toe game in R uses the locator function to manage the human player. This function reads the position of the graphics cursor when the user presses the left mouse button.
A text adventure is a form of interactive fiction where a player can freely explore a virtual world described in prose. These games were trendy in the 1980s. The basic principle is that the computer shows a text that describes the current situation. The player enters a text command, usually a simple verb-noun combination. The computer processes the command and displays the new status. This interchange continues until the game reaches a defined state. Either the player dies, or the player has achieved the game’s objectives.
Most games are set in typical Dungeons and Dragons settings with wizards, dragons and other usual characters. The lines below are a short extract from Colossal Cave, by Will Crowther and Don Woods, the very first text adventure written in the mid-1970s:
You are standing at the end of a road before a small brick building. Around you is a forest. A small stream flows out of the building and down a gully. > go south You are in a valley in the forest beside a stream tumbling along a rocky bed. > enter stream Your feet are now wet.
The BetaBit package by Smart Poland provides an educational console game where the user has to crack codes. This game comes close to the look-and-feel of traditional text adventures but is not quite the same.
This website has two adventure games. The Tenliner Cave Adventure is a programming challenge to cram as much functionality as possible into ten lines of BASIC code. This version uses the minimal amount of R code to recreate this masterpiece of programming.
My Secret of Landusia game provides a fully-functioning set of functions to write classic adventure games. The code can be easily expanded or modified to create new stories.
Games in R: Having fun with the R Language
R is not an optimal language for writing computer entertainment. Multipurpose languages such as Python or Java are much more suited to meet the expectations of gamers.
The many limitations don’t hold me back from writing computer games in the R language because working with limitations create a significant challenge. The Devil is in the Data irregularly publishes code to have fun with R. Subscribe to the blog to receive updates or follow the Rgames repository on Github.
If you know of any other games written in R, then please contact me. I would love to create a complete compendium of how to have fun in this quirky computing language.