How to generate, play and solve Sudoku puzzles in R

Tumuhimbise Moses
5 min readMar 3, 2020

--

As a non-avid but yet interested player of Sudoku and a lover of the R language, I have always longed for its gaming capabilities. My desire and one evening led me to stumble upon a Sudoku puzzle generator and solver for R. Besides installing the package itself, one of the prerequisites to begin tinkering with it is for one to install the tkrplot package. So basically, the main capabilities of this package are outlined in eight topics;

· Fetching Sudoku puzzles from the leading online Sudoku portal

· Generating Sudoku puzzles

· Utilizing hints to solve Sudoku puzzles

· Playing Sudoku puzzles in R

· Printing Sudoku puzzles

· Reading Sudoku puzzle files

· Solving Sudoku puzzles

· Writing Sudoku puzzle files

Fetching Sudoku puzzles from the leading online Sudoku portal

This function allows one to fetch daily puzzles from Sudoku.org.uk with the alternative of choosing a puzzle from its archives.

The grid created is based on a 9x9 arrangement that is suited for one to play.

Utilizing hints to solve Sudoku puzzles

To use this function, one can either press the h key while holding/hovering the mouse above the desired grid. In this case, I placed the mouse over the second cell of the second row in the second column. To this, I was provided with hints as to which value is likely most suited for this cell.

It is important to note that these are hints and not accurate solutions. For instance, I don’t think 5 is the correct value to be placed in this cell. It still remains upon the proficiency of the user/player to choose the most accurate value. I choose 2 (simply by moving the mouse over the desired cell and pressing 2 on the keyboard).

Playing Sudoku puzzles in R

To interactively play a puzzle, one could either fetch one or generate one. In the previous section, we interactively played a puzzle. In the next section, we shall generate a puzzle.

We can generate our own grid and fill in values accordingly with the following script.

some_random_puzzle <- playSudoku(0)now_let_us_play <- playSudoku(some_random_puzzle)

To fill in the cells, one has to move the mouse over every individual box and press a value.

Alternatively, one can use the following script to generate an editor which can be used to also create a puzzle.

another_of_ours <- edit(matrix(0,9,9))solve_it <- playSudoku(another_of_ours)

One can even play a puzzle, randomly generated by the computer/package by using this simple statement,

which will create a random puzzle.

Printing Sudoku puzzles

After generating a puzzle, one can print it to the terminal using the following one-liner script

Reading Sudoku puzzle files

The following function allows us to read a file comprising of a Sudoku grid. This is how a file looks like when typed manually in notepad.

Our file adheres to the 9x9 matrix. To read it, we run the following script. First, make sure you have your working directory set right to avoid “no file errors”.

After running the script, you will be presented with a series of values from 1 to 15. This represents the number of cells within the grid. Input these values wisely to allow for the creation of a sensible puzzle.

I didn’t heed to my own advice and created this 2-year-old child’s idea of a puzzle.

Solving Sudoku puzzles

Solving a puzzle is based on a complex algorithm that utilizes the NxNxN array of logicals which are representative of the NxN cells and the N likely elements. Let us trying solving the SudokuUK puzzle we fetched earlier.

For a more clear representation,

Writing Sudoku puzzle files

We can also write a generated or fetched Sudoku grid (matrix) to a text file. Let us write the SudokuUK puzzle we fetched earlier to a text file.

Now we have a Sudoku puzzle matrix written to file.

That’s it. Simple to use package for the puzzle lovers. Maybe in the next post, I will share more of R’s gaming capabilities.

--

--