03.06.2013 Views

Turbo Prolog

Turbo Prolog

Turbo Prolog

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A WORD GUESSING GAME USING WINDOWS<br />

Program 29 uses <strong>Turbo</strong> <strong>Prolog</strong>'s window facilities to produce a word guessing game. To<br />

keep it fairly short, the operation of the program is quite primitive, but the windows<br />

make its on-screen presentation impressive nevertheless.<br />

The player must guess a total of three words in turn. First. s/he is asked to type in a<br />

letter. If that letter is in the word, it is put in the YES window. If not. the letter goes into<br />

the NO window. After each guess at a letter, the player is asked to guess the whole<br />

word, which must then be typed in letter-by-Ietter, and the IE) key pressed after<br />

every letter. A record is kept of the total number of guesses.<br />

86<br />

1* Program 29 *1<br />

dOlains<br />

list=symbol*<br />

. scores=integer<br />

predicates<br />

member(symbol,list)<br />

run<br />

continue(list,scores)<br />

yes_no_count(symbol,list)<br />

guess_word(scores,list)<br />

word(list,integer)<br />

read_as_list(list,integer)<br />

goal<br />

makewindow(1,7,D,III1,D,D,2S,8D),<br />

makewindow(2,7,13S,IICounting ll ,1,2D,L;,3L;),<br />

makewindow(3,112,112,IIYES II ,S,S,7,30),<br />

makewindow(L;,112,112,IINO II ,S,L;D,7,3D),<br />

makewindow(S,7,7,III1,M,2D,1D,3L;),<br />

run.<br />

clauses<br />

run:- word(W,L),<br />

shiftwindow(1),clearwindow,<br />

write(IIThe word has II,L,II lettersll ),<br />

shiftwindow(2),clearwindow,<br />

shiftwindow(3),clearwindow,<br />

shiftwindow(L;),continue(W,O),fail.<br />

continue(L,R):shiftwindow(L;),clearwindow,<br />

write(IIGuess a letter :11),<br />

Total=R+1,readln(T),yes_no_count(T,L),<br />

shiftwindow(L;),clearwindow,<br />

guess_word(Total,L),continue(L,Total).<br />

yes_no_count(X,List):member(X,List),shiftwindow(2),write(X),!.yes_no_count(X,_):shiftwindow(3),write(X).guess_word(Count,Word):write(IIKnow<br />

the word yet? Press y or nil),<br />

readchar(A),A='y',cursor(O,D),<br />

write(IIType it in one letter per line \nll),<br />

word(Word,L),read_as_list(G,L),<br />

G=Word,clearwindow,window_attr(112),<br />

write(IIRight! You used II,Count,1I guess(es)II),<br />

readchar(_),window_attr(7),!,fail.<br />

<strong>Turbo</strong> <strong>Prolog</strong> Owner's Handbook

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!