34
35:- module(swish_render_chess,
36 [ term_rendering//3 37 ]). 38:- use_module(library(http/html_write)). 39:- use_module('../render'). 40
41:- register_renderer(chess, "Render chess board representations").
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).
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 |})
SWISH chessboard renderer
Render chessboards. Currently only deals with the N-queens problem. This file is nevertheless called
chess.pl
because it should be trivial to extend this to more general chess positions.The styling is a small modification of CSS3 Chess Board */