Title: | Sudoku Puzzle Generator and Solver |
---|---|
Description: | Generates, plays, and solves Sudoku puzzles. The GUI playSudoku() needs package "tkrplot" if you are not on Windows. |
Authors: | David Brahm <[email protected]> and Greg Snow <[email protected]>, with contributions from Curt Seeliger <[email protected]> and Henrik Bengtsson <[email protected]>. |
Maintainer: | David Brahm <[email protected]> |
License: | GPL |
Version: | 2.8 |
Built: | 2024-11-07 06:33:25 UTC |
Source: | CRAN |
Fetches the daily sudoku puzzle from https://www.sudoku.org.uk/ or one of their archive from the previous 31 days.
fetchSudokuUK(day)
fetchSudokuUK(day)
day |
Optional character string specifying the day of the puzzle to download. This is in European date format 'dd/mm/yyyy' and needs to represent a date within the last 31 days. |
A 9x9 matrix representing a sudoku puzzle (blank squares have value 0).
See the website for copyright information. Don't submit your
solution for the prize contest if you used solveSudoku
or
playSudoku
with solve=TRUE
. This function requires a
working internet connection.
Greg Snow [email protected]
solveSudoku
, playSudoku
,
generateSudoku
## Not run: #todays puzzle puz <- fetchSudokuUK() # puzzle from 25 Jan 2006 (if still available) puza <- fetchSudokuUK('25/01/2006') playSudoku(puza) ## End(Not run)
## Not run: #todays puzzle puz <- fetchSudokuUK() # puzzle from 25 Jan 2006 (if still available) puza <- fetchSudokuUK('25/01/2006') playSudoku(puza) ## End(Not run)
Creates a 9x9 Sudoku grid suitable for use by
playSudoku
.
generateSudoku(Nblank=50, print.it=FALSE)
generateSudoku(Nblank=50, print.it=FALSE)
Nblank |
Number of cells to blank out |
print.it |
Logical. If true, print result to screen. |
The basic algorithm is to start with a 'primordial' Sudoku grid, swap around some rows and columns, then blank out some cells.
A matrix, representing a 9x9 Sudoku grid.
Curt Seeliger <[email protected]>, Henrik Bengtsson <[email protected]>, and David Brahm <[email protected]>
generateSudoku(print.it=TRUE)
generateSudoku(print.it=TRUE)
Generates a text string containing a 'hint' for cell (i,j) of Sudoku grid 'z'.
hintSudoku(z, i, j)
hintSudoku(z, i, j)
z |
A 9x9 numeric matrix |
i |
Row index |
j |
Column index |
A character string, suitable for cat
.
Greg Snow [email protected] and David E. Brahm <[email protected]>
Interactively play a game of 9x9 Sudoku with hints and undo
playSudoku(z=NULL, hist.len=100, solve=TRUE, display=c("guess","windows","tk"), hscale=1.25, vscale=1.25, ...)
playSudoku(z=NULL, hist.len=100, solve=TRUE, display=c("guess","windows","tk"), hscale=1.25, vscale=1.25, ...)
z |
Either a 9x9 numeric matrix representing the Sudoku grid
(with '0' representing a blank cell), or 0 (zero) for an empty
matrix, or a filename (passed to |
hist.len |
Integer representing the number of history steps to remember (number of undo's possible). |
solve |
Logical indicating if the solution should be computed (used for checking current answer or cheating). |
display |
Type of display. The default 'guess' uses a windows graphics device if getOption('device')=='windows', otherwise it uses tk (requiring the 'tkrplot' package). |
hscale |
Passed to tkrplot |
vscale |
Passed to tkrplot |
... |
Arguments passed to |
To play, move the mouse arrow over an empty cell and press the number key to enter the number in the cell. Typing '?' brings up a menu of additional commands:
? -- a short help message 1-9 -- insert digit 0,' ' -- clear cell r -- replot the puzzle q -- quit h -- hint/help c -- correct wrong entries (show in red) u -- undo last entry s -- show number in cell a -- show all (solve the puzzle)
An invisible matrix with the solution or current state of the puzzle. Save this if you stop part way through, and use it as the input for the function to start again where you left off (undo info is lost so make sure that everything is correct).
display='windows' makes use of the getGraphicsEvent
function, which currently only works on Windows.
Greg Snow [email protected] and David E. Brahm <[email protected]>
## Not run: puz1 <- playSudoku(0) # Use as an editor to create a puzzle, then quit sol1 <- playSudoku(puz1) # now play the puzzle puz2 <- edit(matrix(0,9,9)) # Or use this editor sol2 <- playSudoku(puz2) # now play the puzzle playSudoku() # Play a randomly generated game playSudoku(fetchSudokuUK()) # Play today's game ## End(Not run)
## Not run: puz1 <- playSudoku(0) # Use as an editor to create a puzzle, then quit sol1 <- playSudoku(puz1) # now play the puzzle puz2 <- edit(matrix(0,9,9)) # Or use this editor sol2 <- playSudoku(puz2) # now play the puzzle playSudoku() # Play a randomly generated game playSudoku(fetchSudokuUK()) # Play today's game ## End(Not run)
Prints a Sudoku grid (a 9x9 matrix) to the terminal.
printSudoku(z)
printSudoku(z)
z |
A 9x9 numeric matrix, with '0' representing a blank cell. |
None; used for side effect.
David E. Brahm <[email protected]>
Reads a file containing a Sudoku grid (a 9x9 matrix).
readSudoku(fn, map)
readSudoku(fn, map)
fn |
A filename. |
map |
Vector of unique puzzle elements (possibly longer than
necessary). The default is |
The input file should look like this:
-6-1-4-5- --83-56-- 2-------1 8--4-7--6 --6---3-- 7--9-1--4 5-------2 --72-69-- -4-5-8-7-
Blank cells can be indicated with any character not in "map", such as the '-' used here.
A numeric matrix (usually 9x9).
David E. Brahm <[email protected]>
z <- readSudoku(system.file("puz1.txt", package="sudoku"))
z <- readSudoku(system.file("puz1.txt", package="sudoku"))
Solves a Sudoku Puzzle.
solveSudoku(z, verbose=FALSE, map=c(1:9,letters), level=0, print.it=TRUE)
solveSudoku(z, verbose=FALSE, map=c(1:9,letters), level=0, print.it=TRUE)
z |
A filename (passed to |
verbose |
If TRUE, report on progress. |
map |
Vector of unique puzzle elements (possibly longer than
necessary). The default is |
level |
Recursion level (should not be set by user). |
print.it |
Logical: print the solution? |
A Sudoku puzzle consists of an NxN grid, where N is a perfect square (usually N=9). The grid is subdivided into N [sqrt(N) x sqrt(N)] boxes. You must fill in the missing values so that each row, each column, and each box contains the integers 1:N exactly once.
The algorithm uses an NxNxN array of logicals, representing the NxN cells and the N possible elements. For example, if a[1,2,3]=TRUE, then z[1,2] is known to be '3'. If a[1,2,4]=FALSE, then z[1,2] is known not to be '4'. The basic rules of Sudoku are used to fill in FALSE's, then elimination is used to find the TRUE's. If that approach runs out of steam, a guess is made and the program recurses to find either a solution or an inconsistency. No attempt is made to prove a solution's uniqueness.
Invisibly returns the solved (numerical) matrix, and prints the character version.
David E. Brahm <[email protected]>
Example "puz1" comes from https://sudoku.com/.
## Not run: solveSudoku(system.file("puz1.txt",package="sudoku"), verbose=TRUE) ## End(Not run)
## Not run: solveSudoku(system.file("puz1.txt",package="sudoku"), verbose=TRUE) ## End(Not run)
Writes a Sudoku grid (a matrix) to a file.
writeSudoku(z, fn)
writeSudoku(z, fn)
z |
A Sudoku grid. |
fn |
A filename. |
None; used for its side effect.
David E. Brahm <[email protected]>