View source with formatted comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2014, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(swish_render_sudoku,
   36	  [ term_rendering//3			% +Term, +Vars, +Options
   37	  ]).   38:- use_module(library(apply)).   39:- use_module(library(http/html_write)).   40:- use_module(library(http/term_html)).   41:- use_module('../render').   42
   43:- register_renderer(sudoku, "Render a sudoku matrix").   44
   45/** <module> SWISH Sudoku renderer
   46
   47Renders a term like below as a _sudoku matrix_
   48
   49  ==
   50  [[_,_,_,_,_,_,_,_,_],
   51   [_,_,_,_,_,3,_,8,5],
   52   [_,_,1,_,2,_,_,_,_],
   53   [_,_,_,5,_,7,_,_,_],
   54   [_,_,4,_,_,_,1,_,_],
   55   [_,9,_,_,_,_,_,_,_],
   56   [5,_,_,_,_,_,_,7,3],
   57   [_,_,2,_,1,_,_,_,_],
   58   [_,_,_,_,4,_,_,_,9]]
   59  ==
   60*/
   61
   62%%	term_rendering(+Term, +Vars, +Options)//
   63%
   64%	Renders Term as a sudoku matrix. Term must be a list of 9 lists,
   65%	each of which must have  9  elements   that  are  all either the
   66%	integer 1..9 or unbound.
   67
   68term_rendering(Term, _Vars, _Options) -->
   69	{ is_sudoku(Term)
   70	}, !,
   71	html(div([class(sudoku),
   72		  'data-render'('Sudoku matrix')
   73		 ],
   74		 [\rows(Term, 1), \sudoku_style])).
   75
   76sudoku_style -->
   77	html({|html||
   78	      <style>
   79div.sudoku { vertical-align: top;
   80	     display:inline-block;
   81	     border: 3px solid black;
   82	     width: 220px;
   83	     height: 220px;
   84	     font-size: 0;
   85	   }
   86div.sudoku-row     { height: 11.11%; }
   87div.sudoku-row.fat { border-bottom: 2px solid black;}
   88div.sudoku-cell { width: 11.11%; height: 100%;
   89		  font-size: 12px;
   90		  font-weight: bold;
   91		  display: inline-block;
   92		  box-sizing: border-box;
   93		  border: 1px solid #888;
   94		  margin: 0px;
   95		  text-align: center;
   96		  vertical-align: middle;
   97		}
   98div.sudoku-cell.fat { border-right: 2px solid black;}
   99	      </style>
  100	     |}).
  101
  102rows([], _) --> [].
  103rows([H|T], I) -->
  104	{ I2 is I+1,
  105	  (   (I == 3 ; I == 6)
  106	  ->  Extra = [fat]
  107	  ;   Extra = []
  108	  )
  109	},
  110	html(div(class(['sudoku-row'|Extra]), \cells(H, 1))),
  111	rows(T, I2).
  112
  113cells([], _) --> [].
  114cells([H|T], I) -->
  115	{ I2 is I+1,
  116	  (   (I == 3 ; I == 6)
  117	  ->  Extra = [fat]
  118	  ;   Extra = []
  119	  )
  120	},
  121	html(div(class(['sudoku-cell'|Extra]), \value(H))), cells(T, I2).
  122
  123value(H) --> { var(H) }, !.
  124value(H) --> term(H, []).
  125
  126
  127%%	is_sudoku(+Term) is semidet.
  128%
  129%	Type check for a term  to  be   a  representation  for  a Sudoku
  130%	puzzle.
  131
  132is_sudoku(Term) :-
  133	is_list(Term),
  134	length(Term, 9),
  135	maplist(is_row, Term).
  136
  137is_row(Row) :-
  138	is_list(Row),
  139	length(Row, 9),
  140	maplist(is_cell, Row).
  141
  142is_cell(Var) :- var(Var).
  143is_cell(I)   :- integer(I), between(1, 9, I)