Scripts/SudokuByHand
< Scripts
Jump to navigation
Jump to search
Download this script: File:Sudokubyhand0.ijs
Roger Hui's sample grid and essay can be viewed at Essays/Sudoku.
The start of your J session might look like the following for Roger Hui's sample grid.
load 'sudokubyhand0.ijs' output =: tryHard grid +-------------------+-------------------+-------------------+ |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| ||t w o|1 | 3|||s i x|seven|1 ||| | 3| 3|| ||t w o| 5 | 5 |||s i x|seven|4 5 |||4 5 |4 | 5 || ||t w o| 8 9| 9|||s i x|seven| 8 ||| 9| 9| 9|| |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| || 3| |s i x||| 3| 3| |||t w o| 3|o n e|| || | 5 |s i x|||4 5 |4 |4 5 |||t w o|4 |o n e|| || 8 9|7 8 9|s i x||| 9| 8 9| 8 |||t w o|7 9|o n e|| |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| ||four |1 | 3|||1 2 3|1 2 3|1 2 |||eight| 3| 3|| ||four | 5 | 5 ||| 5 | | 5 |||eight| 6| 5 6|| ||four |7 9|7 9||| 9| 9| |||eight|7 9| 9|| |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| +-------------------+-------------------+-------------------+ |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| ||five |1 | |||1 2 |1 2 |nine |||three|1 2 | 2 || ||five |4 6|4 |||4 |4 6|nine |||three|4 6| 6|| ||five |7 8 |7 |||7 | |nine |||three| 8 | || |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| ||1 |three| |||1 2 |1 2 |1 2 |||1 |five | 2 || || 6|three|4 |||4 |4 6|4 6|||4 |five | 6|| || 8 9|three|7 9|||7 | |7 ||| 9|five | 9|| |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| ||1 |1 |t w o|||eight|1 3|1 |||1 |1 |seven|| || 6|4 6|t w o|||eight|4 6|4 5 6|||4 |4 6|seven|| || 9| 9|t w o|||eight| | ||| 9| 9|seven|| |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| +-------------------+-------------------+-------------------+ |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| || 3| 2 |o n e||| 2 | 2 | 2 ||| | 2 3|four || || 6| 5 6|o n e||| | 6| 6||| 5 | |four || || 9| 9|o n e|||7 9| 8 9|7 8 |||7 9|7 9|four || |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| ||seven| 2 |eight|||1 2 |1 2 |1 2 |||s i x|1 2 3| 2 3|| ||seven|4 5 |eight|||4 |4 |4 |||s i x| | 5 || ||seven| 9|eight||| 9| 9| |||s i x| 9| 9|| |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| || | 2 | |||1 2 |five |three|||1 |1 2 |eight|| || 6|4 6|4 |||4 |five |three||| | |eight|| || 9| 9| 9|||7 9|five |three|||7 9|7 9|eight|| |+-----+-----+-----+|+-----+-----+-----+|+-----+-----+-----+| +-------------------+-------------------+-------------------+ please make your entry 713
Comments and the verb tryHard in the script are shown below.
NB. sudodubyhand0.ijs NB. Brian Schott NB. 12/1/5 NB. This script helps me to not make basic errors in sudoku NB. by checking for blatantly impossible inputs and by NB. checking that inputs are uniquely correct given the NB. current state of the hand-solved puzzle. (When the NB. verbs are run, non-unique inputs are allowed if NB. they are otherwise valid, but only after the user NB. confirms that (s)he wishes to supply the input NB. value.) NB. The main verb below is named tryHard. However to NB. get familiar with the system it might be easier NB. to start with the verb named entries, which NB. is embedded in the verb tryHard, along with the NB. verb removals. NB. This is a non-GUI version developed with J v5.04. A GUI NB. version may be developed with J v6. NB. I have found that this system works best if the window NB. is sized so that a full grid and the user response NB. just fits on one screen. NB. Henry Rich's t22 example which requires guessing grid =: ];._2 noun define 000200700 570000080 003007040 047310020 300000009 010046350 090600800 020000071 008004000 ) NB. Roger's example grid =: ];._2 noun define 200670000 006000201 400000800 500009300 030000050 002800007 001000004 708000600 000053008 ) NB.* entries v Checks user inputs to a Sudoku grid. NB. form: output =: entries grid NB. newoutput =: output entries grid NB. The second form is useful when a devious sudoku NB. requires guess(es) to solve. An incorrect guess NB. leads to backtracking, so before a guess is made NB. you can save your current results as output and NB. then create a new output in case another guess NB. is required. And so on. NB. This script helps to not make basic errors in sudoku NB. by checking for blatantly impossible entries and by NB. checking that entries are uniquely correct given the NB. current state of the hand-solved puzzle. NB. Nonunique entries can be made if the user insists. NB. The right argument is a sudoku 9 by 9 text array of NB. digits. NB. There may not be a left argument, but if there is none, NB. i. 0 3 is automatically supplied. If there is a left NB. argument, it must be all text atoms and either NB. a) a rank-2 array of shape (n,3) of valid entry-triples, NB. or, NB. b) a pair of boxed rank-2 arrays of shape (n,3) and NB. (m,3), where the first array is comprised of valid NB. removal-triples, and the second array is comprised NB. of valid entry-triples. NB. The arrays must be valid because their validity is NB. not checked. Normally when left hand arrays are NB. supplied, they are the output of a previous run NB. of one of the supplied verbs which was saved into NB. a variable for the purpose of passing the output NB. along. NB. Alternatives a) and b) above have been expanded to allow NB. rank-2 arrays of 3 or more columns so that NB. additional information about each entry can be NB. appended. In the current implementation, info about NB. the session time of each entry/removal is appended. NB. To force this verb to stop execution while it is in its NB. endless while. loop, supply an empty input: just NB. strike the return key. If you have saved the output NB. of the verb into a named noun, then that noun -- or NB. any of its valid rows -- can be supplied later as NB. the left argument when this verb or other verbs NB. here are subsequently run. NB. examples: NB. $S =: entries grid NB. monadic NB. and later, especially if guesses are required NB. $S1 =: S entries grid NB. dyadic NB. Within the verb 'entries' an example "entry" is three NB. digits such as 325 , which means place a 5 in the cell NB. in row 3 and column 2 (origin 1). NB. An empty entry signals the user wishes to quit. NB.* removals v Checks user removals from a Sudoku grid. NB. form: output =: removals grid NB. newoutput =: output removals grid NB. This script helps to not make basic errors in sudoku NB. by disallowing blatantly impossible removals of NB. possible digits from cells. NB. The input options for this verb are the same as for NB. the verb entries, as is the stopping rule. NB. While you may use the verb removals directly, likely NB. you will prefer to use the verb tryHard instead, NB. after you have a little experience. NB.* tryHard v Automates alternating use of the verbs NB. entries and removals. NB. form: output =: tryHard grid NB. newoutput =: output tryHard grid NB. The verb tryHard simplifies repeated running of the two NB. verbs entries and removals, alternatively. To force NB. the switch from one program to the other, supply NB. an empty response (return key) instead of an NB. entry or removal in one of the sub-verbs. NB. The input options for this verb are the same as for NB. the verb entries, as are the output options. tryHard =: verb define (i. 0 3) tryHard y : toggle =. 1 results =. x. while. 1 do. if. toggle do. temp =. results entries y else. temp =. results removals y end. if. L. temp do. if. #>{.temp do. results =. temp else. results =. >{:temp end. else. results =. temp end. still =. ui2 'Exit this program now? (y or [n])' if. still -: ,'y' do. results break. end. toggle =. -. toggle end. )
Contributed by Brian Schott
[[Category:Games R.2.1.10]