Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                doctype html
html(ng-app="sudoku")
  head
  body
    .sudoku(ng-controller="sudokuController as su" pos="0" ng-cloak)
      .box(ng-repeat="i in su.nine")
        .cell(ng-repeat="j in su.nine" contenteditable ng-model="su.array[su.boxmap[i*9+j]]")
      button(ng-click="su.go()") answer
      button(ng-click="su.reset()") reset

    .sudoku(ng-controller="sudokuController as su" pos="1" ng-cloak)
      .box(ng-repeat="i in su.nine")
        .cell(ng-repeat="j in su.nine" contenteditable ng-model="su.array[su.boxmap[i*9+j]]")
      button(ng-click="su.go()") answer
      button(ng-click="su.reset()") reset

    .sudoku(ng-controller="sudokuController as su" pos="3" ng-cloak)
      .box(ng-repeat="i in su.nine")
        .cell(ng-repeat="j in su.nine" contenteditable ng-model="su.array[su.boxmap[i*9+j]]")
      button(ng-click="su.go()") answer
      button(ng-click="su.reset()") reset
              
            
!

CSS

              
                $len: 25px
$llen: ($len + 2) * 3
$lllen: ($llen + 2) * 3
div
  border: black solid 1px
  font-size: 18px
  text-align: center
  float: left
.sudoku
  width: $lllen
  height: $lllen
  margin-right: 10px
.box
  width: $llen
  height: $llen
  .cell
    width: $len
    height: $len

.ng-valid.ng-dirty
  color: green
.ng-invalid.ng-dirty
  color: red
              
            
!

JS

              
                sudokus = [
  [
    8, '', '', '', '', '', '', '', ''
    '', '', 3, 6, '', '', '', '', ''
    '', 7, '', '', 9, '', 2, '', ''
    '', 5, '', '', '', 7, '', '', ''
    '', '', '', '', 4, 5, 7, '', ''
    '', '', '', 1, '', '', '', 3, ''
    '', '', 1, '', '', '', '', 6, 8
    '', '', 8, 5, '', '', '', 1, ''
    '', 9, '', '', '', '', 4, '', ''
  ]
  [
    '', '', '', 7, '', '', '', '', 2
    '', 3, '', '', '', 8, '', '', ''
    4, '', '', 5, '', '', 3, '', ''
    '', '', 7, 2, '', '', '', '', 1
    '', 1, '', '', '', '', '', 6, ''
    8, '', '', '', '', 7, 4, '', ''
    '', '', 1, '', '', 9, '', '', 5
    '', '', '', 6, '', '', '', 2, ''
    9, '', '', '', '', 3, '', '', ''
  ]
  [
    '', 9, 7, 4, '', '', '', '', ''
    4, '', '', '', '', 7, '', 3, ''
    3, '', '', '', '', '', '', '', ''
    8, '', '', 5, '', 9, '', 1, ''
    '', '', '', '', '', '', '', '', ''
    '', 7, '', 1, '', 6, '', '', 8
    '', '', 2, '', '', '', '', '', 6
    '', 1, '', 6, '', '', '', '', 2
    '', '', '', '', '', 5, 3, 8, ''
  ]
  [
    6, '', '', 5, '', '', 3, '', 1
    '', 9, '', '', '', '', '', '', ''
    '', '', 7, '', '', 8, 9, '', 4
    8, '', '', '', '', 1, 5, '', ''
    '', '', '', '', '', '', '', '', ''
    '', '', 4, 9, '', '', '', '', 2
    3, '', 5, 1, '', '', 6, '', ''
    '', '', '', '', '', '', '', 1, ''
    2, '', 1, '', '', 4, '', '', 7
  ]
]
app = angular.module 'sudoku', []
app.directive 'contenteditable', ->
  restrict: 'A'
  require: '?ngModel'
  link: (scope, ele, attrs, ngmodel) ->
    b = scope.$parent.i
    j = scope.j % 3 + b % 3 * 3
    i = scope.j // 3 + b // 3 * 3
    array = scope.$parent.$parent.su.array
    boxmap = scope.$parent.$parent.su.boxmap
    ngmodel.$render = ->
      ele.text ngmodel.$viewValue || ''
      ngmodel.$setValidity '', true
      ngmodel.$setViewValue ngmodel.$viewValue if not ngmodel.$viewValue

    ngmodel.$parsers.push (v) ->
      ngmodel.$setValidity '',
        v is '' or
        /^[1-9]$/.test(v) and
        not(1 for x in array[i*9..i*9+8] when x is v).length and
        not(1 for x in array[j..80] by 9 when x is v).length and
        not(1 for x in boxmap[b*9..b*9+8] when array[x] is v).length
      if ngmodel.$valid then v else NaN
    ele.on 'keyup change', -> ngmodel.$setViewValue parseInt ele.text()
    null

app.controller 'sudokuController', ($attrs) ->
  @array = sudokus[$attrs.pos][..]

  @boxmap = [
    0, 1, 2, 9, 10, 11, 18, 19, 20
    3, 4, 5, 12, 13, 14, 21, 22, 23
    6, 7, 8, 15, 16, 17, 24, 25, 26
    27, 28, 29, 36, 37, 38, 45, 46, 47
    30, 31, 32, 39, 40, 41, 48, 49, 50
    33, 34, 35, 42, 43, 44, 51, 52, 53
    54, 55, 56, 63, 64, 65, 72, 73, 74
    57, 58, 59, 66, 67, 68, 75, 76, 77
    60, 61, 62, 69, 70, 71, 78, 79, 80
  ]

  @getbox = (index) -> (@array[i] for i in @boxmap[index*9..index*9+8])

  @nine = [0..8]
  
  @reset = =>
    @array[i] = sudokus[$attrs.pos][i] for i in [0..80]
    null
  
  @go = ->
    @array[i] = '' for i in [0..80] when isNaN @array[i]
    @brute @next 0
    console.log @array
    
  @brute = (pos) ->
    if pos > 80
      return null
    origin = @array.slice 0
    if @hasslot()
      nominees = @available pos
      if nominees is []
        @array = origin
      else
        for nominee in nominees
          if @hasslot()
            @array = origin
            @array[pos] = nominee
            # await new Promise((r) -> setTimeout(r, 1000))
            @brute @next pos + 1
          else return
    
  @next = (pos) ->
    if @array[pos] is '' or pos > 80 then pos else @next pos + 1
    
  @available = (pos) ->
    x = pos % 9
    y = pos // 9
    row = (i for i in @array[y*9..y*9+8])
    col = (@array[i] for i in [x..80] by 9)
    box = @getbox x // 3 + y // 3 * 3
    _.difference [1..9], _.union(row, col, box)
    
  @hasslot = ->
    '' in @array
  null
              
            
!
999px

Console