Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Author:
- % Date: 08.01.2019
- printlist([]):- write('========\n').
- printlist([X|List]) :-
- write(X),nl,
- printlist(List).
- get_word_list(List):- findall(X,add_word(X),List).
- add_word_to_dynamic_predicate([]):- !.
- add_word_to_dynamic_predicate([H|T]):- assert(add_word(H)), add_word_to_dynamic_predicate(T).
- store_in_local_db(Data):- retractall(add_word(_)),append(List, [_], Data),length(List, Len),((Len =:= 0) -> write('No data !!!'); add_word_to_dynamic_predicate(List)).
- write_data([],'y'):- !.
- write_data([H|T], 'y'):- open('words.txt', append, OutputStream), string_concat(H,'.',Data) ,write(OutputStream,Data), nl(OutputStream), close(OutputStream), write_data(T,'y').
- write_data(_,_):- write('redirecting to home'), start_game().
- %CRUD Operation code%
- open_file(FileName,OutputStream,Data):- nl(OutputStream),open(FileName,write,OutputStream), write(OutputStream,Data),nl(OutputStream),close(OutputStream).
- append_file(FileName,OutputStream,Data):- open(FileName,append,OutputStream), write(OutputStream,Data),nl(OutputStream), close(OutputStream).
- read_file(FileName):- open(FileName, read, InputStream),readfile(InputStream,Data),close(InputStream), store_in_local_db(Data),nl.
- readfile(Stream,[]) :- at_end_of_stream(Stream).
- readfile(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), readfile(Stream,L).
- create_new_file():- open('words.txt',write,OutputStream), nl(OutputStream), close(OutputStream).
- %Game Code%
- choose_word(Word) :-
- get_word_list(List),
- length(List, Len),
- random(0, Len, Index),
- nth0(Index, List, Word).
- create_stars(0,'').
- create_stars(N,Str) :-
- M is N-1,
- create_stars(M,Str2),
- string_concat('*',Str2,Str), !.
- init_play() :-
- choose_word(Word),
- string_length(Word,N),
- create_stars(N,Stars),
- play(Word,Stars,0).
- play(RightWord, GuessedWord, Tries) :-
- writeln(''),
- TriesNext is Tries+1,
- write('Please guess the word: '), writeln(GuessedWord),
- write('Please guess a letter: '),
- get_single_char(C), char_code(Guess,C),
- atom_chars(RightWord, WordCharacters),
- findall(Ind, nth0(Ind,WordCharacters,Guess), Indices),
- replace_str(GuessedWord, Indices, Guess, NewGuessedWord),
- (RightWord=NewGuessedWord ->
- end_play(TriesNext);
- play(RightWord, NewGuessedWord, TriesNext)), !.
- replace_str(Str, Ind, Guess, RetStr) :-
- atom_chars(Str, StrChars),
- length(StrChars, Len),
- LastIndex is Len-1,
- replace_list(StrChars, Ind, Guess, LastIndex, RetList),
- atom_chars(RetStr,RetList).
- replace_list(_, _, _, -1, []).
- replace_list(CharList, Indices, Guess, Index, RetList) :-
- nth0(Index,CharList,Char),
- Index2 is Index-1,
- replace_list(CharList, Indices, Guess, Index2, RetList2),
- (member(Index,Indices) ->
- append(RetList2,[Guess],RetList) ;
- append(RetList2,[Char],RetList )), !.
- end_play(Tries) :-
- writeln(''), write('You needed '), write(Tries), write(' tries. \n'), start_game().
- %==========%
- dynamic add_word/1.
- read_data_from_file():- (exists_file('words.txt')-> read_file('words.txt');create_new_file(), write('No data !!!')), start_game().
- list_knowledge_base():- get_word_list(List), length(List,Len), ((Len =:=0) -> write('No data');write('========\n'),printlist(List)), start_game().
- add_new_word():- write('Please enter a word: \n'), readln([H|T]), assert(add_word(H)),start_game().
- delete_word():- write('Please enter a word you want to delete: \n'), readln([H|T]), get_word_list(List), ( member(H,List) -> retract(add_word(H)), write('Succefully deleted.\n'); write("Word is not in Databse\n")),start_game().
- write_database_file():- get_word_list(List), length(List, Len), ((Len =:= 0) -> write('No data !!!'),create_new_file();
- write("File will be overridden, press 'y' for yes and 'n' for No."), get_single_char(X), char_code(Char_Code,X),file_utils(List,Char_Code)),start_game().
- file_utils(List,'y'):- create_new_file(),write_data(List,Char_Code),!.
- file_utils(List,'n'):- write_data(List,Char_Code),!.
- file_utils(List,_):- write('Invalid Option try again\n '),!.
- guess_word():- init_play().
- end_game():- write('Game Over'), !.
- game('r'):- read_data_from_file(),!.
- game('l'):- list_knowledge_base(),!.
- game('a'):- add_new_word(),!.
- game('d'):- delete_word(),!.
- game('w'):- write_database_file(),!.
- game('g'):- guess_word(),!.
- game('e'):- end_game().
- game(_):- write('Invalid Chracter \n'), start_game().
- start_game():- write('Guess a Word \n ===================== \n r - read database file \n l - list knowledge base \n a - add a new word \n d - delete a word \n w - write database file \n g - guess a word \n e - end the game'),
- nl,get_single_char(X), char_code(Char_Code,X),game(Char_Code).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement