2
votes

So my goal is to display the world as it stands based on these facts(without just hardcoding it):

on(a,table).
on(b,table).
on(c,table).
on(d,table).
on(e,table).
on(f,table).
on(g,table).
on(h,b).
on(i,h).
on(j,i).
on(k,c).
on(l,k).
on(m,f).
on(n,m).
left(a,b).
left(b,c).
left(c,d).
left(d,e).
left(e,f).
left(f,g).

It should look something similar to this after being printed.

          J  
          i  L        N  
          h  k        m  
       a  b  c  D  E  f  g
       ___________________________________
           Table  

I've tried using bagof() similar to this to grab each level but this would get tedious for more than 2 levels, im sure there is a better way to do this I just cant think of it.

bagof(X,(on(X,table)),Bottom),  
bagof(D,Z^(on(D,Z),on(Z,table)),Level01),  

Any help would be greatly appreciated, thanks.

1

1 Answers

0
votes

separating the 'logical' layout from actual IO issues could help (but note that left/2 constraint isn't used):

coords(Cs) :-
    findall(E, on(E,table), Base),
    findall(placed(E,1,C), nth1(C,Base,E), OnTable),
    rows(OnTable, [], Cs).

rows([], Rs, Rs).
rows(Row, Rows, Full) :-
    findall(placed(F,R1,C), (member(placed(E,R,C), Row), on(F,E), R1 is R+1), ThisRow),
    rows(ThisRow, [Row|Rows], Full).

now we get

?- coords(X),maplist(writeln,X).
[placed(j,4,2)]
[placed(i,3,2),placed(l,3,3),placed(n,3,6)]
[placed(h,2,2),placed(k,2,3),placed(m,2,6)]
[placed(a,1,1),placed(b,1,2),placed(c,1,3),placed(d,1,4),placed(e,1,5),placed(f,1,6),placed(g,1,7)]
X = [[placed(j, 4, 2)], [placed(i, 3, 2), placed(l, 3, 3), placed(n, 3, 6)], [placed(h, 2, 2), placed(k, 2, 3), placed(m, 2, 6)], [placed(a, 1, 1), placed(b, 1, 2), placed(c, 1, 3), placed(d, 1, 4), placed(..., ..., ...)|...]] 
.

[placed(a,0,1),placed(b,0,2),placed(c,0,3),placed(d,0,4),placed(e,0,5),placed(f,0,6),placed(g,0,7)]

so, instead of writeln/1, should be enough to print a row with spaces to fill skipped columns.

Actually, the Row in placed(Label,Row,Col) could be implicit, and the code would be simpler... see if you can do this 'optimization'.