home *** CD-ROM | disk | FTP | other *** search
Prolog Source | 1986-06-07 | 11.8 KB | 363 lines |
-
- domains
- colors_list = color*
- color = symbol
- code = integer
- row, column, keycode, attribute, displa = integer
- database
- guess_colors(colors_list)
- to_guess(colors_list)
- my_guess(colors_list)
-
- predicates
- member(color, colors_list)
- append(colors_list, colors_list, colors_list)
- extract_element(color, colors_list, colors_list, integer)
- check_color(integer, integer,colors_list)
- colors(code, color)
- select_colors(colors_list)
- is_random(color)
- check_pos(integer,integer,colors_list, colors_list)
- check_guess(integer, integer, colors_list)
- showGuess(colors_list,column)
- get_element(color,integer,colors_list)
- final(integer)
- run
- inform
- putcolor(keycode, row, column,column)
- attrib(color,keycode,attribute, displa)
- readKey(keycode, row, column)
- new_cur(column, displa, column)
- getEntry(row,column)
- ireadchar(char, row, column)
- fakekey(char, row, column)
- init_line(row, column,char)
- read_line(row,column,colors_list,colors_list)
- blink(row,column)
- delay(integer)
- play_game(integer)
- play_line(row,integer,integer)
- allcolors
- goal
- makewindow(8,40,0,"",0,0,25,80),
- inform,
- not(run),
- makewindow(8,7,0,"",0,0,25,80).
-
- clauses
-
- /* some "facts" (tables, just tables) */
- colors(1,"Blue").
- colors(2,"Green").
- colors(3,"Cyan").
- colors(4,"Magenta").
- colors(5,"Red").
- colors(6,"Orange").
-
- attrib("Blue",98,113,0). attrib("Green",103,114,0).
- attrib("Cyan",99,115,0). attrib("Red",114,116,0).
- attrib("Magenta",109,117,0). attrib("Orange",111,118,0).
- attrib(left,75,0,-7). attrib(right,77,0,7).
-
- /* Main module (goal) */
-
- run :- !,
- play_game(Result),
- final(Result),
- nl,nl,
- write("Another game (Y/N)?"),
- readchar(Another),
- Another = 'y',
- removewindow,
- removewindow,
- removewindow,
- removewindow,
- run, !.
-
-
- inform :-
- makewindow(6,113,31,"INSTRUCTIONS",0,1,25,34),
- write("(c) 1986 by Israel del Rio"),
- nl,nl,
- write("Guess the 4 hidden colors from\nthe following: "),
- nl,
- allcolors.
-
- inform :-
- nl,nl,
- write(" Type the first letter of the \ncolor name to be selected."),
- nl,
- write(" Use the left and right arrows\nto change columns."),
- nl,
- write(" Press ENTER when you are\nsatisfied with your guess."),
- nl,nl,
- write("\01 = Right Position & Color"),
- nl,
- write("\05 = Right Color, wrong position"),
- nl,nl,
- write(" Press any key to start game"),
- readchar(_).
-
- allcolors :-
- attrib(Name,_,Attr,_),
- Attr <> 0,
- nl,
- cursor(Row, Col),
- write("\219\219 = ",Name),
- field_attr(Row,Col,16,Attr),
- fail.
-
- play_game(Result) :-
-
- makewindow(12,0,0,"",7,36,16,39),
- makewindow(2,31,116,"Hints",3,61,16,8),
- cursor(0,1),
- write("\01"," ","\05"),
- makewindow(1,112,116,"Master Mind",3,30,16,31),
- select_colors(Y),
- clearwindow,
- init_line(0,1,'?'),
- /* showGuess(Y,1),*/
- play_line(2,0,Result),
- retract(guess_colors(_)),
- showGuess(Y, 1), !.
-
-
- final(0) :-
- sound(10,5000),
- makewindow(3,87,87,"Sorry!",16,0,8,20),
- clearwindow,
- write(" Maximum number of guesses is 12"), !.
-
- final(Result) :-
- sound(10,5000),
- makewindow(3,106,6,"Congratulations!",17,59,8,20),
- clearwindow,
- write(" You guessed the colors in ",Result," moves"), !.
-
-
- /* randomly selects 4 colors out of 6*/
-
- select_colors([A,B,C,D]) :- retract(guess_colors(_)), !,
- select_colors([A,B,C,D]).
-
- select_colors([A,B,C,D]) :- is_random(A), is_random(B),
- is_random(C), is_random(D),
- asserta(guess_colors([A,B,C,D])).
-
-
- is_random(X) :- random(Y), Z = (Y * 6) + 0.5, colors(Z,X).
-
-
- /* Evaluates how good the List guess was */
-
- check_guess(Good_pos, Good_col, List) :-
- retract(to_guess(_)),
- retract(my_guess(_)),
- check_guess(Good_pos, Good_col, List), !.
-
- check_guess(Good_pos, Good_col, List) :-
- guess_colors(Glist),
- asserta(to_guess([])),
- asserta(my_guess([])),
- check_pos(Good_pos,0,List, Glist),
- my_guess(Mlist),
- check_color(Good_col,0,Mlist), !.
-
- /* checks how many exact matches occurred*/
-
- check_pos(X,Y,[],_) if X = Y, !.
- check_pos(X,Y,[H|Tail],[G|Gtail]) if H = G, Z = Y + 1,
- check_pos(X,Z,Tail, Gtail), !.
-
- check_pos(X,Y,[H|Tail],[Z|Gtail]) :-
- W=Z, to_guess(GC),
- append([W], GC,NGC),
- retract(to_guess(_)),
- asserta(to_guess(NGC)),
- HH=H, my_guess(GG),
- append([HH],GG,NGG),
- retract(my_guess(_)),
- asserta(my_guess(NGG)),
- check_pos(X,Y,Tail,Gtail).
-
- /* Checks if color ok, but not right pos */
-
- check_color(X,Count,[]) :- X = Count, !.
-
- check_color(X,Count,[H|Tail]) :-
- to_guess(Newlist),
- extract_element(H,Newlist,[], Success),
- Success = 1,
- Count2 = Count + 1,
- check_color(X,Count2, Tail), !.
-
- check_color(X,Count,[_|Tail]) :-
- check_color(X, Count, Tail).
-
- /* extracts an element from a list */
-
- extract_element(_,[],NewNew,X) :-
- X = 0,
- retract(to_guess(_)),
- asserta(to_guess(NewNew)), !.
-
- extract_element(X,[Y|List],Newlist,F) :-
- X<>Y, append(Newlist,[Y],NewNew),
- extract_element(X,List,NewNew,F), !.
-
-
- extract_element(_,[_|List],Newlist,X) :-
- X = 1,
- append(Newlist,List,NewNew),
- retract(to_guess(_)),
- asserta(to_guess(NewNew)), !.
-
-
-
- /* Performs & verifies one move */
- play_line(Row,4,Res) :- Res = Row - 2, !.
-
- play_line(14,_,Res) :- Res = 0, !.
-
- play_line(Row,_,Res) :-
- init_line(Row,1,'\178'),
- cursor(Row,1),
- getEntry(Row,1),
- read_line(Row,1,[],Selection),
- check_guess(M,C,Selection),
- shiftwindow(2),
- cursor(Row,1),
- write(M," ",C),
- shiftwindow(1),
- NewRow = Row + 1,
- play_line(NewRow,M,Res), !.
-
- play_line(Row,_,Res) :-
- sound(10,100),
- makewindow(3,64,64,"ERROR",9,26,4,48),
- clearwindow,
- write("You must select 4 colors before presing ENTER"),
- nl,write(" Press any key to continue"),
- readchar(_),
- removewindow,
- play_line(Row,0,Res), !.
-
- init_line(_,29,_) :-!.
- init_line(Row, Col,V) :-
- cursor(Row, Col),
- write(V,V,V,V,V,V),
- NewCol = Col + 7,
- init_line(Row, NewCol,V), !.
-
- read_line(_,29, PrevList, List) :- List = PrevList, !.
- read_line(Row, Col, PrevList, List) :-
- scr_attr(Row, Col, Attr),
- attrib(X, _, Attr, _),
- append(PrevList,[X],NewList),
- NewCol = Col + 7,
- read_line(Row, NewCol, NewList, List), !.
-
- /* Reads player entry */
-
- getEntry(Row, Column) :-
- readKey(K, Row, Column),
- K <> 13,
- putcolor(K, Row, Column, NewCol),
- getEntry(Row,NewCol), !.
-
- getEntry(_, _) :- !.
-
-
- showGuess([],_) :- !.
-
- showGuess([C|List],Column) :-
- cursor(0,Column),
- attrib(C,Code,_,_),
- putcolor(Code, 0, Column, _),
- Newcol = Column + 7,
- showGuess(List,Newcol), !.
-
- /* Reads typed keys */
-
- readKey(K, Row, Col) :-
- fakekey(X, Row, Col),
- X <> '\0',
- frontchar(Y,X,""),
- upper_lower(Y,Z),
- frontchar(Z,C,_),
- char_int(C,K), !.
-
- readKey(K, Row, Col) :-
- fakekey(C, Row, Col),
- char_int(C,K), !.
-
-
- fakekey(K,Row,Col) :- ireadchar(K,Row,Col), !.
- fakekey(K,Row,Col) :- fakekey(K,Row,Col).
-
- ireadchar(K,_,_) :- inkey(L), !, K = L.
-
- ireadchar(_, Row, Col) :-
- blink(Row, Col), fail.
-
- blink(Row, Col) :- !,
- scr_attr(Row, Col,Attr),
- delay(600),
- field_attr(Row, Col, 6, 0),
- delay(100),
- field_attr(Row, Col, 6, Attr).
-
-
- putcolor(K, Row, Col,NewCol) :-
- attrib(_, K, Attr, _),
- Attr <> 0,
- NewCol = Col,
- write('\219','\219','\219','\219','\219','\219'),
- field_attr(Row,NewCol,6,Attr),
- cursor(Row, NewCol), !.
-
- putcolor(K, Row, Col, Newcol) :-
- attrib(_, K, _, Disp),
- new_cur(Col, Disp, NewCol),
- cursor(Row, Newcol), !.
-
- putcolor(_, _, OldCol, NewCol) :-
- sound(2,50), NewCol = OldCol.
-
-
-
- new_cur(Old_col, Disp, New_col) :-
- New_col = Old_col + Disp,
- New_col < 23,
- New_col > 0, !.
-
- new_cur(_, Disp, New_col) :-
- Disp < 0,
- New_col = 22, !.
-
- new_cur(_, _, New_col) :-
- New_col = 1.
-
-
- /**************** GENERAL PURPOSE ROUTINES ***************/
-
-
- /* appends 2 lists to a third list */
- append([],List,List).
- append([X|L1], List2, [X|L3]) if
- append(L1,List2,L3).
-
- /* Gets element #n from a list */
- get_element(X,1,[Z|_]) if ! and X=Z.
- get_element(X,Y,[_|Tail]) :- Counter = Y -1, !,
- get_element(X,Counter,Tail).
-
- /* Verifies if an entry is member of list */
- member(Name,[Name|_]) :- !.
- member(Name,[_|Tail]) if member(Name,Tail).
-
- delay(0) :- !.
- delay(N) :- NN = N - 1, delay(NN).
-
-