Hi guys i have the following code and i m trying to make it work but did't manage can please some one help me, I am new to prolog and i m totally lost. The programm is a crossworld puzzle. Thanks for your time
%% Dictionary
dictionary([hello,exit,test,prolog]).
%% Makes a Vector
makeVector(0,[]).
makeVector(N1,['X'|T]) :- N is N1 - 1,
makeVector(N,T).
%% Creates a Matrix
makeMatrix(Rows,Cols,Matrix) :- Cells is Rows * Cols,
makeVector(Cells,Matrix).
%% printMatrix(Matrix) :- Write(X), nl,
%% Get Cell
getCell(0,[H|_],H).
getCell(Pos, [_|T], Element) :- NextPos is Pos -1,
getCell(NextPos,T,Element).
subList(StartPos,Size,Vector,A3) :- append(A1,A2,Vector), %split vector into a pair of lists
length(A1,StartPos), %only accept A1 with a length of StartPos
append(A3,_,A2), %split A2 into two lists (discarding the one at the back)
length(A3,Size). %only accept A3 with the correct length of Size
getCell(Row,Col,Matrix,NumCols,Element):- Cell is (Row * NumCols) + Col,
getCell(Cell,Matrix,Element).
getRow(RowNum,Matrix,NumCols,Row) :- StartCell is RowNum * NumCols,
subList(StartCell,NumCols,Matrix,Row).
%base case
setCell(0,[_|Tail],NewValue,[NewValue|Tail]).
setCell(Position,[Head|Tail],NewValue,[Head|NewTail]):-
NextPos is Position-1,
setCell(NextPos,Tail,NewValue,NewTail).
setCell(Row,Col,Matrix,NumCols,NewValue,NewMatrix) :-
CellPos is (Row * NumCols) + Col,
setCell(CellPos,Matrix,NewValue,NewMatrix).
%% Transpose Matrix
flip(Matrix,NumCols,Transpose) :-
length(Matrix,L),
makeVector(L,NewMatrix),
NumRows is L / NumCols,
LastRowIndex is NumRows - 1,
LastColIndex is NumCols - 1,
traverseCopy(LastRowIndex,LastColIndex, Matrix, NumRows,NumCols, NewMatrix, Transpose).
traverseCopy(0,0,Matrix,NumRows,NumCols,NewMatrix,Transpose):-
getCell(0,0,Matrix,NumCols,Content),
setCell(0,0,NewMatrix,NumRows,Content,Transpose).
traverseCopy(R,0,Matrix,NumRows,NumCols,NewMatrix,Transpose):-
getCell(R,0,Matrix,NumCols,Content),
setCell(0,R,NewMatrix,NumRows,Content,NewTranspose),
NextRow is R-1, % move one row up
LastCol is NumCols-1, % goto last column (0-based indexing)
traverseCopy(NextRow,LastCol,Matrix,NumRows,NumCols,NewTranspose,Transpose).
traverseCopy(R,C,Matrix,NumRows,NumCols,NewMatrix,Transpose):-
getCell(R,C,Matrix,NumCols,Content),
setCell(C,R,NewMatrix,NumRows,Content,NewTranspose),
NextCol is C-1, % move one column left
traverseCopy(R,NextCol,Matrix,NumRows,NumCols,NewTranspose,Transpose).
%% replaceRow
replaceRow(NewRow,RowNum,Matrix,NumCols,NewMatrix):-
CellNum is RowNum * NumCols,
subList(0,CellNum,Matrix,PREVLIST),
getRow(RowNum,Matrix,NumCols,RowToBeReplaced),
append(PREVLIST,RowToBeReplaced,FirstCellToEndOfRow),
append(FirstCellToEndOfRow,RESTLIST,Matrix),
append(PREVLIST,NewRow,NewFirstCellToEndOfRow),
append(NewFirstCellToEndOfRow,RESTLIST,NewMatrix).
replaceColumn(NewCol,ColNum,Matrix,NumCols,NewMatrix):-
length(Matrix,L),
NumRows is L/NumCols,
flip(Matrix,NumCols,Transpose),
replaceRow(NewCol,ColNum,Transpose,NumRows,NewTranspose),
write(NewTranspose),
write(NumRows),
flip(NewTranspose,NumRows,NewMatrix).
%% Example of a Main Program
main(Puzzle, SolvedPuzzle) :- makeMatrix(4,4,Puzzle),
getCell(2,Puzzle,X).
word2chars([],[]).
word2chars([Word|RestWords],[Chars|RestChars]) :-
atom_chars(Word,Chars),
word2chars(RestWords, RestChars).
%% Dictionary
dictionary([hello,exit,test,prolog]).
%% Makes a Vector
makeVector(0,[]).
makeVector(N1,['X'|T]) :- N is N1 - 1,
makeVector(N,T).
%% Creates a Matrix
makeMatrix(Rows,Cols,Matrix) :- Cells is Rows * Cols,
makeVector(Cells,Matrix).
%% printMatrix(Matrix) :- Write(X), nl,
%% Get Cell
getCell(0,[H|_],H).
getCell(Pos, [_|T], Element) :- NextPos is Pos -1,
getCell(NextPos,T,Element).
subList(StartPos,Size,Vector,A3) :- append(A1,A2,Vector), %split vector into a pair of lists
length(A1,StartPos), %only accept A1 with a length of StartPos
append(A3,_,A2), %split A2 into two lists (discarding the one at the back)
length(A3,Size). %only accept A3 with the correct length of Size
getCell(Row,Col,Matrix,NumCols,Element):- Cell is (Row * NumCols) + Col,
getCell(Cell,Matrix,Element).
getRow(RowNum,Matrix,NumCols,Row) :- StartCell is RowNum * NumCols,
subList(StartCell,NumCols,Matrix,Row).
%base case
setCell(0,[_|Tail],NewValue,[NewValue|Tail]).
setCell(Position,[Head|Tail],NewValue,[Head|NewTail]):-
NextPos is Position-1,
setCell(NextPos,Tail,NewValue,NewTail).
setCell(Row,Col,Matrix,NumCols,NewValue,NewMatrix) :-
CellPos is (Row * NumCols) + Col,
setCell(CellPos,Matrix,NewValue,NewMatrix).
%% Transpose Matrix
flip(Matrix,NumCols,Transpose) :-
length(Matrix,L),
makeVector(L,NewMatrix),
NumRows is L / NumCols,
LastRowIndex is NumRows - 1,
LastColIndex is NumCols - 1,
traverseCopy(LastRowIndex,LastColIndex, Matrix, NumRows,NumCols, NewMatrix, Transpose).
traverseCopy(0,0,Matrix,NumRows,NumCols,NewMatrix,Transpose):-
getCell(0,0,Matrix,NumCols,Content),
setCell(0,0,NewMatrix,NumRows,Content,Transpose).
traverseCopy(R,0,Matrix,NumRows,NumCols,NewMatrix,Transpose):-
getCell(R,0,Matrix,NumCols,Content),
setCell(0,R,NewMatrix,NumRows,Content,NewTranspose),
NextRow is R-1, % move one row up
LastCol is NumCols-1, % goto last column (0-based indexing)
traverseCopy(NextRow,LastCol,Matrix,NumRows,NumCols,NewTranspose,Transpose).
traverseCopy(R,C,Matrix,NumRows,NumCols,NewMatrix,Transpose):-
getCell(R,C,Matrix,NumCols,Content),
setCell(C,R,NewMatrix,NumRows,Content,NewTranspose),
NextCol is C-1, % move one column left
traverseCopy(R,NextCol,Matrix,NumRows,NumCols,NewTranspose,Transpose).
%% replaceRow
replaceRow(NewRow,RowNum,Matrix,NumCols,NewMatrix):-
CellNum is RowNum * NumCols,
subList(0,CellNum,Matrix,PREVLIST),
getRow(RowNum,Matrix,NumCols,RowToBeReplaced),
append(PREVLIST,RowToBeReplaced,FirstCellToEndOfRow),
append(FirstCellToEndOfRow,RESTLIST,Matrix),
append(PREVLIST,NewRow,NewFirstCellToEndOfRow),
append(NewFirstCellToEndOfRow,RESTLIST,NewMatrix).
replaceColumn(NewCol,ColNum,Matrix,NumCols,NewMatrix):-
length(Matrix,L),
NumRows is L/NumCols,
flip(Matrix,NumCols,Transpose),
replaceRow(NewCol,ColNum,Transpose,NumRows,NewTranspose),
write(NewTranspose),
write(NumRows),
flip(NewTranspose,NumRows,NewMatrix).
%% Example of a Main Program
main(Puzzle, SolvedPuzzle) :- makeMatrix(4,4,Puzzle),
getCell(2,Puzzle,X).
word2chars([],[]).
word2chars([Word|RestWords],[Chars|RestChars]) :-
atom_chars(Word,Chars),
word2chars(RestWords, RestChars).