Ready to have fun with functions, matrices, random sampling, and more? If so, get ready to simulate your own Boggle board in R! (For those of you who are unfamiliar with Boggle you can play it here: Wordshake Boggle)

In this post, I will guide you step-by-step, explaining the concepts as I go. Links are included at the end of each section with relevant content that goes much more in depth than I can here. As always, please comment below if you have any questions!

If you want to take a stab at this on your own before following along, here are two general guidelines:

  1. Write a function that returns a random Boggle board in a 4x4 matrix.
  2. Make sure that each time this function is run, the face and placement in the matrix of each die is randomized.

Creating 16 dice

As you know, to play Boggle you need 16, six-sided dice, where each side has a letter printed on it. To make a die in R, we are going to create a vector that contains six different letters:

die <- c("F", "Y", "N", "I", "S", "W")

Because we need 16 total, and the process to write all of them out might get tedious, feel free to copy and paste the code for all 16 dice below:

d1 = c('S','R','E','L','A','C')
d2 = c('D','P','A','C','E','M')
d3 = c('Qu','B','A','O','J','M')
d4 = c('D','U','T','O','K','N')
d5 = c('O','M','H ','R','S','A')
d6 = c('E','I','F','E','H','Y')
d7 = c('B','R','I','F','O','X')
d8 = c('R','L','U','W','I','G')
d9 = c('N','S','O','W','E','D')
d10 = c('Y ','L','I','B','A','T')
d11 = c('T','N','I','G','E','V')
d12 = c('T','A','C','I','T','O')
d13 = c('P','S','U','T','E','L')
d14 = c('E','P','I','S','H ','N')
d15 = c('Y','K','U','L','E','G')
d16 = c('N','Z','E','V','A','D')

For a review of vectors check out Data Mentor: R Vector.

Storing the dice in a matrix

In order to most efficiently store each die and be able to randomize their placement and face, we will place each in its own column of a matrix. As shown below, we can do this by passing a vector of all the dice into the matrix() function and then specify with “ncol” that we want 16 columns. As there are only 16 elements in the vector passed in, each element or dice will get its own column.

  dice <- matrix(c(d1, d2, d3, d4, d5, d6, d7, d8, 
                  d9, d10, d11, d12, d13, d14, d15, d16),
                ncol = 16)

If we call dice, we can see that we created a matrix where each column contains the elements or letters of that numbered die.

dice
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] "S"  "D"  "Qu" "D"  "O"  "E"  "B"  "R"  "N"  "Y "  "T"   "T"   "P"   "E"   "Y"   "N"  
[2,] "R"  "P"  "B"  "U"  "M"  "I"  "R"  "L"  "S"  "L"   "N"   "A"   "S"   "P"   "K"   "Z"  
[3,] "E"  "A"  "A"  "T"  "H " "F"  "I"  "U"  "O"  "I"   "I"   "C"   "U"   "I"   "U"   "E"  
[4,] "L"  "C"  "O"  "O"  "R"  "E"  "F"  "W"  "W"  "B"   "G"   "I"   "T"   "S"   "L"   "V"  
[5,] "A"  "E"  "J"  "K"  "S"  "H"  "O"  "I"  "E"  "A"   "E"   "T"   "E"   "H "  "E"   "A"  
[6,] "C"  "M"  "M"  "N"  "A"  "Y"  "X"  "G"  "D"  "T"   "V"   "O"   "L"   "N"   "G"   "D"  

For a review of matrices check out W3 Schools: R Matrices.

Rolling the dice

In Boggle, when the dice are rolled, two random events occur for each die. First, the die lands on a random face (roll). Second, the die lands in a random slot in the 4x4 matrix (placement).

Randomizing the roll

To randomize which face each die lands on, we will use the function apply() which applies a user-specified function to the marginal values of matrix (either the rows(1) or columns(2)). In this case, we are applying the function sample() to the 16 columns of the matrix dice. The sample() function tells R to randomly select a sample of size one (so one letter) in this case from the element specified - a die. Lastly, we will save this output as a vector called faces.

faces <- apply(dice, 2, sample, size = 1)

If we look at faces, you should see a randomly generated vector of letters, one from each die.

faces
[1] "S" "A" "O" "O" "A" "H" "X" "I" "W" "A" "V" "I" "L" "P" "K" "A"

Randomizing the placement

Now, the last task left is to randomly assign each face of the dice generated above to a location in our 4x4 Boggle board. To do this, we can once again use the sample() function, but because our sample size is 16 and the length of our vector faces is also 16, all this will do is randomize the order of its elements. Once the order is randomized, we can then store it in a matrix called board (our Boggle board!) which has four rows and four columns.

board <- matrix(sample(faces, size = 16), nrow = 4, ncol = 4)

Looking at the output of board, we see that indeed, the order of faces was randomized and we finally have the letters (one from each die) in a 4x4 Boggle board!

board
     [,1] [,2] [,3] [,4]
[1,] "A"  "I"  "O"  "W" 
[2,] "S"  "H"  "K"  "A" 
[3,] "V"  "O"  "A"  "A" 
[4,] "I"  "L"  "X"  "P" 

For a review of the function apply() check out R Coder: Apply in R.
For a review of the function sample() check out R Examples: Sample().

Finishing touches

Now that we have all the code to make our Boggle board, we are just about finished! The last coding step is to create a function called get_board() inside of which we will stick all our code up until this point. Your function should look like the one below:

get_board <- function(){
  d1 = c('S','R','E','L','A','C')
  d2 = c('D','P','A','C','E','M')
  d3 = c('Qu','B','A','O','J','M')
  d4 = c('D','U','T','O','K','N')
  d5 = c('O','M','H ','R','S','A')
  d6 = c('E','I','F','E','H','Y')
  d7 = c('B','R','I','F','O','X')
  d8 = c('R','L','U','W','I','G')
  d9 = c('N','S','O','W','E','D')
  d10 = c('Y ','L','I','B','A','T')
  d11 = c('T','N','I','G','E','V')
  d12 = c('T','A','C','I','T','O')
  d13 = c('P','S','U','T','E','L')
  d14 = c('E','P','I','S','H ','N')
  d15 = c('Y','K','U','L','E','G')
  d16 = c('N','Z','E','V','A','D')
  
  dice <- matrix(c(d1,d2,d3,d4,d5,d6,d7,d8,
                  d9,d10,d11,d12,d13,d14,d15,d16),
                ncol = 16) # putting each die into a separate column in the matrix
  
  faces <- apply(dice, 2, sample, size = 1) # 1st randomization (roll)
  
  board <- matrix(sample(faces, size = 16), nrow = 4, ncol = 4) # 2nd randomization (placement)

  return(board)
}

Now, when we call the function, we should get a randomly generated Boggle board like this!

get_board()
     [,1] [,2] [,3] [,4]
[1,] "Qu" "L"  "U"  "H "
[2,] "F"  "L"  "E"  "E" 
[3,] "S"  "F"  "E"  "V" 
[4,] "I"  "N"  "T"  "T" 

For a review of functions check out Tutorials Point: R - Functions.

Boggle time!

Congratulations! Good job sticking through ‘till the end … and making your own Boggle board! All that’s left to do is start a three-minute timer, call your function, and start playing! Oh, and be sure to show it off to your friends too!

Bonus challenges

If you feel like you understood everything and are up for a challenge, try to create a:

  • 5x5 Boggle board
  • Timer in R
  • Function that counts the number of vowels in your board
  • Function into which you can write your answers and get scored based on correctness and length

Please feel free to comment below with any questions, points, and suggestions for other blog posts!