Title: | Tools for Making and Spoiling Sudoku Games |
---|---|
Description: | Tools for making, retrieving, displaying and solving sudoku games. This package is an alternative to the earlier sudoku-solver package, 'sudoku'. The present package uses a slightly different algorithm, has a simpler coding and presents a few more sugar tools, such as plot and print methods. Solved sudoku games are of some interest in Experimental Design as examples of Latin Square designs with additional balance constraints. |
Authors: | Bill Venables <[email protected]> |
Maintainer: | Bill Venables <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.2-1 |
Built: | 2024-11-08 03:26:55 UTC |
Source: | https://github.com/cran/sudokuAlt |
Construct a Sudoku Game Object
as.sudoku(x, ...)
as.sudoku(x, ...)
x |
an n^2 x n^2 matrix object to represent the game |
... |
Other additional arguments (currently ignored) |
Coerce an object to one that can be used as a sudoku game. IMPORTANT: games are represented as n^2xn^2 character matrices, using 1-9 for n=2 or 3, and LETTERS[1:(n^2)] for n = 4 or 5.
An object of class 'sudoku'
Bill Venables
M <- as.sudoku(matrix("", 16, 16)) M[1:4, 1:4] <- matrix(LETTERS[1:16], 4, 4, byrow = TRUE) sM <- solve(M) plot(sM)
M <- as.sudoku(matrix("", 16, 16)) M[1:4, 1:4] <- matrix(LETTERS[1:16], 4, 4, byrow = TRUE) sM <- solve(M) plot(sM)
Construct a Sudoku Game Object
## S3 method for class 'matrix' as.sudoku(x, ...)
## S3 method for class 'matrix' as.sudoku(x, ...)
x |
An n^2 x n^2 matrix |
... |
other arguments (currently ignored) |
Coerce a matrix to an object that can be used as a sudoku game
An object of class 'sudoku'
Bill Venables
Identity function for sudoku objects
## S3 method for class 'sudoku' as.sudoku(x, ...)
## S3 method for class 'sudoku' as.sudoku(x, ...)
x |
A sudoku object |
... |
other arguments (ignored) |
the input sudoku object
Bill Venables
Format a Date Relative to the Current Date
daysAgo(n = 0, warn = TRUE)
daysAgo(n = 0, warn = TRUE)
n |
A positive integer for how many days ago |
warn |
Issue a warning if n <= 0 or n > 30 |
Internal function used by fetchUKGame().
A character string of the form "dd/mm/yy"
Bill Venables
daysAgo() ## today daysAgo(7) ## a week ago
daysAgo() ## today daysAgo(7) ## a week ago
Take a sudoku game and represent the information as a data frame giving the row, column, square and symbol for each entry. This is a useful form if the (complete) game is to be used as an experimental design
designGame(g, ...) ## Default S3 method: designGame(g, ...) ## S3 method for class 'sudoku' designGame(g, ...)
designGame(g, ...) ## Default S3 method: designGame(g, ...) ## S3 method for class 'sudoku' designGame(g, ...)
g |
a sudoku game, presumably solved |
... |
currently ignored |
a data frame with four colums, Row
, Col
,
Square
and Symbol
set.seed(2019) d <- seedGame(4) %>% solve() %>% regulariseGame(target = "b") %>% designGame() rbind(head(d), tail(d))
set.seed(2019) d <- seedGame(4) %>% solve() %>% regulariseGame(target = "b") %>% designGame() rbind(head(d), tail(d))
Construct a Vacant Game Structure
emptyGame(n = 3)
emptyGame(n = 3)
n |
an integer value between 2 and 5 inclusive. |
Returns a vacant game structure to allow special patterns to be constructed, as shown in the example.
An empty sudoku game structure
Bill Venables
g <- emptyGame(4) diag(g) <- LETTERS[1:16] plot(g) g %>% solve %>% plot -> sg ## %>% imported from magrittr
g <- emptyGame(4) diag(g) <- LETTERS[1:16] plot(g) g %>% solve %>% plot -> sg ## %>% imported from magrittr
Retrieve a Sudoku Game
fetchAUGame(day = 0, difficulty = c("tough", "hard", "medium", "easy"))
fetchAUGame(day = 0, difficulty = c("tough", "hard", "medium", "easy"))
day |
non-negative integer, how many days ago? zero for today's game. |
difficulty |
character string, how hard would you like it? |
Connects to http://www.sudoku.com.au and retrieves
the sudoku game from day
days ago. Based on a function from a
related sudoku package, sudoku::fetchSudokuUK
with minor changes.
The published sudoku game as a sudoku object.
Bill Venables
## Not run: fetchAUGame() %>% solve %>% plot -> gau ## The 'tough' game for today fetchAUGame(3, "easy") %>% solve %>% plot -> eau ## 'easy' game from 3 days ago ## End(Not run)
## Not run: fetchAUGame() %>% solve %>% plot -> gau ## The 'tough' game for today fetchAUGame(3, "easy") %>% solve %>% plot -> eau ## 'easy' game from 3 days ago ## End(Not run)
Retrieve a Sudoku Game
fetchUKGame(day = NULL)
fetchUKGame(day = NULL)
day |
positive integer < 30, how many days ago? or NULL for the most recently published game. |
Connects to http://www.sudoku.org.uk/DailySudoku.asp
and retrieves
the sudoku game from day
days ago. Based on a function from a
related sudoku package, sudoku::fetchSudokuUK
with minor changes.
The published sudoku game as a sudoku object.
Bill Venables
## Not run: (g0 <- fetchUKGame()) ## The game for today (according to GMT) (g3 <- fetchUKGame(3)) ## game from 3 days ago (according to GMT) if(require(sudoku)) { ## the original solver g0a <- as.sudoku(fetchSudokuUK()) identical(g0, g0a) ## should be TRUE } g0 %>% solve %>% plot -> sg0 ## spoil the game ## End(Not run)
## Not run: (g0 <- fetchUKGame()) ## The game for today (according to GMT) (g3 <- fetchUKGame(3)) ## game from 3 days ago (according to GMT) if(require(sudoku)) { ## the original solver g0a <- as.sudoku(fetchSudokuUK()) identical(g0, g0a) ## should be TRUE } g0 %>% solve %>% plot -> sg0 ## spoil the game ## End(Not run)
Construct a Random Sudoku Game
makeGame(n = 3, gaps = ceiling(3 * n^4/4), maxit = 25)
makeGame(n = 3, gaps = ceiling(3 * n^4/4), maxit = 25)
n |
Size of the game, n^2 x n^2 |
gaps |
Number of holes to leave for the solution |
maxit |
Number of tries before giving up. |
Construcs a sudoku game for given n, 2 <= n <= 5. n = 5 can be problematical.
a sudoku game
Bill Venables
set.seed(54321) makeGame() %>% solve %>% plot -> sg originalGame(sg)
set.seed(54321) makeGame() %>% solve %>% plot -> sg originalGame(sg)
Retrieve the Original from a Solved Game
originalGame(x)
originalGame(x)
x |
a sudoku object |
Convenience function for accessing an original from a solved game. If the game is unsolved, the object itself is returned.
The original sudoku game corresponding to the solution, or object itself if the game is unsolved
Bill Venables
set.seed(666) seedGame() %>% solve %>% plot -> sg ## %>% imported from magrittr originalGame(sg)
set.seed(666) seedGame() %>% solve %>% plot -> sg ## %>% imported from magrittr originalGame(sg)
Plot a Sudoku Game
## S3 method for class 'sudoku' plot( x, ..., cex = 1.5 - (n - 3)/2, colSolution = "grey", colGame = "fire brick" )
## S3 method for class 'sudoku' plot( x, ..., cex = 1.5 - (n - 3)/2, colSolution = "grey", colGame = "fire brick" )
x |
The sudoku game |
... |
additional arguments |
cex |
Character expansion factor |
colSolution |
colour to be used for the solution (if present) |
colGame |
colour to be used for the original game |
Present a graphical display of a sudoku game and its solution if the game is solved
The sudoku game x
, invisibly.
Bill Venables
set.seed(20191) makeGame(4, gaps = 0) %>% plot(cex=1) -> sg
set.seed(20191) makeGame(4, gaps = 0) %>% plot(cex=1) -> sg
Print a Sudoku Object
## S3 method for class 'sudoku' print(x, ...)
## S3 method for class 'sudoku' print(x, ...)
x |
The sudoku game object |
... |
extra arguments (ignored) |
Prints a sudoku object in an easily recognisable form.
the object, invisibly
Bill Venables
Put a solved sudoku game into a canonical form
regulariseGame(g, ...) ## S3 method for class 'sudoku' regulariseGame(g, target = c("block", "col", "row"), ...) ## Default S3 method: regulariseGame(g, ...)
regulariseGame(g, ...) ## S3 method for class 'sudoku' regulariseGame(g, target = c("block", "col", "row"), ...) ## Default S3 method: regulariseGame(g, ...)
g |
a solved sudoku game |
... |
additional arguments to methods (currently not used) |
target |
character; which section do you want to be in sorted order? |
If a solved sudoku game is to be used as an experimental design it is sometimes useful to re-arrange the symbols so that either the first row, first column or top left block symbols are in sorted order. This function accomplishes this task.
a regularised solved sudoku game
set.seed(1234) g <- makeGame() %>% solve() %>% regulariseGame(target = "b") %>% plot() plot(originalGame(g))
set.seed(1234) g <- makeGame() %>% solve() %>% regulariseGame(target = "b") %>% plot() plot(originalGame(g))
Generate a random sudoku game starting point
seedGame(n = 3)
seedGame(n = 3)
n |
Size of the game, n^2 x n^2 |
Generates a game with one instance of each symbol in random positions.
A sparse unsolved sudoku game
Bill Venables
set.seed(2345) g <- seedGame(3) sg <- solve(g) ## a completed random game plot(sg)
set.seed(2345) g <- seedGame(3) sg <- solve(g) ## a completed random game plot(sg)
Solve a Sudoku Puzzle
## S3 method for class 'sudoku' solve(a, ...)
## S3 method for class 'sudoku' solve(a, ...)
a |
A sudoku game object to be solved |
... |
Extra arguments (curently ignored) |
An alternative front end to solveGame
as a method for the base generic function solve
.
a solved game, or NULL if no solution exists.
Bill Venables
set.seed(1234) makeGame(3, gaps = 59) %>% solve %>% plot -> sg originalGame(sg) g <- emptyGame(4) # construct a patterned game diag(g) <- LETTERS[1:16] g %>% solve %>% plot -> sg sg
set.seed(1234) makeGame(3, gaps = 59) %>% solve %>% plot -> sg originalGame(sg) g <- emptyGame(4) # construct a patterned game diag(g) <- LETTERS[1:16] g %>% solve %>% plot -> sg sg
Solve a Sudoku Game
solveGame(game)
solveGame(game)
game |
The game to be solved |
Given a sudoku game to be solved, find the solution. IMPORTANT: games are represented as n^2 x n^2 character matrices, using 1-9 for n=2 or 3, and LETTERS[1:(n^2)] for n = 4 or 5.
A solved sudoku game object if one found, or NULL if no solution exists. The original game is attached as an attribute if the game is solved.
Bill Venables
set.seed(1234) makeGame(3, gaps = 60) %>% solve %>% plot -> sg (g <- originalGame(sg)) g <- emptyGame(4) # construct a patterned game diag(g) <- LETTERS[1:16] sg <- solve(g) plot(sg)
set.seed(1234) makeGame(3, gaps = 60) %>% solve %>% plot -> sg (g <- originalGame(sg)) g <- emptyGame(4) # construct a patterned game diag(g) <- LETTERS[1:16] sg <- solve(g) plot(sg)