knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
In this vignette, you'll learn the basic usage of msweepeR
, a text based minesweeper game for the R programming language.
To start a new msweepeR
game, you first need to assign a new msweepeR
class object:
#Load msweepeR package library(msweepeR) #Create a new msweepeR game new_game <- msweepeR()
By default, the msweepeR()
constructor function creates a 10x10 board with 10 mines. You can change the board size by modifying the w
and h
parameters. To change the number of mines in the game board, you can either modify the level
parameter or the mines
parameter. The level
parameter changes the level of difficulty in the game. Currently, there are 3 levels of difficulty (1, 2, 3). Each difficulty level, sets the number of mines to a fixed proportion of the total tiles in the board (0.10, 0.15, and 0.20 respectively). The m
parameter manually sets the number of mines in the board.
There are a few restrictions that you must keep in mind when starting a new game and defining the parameters. First, the board width must be narrower that the total console width. Each tile is 3 characters long, therefore the total board width is w*3 + nchar(h) + 1
. The second restriction to keep in mind is that the total number of mines can't be higher than half of the total number of tiles (doing so would make the game much easier to play). Finally, the third restriction is that level
and mines
parameters can't be defined at the same time: you must set one of them to NULL
(by default, mines
is set to NULL
and level
to 1
).
#Create a new msweepeR game in the highest difficulty level new_game <- msweepeR(level = 3) #Create a new msweepeR game in a 15x15 board new_game <- msweepeR(w = 15, h = 15) #Try to create a new msweepeR game in a 15x15 with 100 mines #Notice the error caused by defining both mines and level parameters (remember that the level parameter is set by default to 1) new_game <- msweepeR(w = 15, h = 15, mines = 100) #Create a new msweepeR game in a 15x15 with 100 mines #Notice the warning that triggers when more than 25% of tiles are mines new_game <- msweepeR(w = 15, h = 15, mines = 100, level = NULL)
After creating a msweepeR
game, you have to update the object by calling the play.msweepeR()
method.
#First, create a msweepeR game new_game <- msweepeR() #Then, update by calling the play.msweepeR() method to play the game new_game <- play.msweepeR(new_game)
When called, the play.msweepeR()
method will print in the console some basic info about the game session and a text based representation of the game board, like the one showed below. Unopened tiles will be represented by a dot (.) character. Each tile has a [row, col]
coordinate, like any other matrix-like object in R. In the first row, and first column of the game board, you can see the row number and column number.
After printing the board, play.msweepeR()
asks you what's your next move. As in any other minesweeper game, you can open a tile or put a flag on it. In order to tell play.msweepeR()
what's your next move, you have to use a special command syntax:
action @ row, col
Where action
is either open
or flag
, and row, col
the tile coordinates. After typing your move, press return and the play.msweepeR()
method will parse and evaluate your command. As in any other minesweeper game, you'll loose if you open a mined tile, and you'll win if you flag all mined tiles. Once, a valid command is evaluated, the updated msweepeR
object will be printed to the console again, an you'll be asked to make another move.
Empty opened tiles will be represented with a blank space character or (like any other minesweeper puzzle) with a colored number that indicates the total number of mines in the 8 neighboring tiles.
To exit a msweepeR
game, you can press the escape key, or type the exit
command after the What would you like to do?
prompt.
To report a bug in this R package, you can open an issue at the package's GitHub page (https://github.com/pablorm296/msweepeR) or by sending a mail to preyes@colmex.mx
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.