Thanks a ton for your help, I think I have the program mostly figured out. I am trying to do stacked as a recursive function, so far I have
stacked([], Y) :0 \on+(_, Y).
stacked([Z | _], N):- on(Z, N), stacked([], Z).
It works in cases where there are 0-1 blocks stacked on another, but...