File: queens.html

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (134 lines) | stat: -rw-r--r-- 4,161 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

<html lang="en">
	<head>
	    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	    <title>Eight Queens Demo</title>
	    <meta name="author" content="Torbjörn Lager and Jan Wielmaker">
	    <script language="javascript" type="text/javascript" src="/usr/share/javascript/jquery/jquery.js"></script>
	    <script type="text/javascript" src="/js/pengines.js"></script>
	    <script type="text/x-prolog">

	        /*  queens(+N, -Queens) is nondet.
			@author Richard A. O'Keefe (The Craft of Prolog)
	        */

	        queens(N, Queens) :-
	            length(Queens, N),
			board(Queens, Board, 0, N, _, _),
			queens(Board, 0, Queens).

	        board([], [], N, N, _, _).
	        board([_|Queens], [Col-Vars|Board], Col0, N, [_|VR], VC) :-
			Col is Col0+1,
			functor(Vars, f, N),
			constraints(N, Vars, VR, VC),
			board(Queens, Board, Col, N, VR, [_|VC]).

	        constraints(0, _, _, _) :- !.
	        constraints(N, Row, [R|Rs], [C|Cs]) :-
			arg(N, Row, R-C),
			M is N-1,
			constraints(M, Row, Rs, Cs).

			queens([], _, []).
	        queens([C|Cs], Row0, [Col|Solution]) :-
			Row is Row0+1,
			select(Col-Vars, [C|Cs], Board),
			arg(Row, Vars, Row-Row),
			queens(Board, Row, Solution).

	    </script>
	    <style type="text/css" media="screen">
	        #board {
	            width: 400px;
	            height: 400px;
	            border: 20px groove #630;
	        }
	        .row {
	            width: 100%;
	        }
	        .square {
	            float: left;
	        }
	        .odd {
	            background-color: #E4E4E4;
	        }
	        .even {
	            background-color: #8A4117;
	        }
	        .square-img {
	            width:100%;
	            height:100%;
	        }
	    </style>
	    <script type="text/javascript">
	        var boardsize = 8;
	        var boardwidth = 400;
	        var pengine = new Pengine({
	            onsuccess: handleSuccess,
	            onfailure: handleFailure,
	            onerror:   handleError
	        });
	        function board(size, width) {
	            var board = "";
	            var fieldsize = Math.floor(width/size);
	            for(var i=1; i<=size; i++) {
	                board += row(i, size, fieldsize);
	            }
	            $("#board").html(board);
	        }
	        function row(n, size, fieldsize) {
	            var y = fieldsize * (n-1);
	            var row = "<div class='row' style='top:"+y+"px; left:0px;'>\n";
	            for ( var i=1; i<=size; i++ ) {
	                var id = i+"-"+n;
	                var oe = (i+n)%2 == 0 ? "even" : "odd";
	                row += "<div id='"+id+"' class='square "+oe +
	                   "' style='width:"+fieldsize+"px; height:"+fieldsize+"px'></div>\n";
	            }
	            row += "</div>\n";
	            return row;
	        }
	        function clearBoard() {
	            for (var i = 1; i <= boardsize; i++) {
	                for (var j = 1; j <= boardsize; j++) {
	                    $("#"+i+"-"+j).html("");
	                }
	            }
	        }
	        function setQueens(squareList) {
	            for (var i = 1; i <= boardsize; i++) {
	                var id = i+"-"+squareList[i-1];
	                $("#"+id).html("<img src='queen.png' class='square-img'/>");
	            }
	        }
	        function handleSuccess() {
	            clearBoard();
	            setQueens(this.data[0].Queens);
	        }
	        function handleFailure() {
	            $("#msg").html('No more solutions');
	        }
	        function handleError() {
	            $("#msg").html(this.data);
	        }
	        function query() {
	            $("#msg").html("");
	            pengine.ask('queens(8, Queens)');
	        }
	        function next() {
	            pengine.next();
	        }
	    </script>
	</head>
	<body onload="board(boardsize, boardwidth)">
	    <center style="margin-top:20px;">
		<div id="board"></div>
		<div style="margin-top:20px;">
			<button id="first" onclick="query()">First</button>
			<button id="next" onclick="next()">Next</button>
			<div style="margin-top:20px;" id="msg"></div>
	        </div>
	    </center>
	</body>
</html>