• API
• FAQ
• Tools
• Archive
daily pastebin goal
31%
SHARE
TWEET

# Untitled

a guest Oct 18th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. % randelem(+list,?elem)
2. randelem(L,X) :- length(L,LL), I is random(LL), nth0(I,L,X).
3.
4. % selandrem(+index,+list,-list,-elem)
5. selandrem0(0,[X|R],R,X) :- !.
6. selandrem0(I,[A|R],[A|Rout],X) :- NI is I-1, selandrem0(NI,R,Rout,X).
7.
8. % just a helper function to boggleboard, below
9. % bbhelp(-board,+nleft,+cubes)
10. bbhelp([],0,[]) :- !.
11. bbhelp([X|R],L,CS) :- I is random(L), selandrem0(I,CS,NCS,C), randelem(C,X), !,
12.                 NL is L-1, bbhelp(R,NL,NCS).
13.
14. % returns a randomly chosen boggle board.
15. % No choice points -- will not backtrack
16. % boggleboard(-board)
17. boggleboard(B) :- bbhelp(B,16,
18.         [['A','A','E','E','G','N'],['A','B','B','J','O','O'],
19.          ['A','C','H','O','P','S'],['A','F','F','K','P','S'],
20.          ['A','O','O','T','T','W'],['C','I','M','O','T','U'],
21.          ['D','E','I','L','R','X'],['D','E','L','R','V','Y'],
22.          ['D','I','S','T','T','Y'],['E','E','G','H','N','W'],
23.          ['E','E','I','N','S','U'],['E','H','R','T','V','W'],
24.          ['E','I','O','S','S','T'],['E','L','R','T','T','Y'],
25.          ['H','I','M','N','Q','U'],['H','L','N','N','R','Z']]).
26.
27. % isboggleword(+board,+dictionary,?word)
28. % checks if the word given is a valid word on the board,
29. % if the word is not given, it will generate all the valid words on the board.
30. isboggleword(Board,node(C,true,_,_),[C]):-boggleletter(Board,_,_,C).
32. isboggleword(Board,node(_,_,_,L),[C|T]):-isboggleword(Board,L,[C|T]).
33.
34.
39. checkadjacency(B,C,C2):-boggleletter(B,X,Y,C),Y2 is Y+1,X2 is X-1,boggleletter(B,X2,Y2,C2).
40. checkadjacency(B,C,C2):-boggleletter(B,X,Y,C),Y2 is Y+1,X2 is X+1,boggleletter(B,X2,Y2,C2).
41. checkadjacency(B,C,C2):-boggleletter(B,X,Y,C),Y2 is Y-1,X2 is X-1,boggleletter(B,X2,Y2,C2).
42. checkadjacency(B,C,C2):-boggleletter(B,X,Y,C),Y2 is Y-1,X2 is X+1,boggleletter(B,X2,Y2,C2).
43.
44.
45.
46. isSame(B,[H|T]):-sameuntil(B,H,T).
47. sameuntil(B,H,[H|_]):-boggleletter(B,X,Y,H),boggleletter(B,X2,Y2,H),Y2 =\= Y ; X2 =\=X,!.
48. sameuntil(B,H,[_|T]):-sameuntil(B,H,T).
49.
50.
51.
52. removedup([],[]).
53. removedup([H|InList],[H|OutList]):-not(member(H,InList)),!,removedup(InList,OutList).
54. removedup([H|InList],OutList):-member(H,InList),removedup(InList,OutList).
55.
56. % boggleletter(+board,?xindex,?yindex,?character)
57. boggleletter(B,X,Y,C) :- nth0(N,B,C), XR is N/4, X is floor(XR), Y is mod(N,4).
58.
59.
60. % you should not use this... you will need to traverse the dictionary
61. % yourself.  However, this checks whether the first argument (a list of
62. % characters) is in the dictionary (the second argument)
63. % order it adds the word to the dictionary
64. % indict(?word,?dictionary)
65. indict([X],node(X,true,_,_)).
66. indict([X|RX],node(X,_,Y,_)) :- indict(RX,Y).
67. indict([X|RX],node(_,_,_,Y)) :- indict([X|RX],Y).
68.
69. % a helper function to loaddict, below
73.
74. % a helper function to loaddict, only succeeds if the dictionary is grounded.
75. restrictdict(null) :- !.
76. restrictdict(node(_,false,L,R)) :- !, restrictdict(L), !, restrictdict(R).
77. restrictdict(node(_,true,L,R)) :- !, restrictdict(L), !, restrictdict(R).
78.
84.     restrictdict(Dict),
85.     close(S).
86.
87. % draws a boggle board
88. % drawboard(+board).
89. drawboard(B) :- drawboard(B,16).
90. drawboard(_,0) :- !.
91. drawboard([X|Y],I) :- 1 =:= mod(I,4), !, writeln(X),
92.         II is I-1, drawboard(Y,II).
93. drawboard([X|Y],I) :- write(X), II is I-1, drawboard(Y,II).
94.
95. % word is a list of characters
96. % boggleword(+board,?word)
98.
99. % below is only needed for Q3
100. %---------------------
101.
102. % converttostr(?listlistchar,?liststr)
103. converttostr([],[]).
104. converttostr([X|XR],[Y|YR]) :- atom_chars(Y,X), converttostr(XR,YR).
105.
106. % allbogglewords(+board,-words)