2014-12-13 85 views
0

我有這個代碼,我想在控制檯中打印一塊板。代碼在打印方面按預期工作,但最終會返回「否」,而不是「是」。我怎樣才能使這個遞歸返回true?序言遞歸始終沒有結束

主要代碼:

printLine(_,Size,Size). 
printLine([H1,H2|R],Size,Indice) :- 
     write(H1), write(' '), write(H2), write(' '), 
     IndiceNovo is Indice+1, 
     printLine(R,Size,IndiceNovo). 

printTops([]). 
printTops([H|T]) :- write(H),write(' '),printTops(T). 

printBoard(_,_,Size,Size*2,_):- nl,nl, write('end'). 
printBoard(Numeros,Topos,Size,Indice,BarrelIndex):- 
     calculateDiv(Size,D), nl, 
     writeDiv(D), nl, 

     TopoIndex1 is (1+BarrelIndex*Size), 
     TopoIndex2 is (Size + BarrelIndex*Size), 

     slice(Topos,TopoIndex1,TopoIndex2,TopsTemp), 
     write(' '), printTops(TopsTemp),nl, 

     Indice2 is Indice+1, 

     Index1 is (1+Indice*Size*2), Index2 is (Size*2 + (Indice*Size*2)), 
     slice(Numeros,Index1,Index2,Linha), 
     printLine(Linha,Size,0),nl, 
     Index21 is (1+(Indice2*Size*2)), Index22 is (Size*2 + (Indice2*Size*2)), 
     slice(Numeros,Index21,Index22,Linha2), 
     printLine(Linha2,Size,0),nl, 

     IndiceNovo is Indice+2, NovoBarrelIndex is BarrelIndex+1, 
     printBoard(Numeros,Topos,Size,IndiceNovo,NovoBarrelIndex). 

printBoard(Board,Topos):- 
     length(Topos,Size), 
     Size1 is truncate(sqrt(Size)), 
     printBoard(Board,Topos,Size1,0,0). 

測試代碼:

p:- 
     B= [1,2,3,4,5,6,7,8,3,4,1,5,7,8,2,6,2,1,4,3,6,7,8,5,5,7,6,8,1,3,4,2,4,6,8,7,3,2,5,1,7,8,2,1,4,5,6,3,6,3,5,2,8,4,1,7,8,5,7,6,2,1,3,4], 
     T= [10,13,26,23,15,21,17,19,25,18,14,90,22,20,64,84], 
     printBoard(B,T). 

我的輸出:

enter image description here

回答

1

你需要評估算術/ 2。

printBoard(_,_,Size,Size2,_):- Size2 is Size*2, nl,nl, write('end'). 
+0

簡單而有效。非常感謝! – 2014-12-13 13:56:50