Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. % Author:
  2. % Date: 08.01.2019
  3.  
  4. printlist([]):- write('========\n').
  5. printlist([X|List]) :-
  6. write(X),nl,
  7. printlist(List).
  8. get_word_list(List):- findall(X,add_word(X),List).
  9.  
  10. add_word_to_dynamic_predicate([]):- !.
  11. add_word_to_dynamic_predicate([H|T]):- assert(add_word(H)), add_word_to_dynamic_predicate(T).
  12. 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)).
  13.  
  14. write_data([],'y'):- !.
  15. 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').
  16. write_data(_,_):- write('redirecting to home'), start_game().
  17.  
  18. %CRUD Operation code%
  19. open_file(FileName,OutputStream,Data):- nl(OutputStream),open(FileName,write,OutputStream), write(OutputStream,Data),nl(OutputStream),close(OutputStream).
  20. append_file(FileName,OutputStream,Data):- open(FileName,append,OutputStream), write(OutputStream,Data),nl(OutputStream), close(OutputStream).
  21. read_file(FileName):- open(FileName, read, InputStream),readfile(InputStream,Data),close(InputStream), store_in_local_db(Data),nl.
  22. readfile(Stream,[]) :- at_end_of_stream(Stream).
  23. readfile(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), readfile(Stream,L).
  24. create_new_file():- open('words.txt',write,OutputStream), nl(OutputStream), close(OutputStream).
  25.  
  26.  
  27.  
  28.  
  29.  
  30. %Game Code%
  31.  
  32. choose_word(Word) :-
  33. get_word_list(List),
  34. length(List, Len),
  35. random(0, Len, Index),
  36. nth0(Index, List, Word).
  37.  
  38. create_stars(0,'').
  39. create_stars(N,Str) :-
  40. M is N-1,
  41. create_stars(M,Str2),
  42. string_concat('*',Str2,Str), !.
  43.  
  44. init_play() :-
  45. choose_word(Word),
  46. string_length(Word,N),
  47. create_stars(N,Stars),
  48. play(Word,Stars,0).
  49.  
  50. play(RightWord, GuessedWord, Tries) :-
  51. writeln(''),
  52. TriesNext is Tries+1,
  53. write('Please guess the word: '), writeln(GuessedWord),
  54. write('Please guess a letter: '),
  55. get_single_char(C), char_code(Guess,C),
  56. atom_chars(RightWord, WordCharacters),
  57. findall(Ind, nth0(Ind,WordCharacters,Guess), Indices),
  58. replace_str(GuessedWord, Indices, Guess, NewGuessedWord),
  59. (RightWord=NewGuessedWord ->
  60. end_play(TriesNext);
  61. play(RightWord, NewGuessedWord, TriesNext)), !.
  62.  
  63. replace_str(Str, Ind, Guess, RetStr) :-
  64. atom_chars(Str, StrChars),
  65. length(StrChars, Len),
  66. LastIndex is Len-1,
  67. replace_list(StrChars, Ind, Guess, LastIndex, RetList),
  68. atom_chars(RetStr,RetList).
  69.  
  70. replace_list(_, _, _, -1, []).
  71. replace_list(CharList, Indices, Guess, Index, RetList) :-
  72. nth0(Index,CharList,Char),
  73. Index2 is Index-1,
  74. replace_list(CharList, Indices, Guess, Index2, RetList2),
  75. (member(Index,Indices) ->
  76. append(RetList2,[Guess],RetList) ;
  77. append(RetList2,[Char],RetList )), !.
  78.  
  79. end_play(Tries) :-
  80. writeln(''), write('You needed '), write(Tries), write(' tries. \n'), start_game().
  81.  
  82.  
  83. %==========%
  84.  
  85.  
  86.  
  87.  
  88. dynamic add_word/1.
  89.  
  90. read_data_from_file():- (exists_file('words.txt')-> read_file('words.txt');create_new_file(), write('No data !!!')), start_game().
  91. list_knowledge_base():- get_word_list(List), length(List,Len), ((Len =:=0) -> write('No data');write('========\n'),printlist(List)), start_game().
  92. add_new_word():- write('Please enter a word: \n'), readln([H|T]), assert(add_word(H)),start_game().
  93. 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().
  94. write_database_file():- get_word_list(List), length(List, Len), ((Len =:= 0) -> write('No data !!!'),create_new_file();
  95. 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().
  96.  
  97. file_utils(List,'y'):- create_new_file(),write_data(List,Char_Code),!.
  98. file_utils(List,'n'):- write_data(List,Char_Code),!.
  99. file_utils(List,_):- write('Invalid Option try again\n '),!.
  100.  
  101. guess_word():- init_play().
  102. end_game():- write('Game Over'), !.
  103.  
  104. game('r'):- read_data_from_file(),!.
  105. game('l'):- list_knowledge_base(),!.
  106. game('a'):- add_new_word(),!.
  107. game('d'):- delete_word(),!.
  108. game('w'):- write_database_file(),!.
  109. game('g'):- guess_word(),!.
  110. game('e'):- end_game().
  111. game(_):- write('Invalid Chracter \n'), start_game().
  112.  
  113. 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'),
  114. nl,get_single_char(X), char_code(Char_Code,X),game(Char_Code).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement