8 queens puzzle Netbeans(java) graphical interface + SWI prolog -


i'm found solution, on bprolog, , ask how translate jpl on swi prolog? or maybe can take me solution jpl libriry using

    // nobukuni kino import java.awt.*; import java.awt.event.*; import java.applet.*; import bprolog.plc.plc;    
public  class queens extends applet implements runnable {   static int nqueens = 8;   /* run application */     public static void main(string args[]) {         frame f = new frame("queens");    f.addwindowlistener(new windowadapter(){     public void windowclosing(windowevent e){       system.exit(0);     }});    queens qb = new queens();         qb.init();         f.add("center",qb);         f.setsize(321,321);         f.show();   qb.run1();   system.exit(0);     }    int w,h;   color piececolor = new color(255,150,150);   thread runner;    public void start() {       if (runner == null) {           runner = new thread(this);           system.out.println("start");           runner.start();       } else {           runner.resume();       }   }    public void stop() {       if (runner != null) {           runner.suspend();       }   }    public void run(){}    public void run1() {       plc.startplc(new string []{});       integer[] queens = new integer[nqueens];       (int i=0; i<nqueens; i++) queens[i] = new integer(i+1);       plc goal = new plc("callqueens", new object[] {queens,this});                 plc.exec("load('queens')");       goal.call();   }    public void paint(graphics g) {       board(g);   }   public void update(graphics g) {   }    public void board(graphics g) {       w = (getsize().width-1)/nqueens;       h = (getsize().height-1)/nqueens;       g.setcolor(color.black);       g.drawrect(0, 0, nqueens*w+1, nqueens*h+1);        (int = 1; <= nqueens; i++) {           (int j = 1; j <= nqueens; j++) {               clearsquare(i,j);           }       }   }   public void putsquare(integer row, integer col) {       putsquare(row.intvalue(), col.intvalue());   }   public void putsquare(int row, int col) {       graphics g = getgraphics();       g.setcolor(piececolor);       g.fillrect(w*(row-1)+1, h*(col-1)+1, w, h);       thread.yield();   }    public void clearsquare(integer row, integer col) {       clearsquare(row.intvalue(), col.intvalue());   }   public void clearsquare(int row, int col) {       graphics g = getgraphics();       if ((row+col)%2 == 1) {           g.setcolor(color.black);       }       else {           g.setcolor(color.white);       }       g.fillrect(w*(row-1)+1, h*(col-1)+1, w, h);       thread.yield();   }   public void sleep(integer mill) {       try {           thread.sleep(mill.intvalue(),0);       } catch(interruptedexception e) {}   } } 
:-module queens. :-public queens/2. draw(m,n):-         global_get(board,qb),     javamethod(qb,putsquare(m,n)). draw(m,n):-         global_get(board,qb),     javamethod(qb,clearsquare(m,n)),     fail.  callqueens(q,qb):-     cputime(start),     queens(q,qb),     cputime(end),     t end-start,         write(executiontime(t)),nl,         statistics.  queens(q,qb):-     javamethod(qb,sleep(500)),         global_set(board,qb),         put(q,[],r),write(r),nl,     javamethod(qb,sleep(1000)),     fail. queens(q,qb):-     global_set(board,[]). % qb not valid after return java  put([q1|qs],board,result):-!,     sel([q1|qs],q,rs),     safe(board,q,q),     length(qs,l),n 1+l,     draw(q,n),     put(rs,[q|board],result). put([],result,result).  safe([q|rs],p,m):-!,    pp p+1,    q\==pp,    mm m-1,     q\==mm,    safe(rs,pp,mm). safe([],p,m).  sel([x|y],x,y). sel([x|y],z,[x|w]):-sel(y,z,w). 

a literal translation not worth effort. code show encodes specific search strategy extremely difficult modify. actual logic , side effects interwoven.

however, there an animation library(clpfd) of swi far more interesting. there, possible exchange labeling strategies directly. can see why simple naive labeling turns out ineffective. equally, can add own strategy, without modification of viewer's code.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -