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-2016, 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_chess, 36 [ term_rendering//3 % +Term, +Vars, +Options 37 ]). 38:- use_module(library(http/html_write)). 39:- use_module('../render'). 40 41:- register_renderer(chess, "Render chess board representations"). 42 43/** <module> SWISH chessboard renderer 44 45Render chessboards. Currently only deals with the N-queens problem. This 46file is nevertheless called =chess.pl= because it should be trivial to 47extend this to more general chess positions. 48 49The styling is a small modification of [CSS3 Chess 50Board](http://designindevelopment.com/css/css3-chess-board/) 51*/ 52 53%% term_rendering(+Term, +Vars, +Options)// 54% 55% Render an N-queens problem. This renderer assumes that the 56% solution is represented by a permutation of a list of integers 57% 1..N, where the I-th integer describes the column of the queen 58% at row I. 59 60term_rendering(Term, _Vars, _Options) --> 61 { is_nqueens(Term), 62 length(Term, N), 63 LineHeight is 200/N 64 }, 65 html(div([ style('display:inline-block;'+ 66 'line-height:'+LineHeight+'px;'+ 67 'font-size:'+LineHeight+'px;' 68 ), 69 'data-render'('Chess board') 70 ], 71 [ table(class('chess-board'), 72 \nqueens(Term, N)), 73 \chess_style 74 ])). 75 76is_nqueens(Term) :- 77 is_list(Term), 78 maplist(integer, Term), 79 length(Term, N), 80 numlist(1, N, All), 81 sort(Term, All). 82 83nqueens([], _) --> []. 84nqueens([H|T], N) --> 85 html(tr(\nrow(0, N, H))), 86 nqueens(T, N). 87 88nrow(N, N, _) --> !. 89nrow(I, N, At) --> 90 { I2 is I+1 }, 91 ( { I2 == At } 92 -> html(td(&('#9819'))) 93 ; html(td([])) 94 ), 95 nrow(I2, N, At). 96 97%% chess_style// 98% 99% @see http://designindevelopment.com/css/css3-chess-board/ 100 101chess_style --> 102 html({|html|| 103<style> 104.chess-board { 105 border:2px solid #333; width:200px; height:200px; 106} 107.chess-board td { 108 background:#fff; 109 background:-moz-linear-gradient(top, #fff, #eee); 110 background:-webkit-gradient(linear,0 0, 0 100%, from(#fff), to(#eee)); 111 box-shadow:inset 0 0 0 1px #fff; 112 -moz-box-shadow:inset 0 0 0 1px #fff; 113 -webkit-box-shadow:inset 0 0 0 1px #fff; 114 text-align:center; 115 vertical-align:middle; 116} 117.chess-board tr:nth-child(odd) td:nth-child(even), 118.chess-board tr:nth-child(even) td:nth-child(odd) { 119 background:#ccc; 120 background:-moz-linear-gradient(top, #ccc, #eee); 121 background:-webkit-gradient(linear,0 0, 0 100%, from(#ccc), to(#eee)); 122 box-shadow:inset 0 0 10px rgba(0,0,0,.4); 123 -moz-box-shadow:inset 0 0 10px rgba(0,0,0,.4); 124 -webkit-box-shadow:inset 0 0 10px rgba(0,0,0,.4); 125} 126</style> 127 |})