English 中文(简体)
SWI Prolog 中的绞刑人游戏
原标题:Hangman Game in SWI Prolog

I m trying to make a simple hangman game in SWI Prolog.
Since we made this program run can you help me enchance the program with the following:

1>

2 最后,添加一个计数器,计数错误猜数的数量, 当到达某个数数时退出游戏。 程序应该告诉用户他们输了, 显示词句的真实性, 并终止。 重复猜数不应该算错 。

我要感谢所有帮助我的人,这对我意义重大。

我给你提供代码和评论。

% This top-level predicate runs the game.  It prints a 
% welcome message, picks a phrase, and calls getGuess.

% Ans = Answer
% AnsList = AnswerList

hangman:- 
    getPhrase(Ans), 
    !, 
    write( Welcome to hangman. ),
    nl,
    name(Ans,AnsList), 
    makeBlanks(AnsList, BlankList), 
    getGuess(AnsList,BlankList).

% Randomly returns a phrase from the list of possibilities.

getPhrase(Ans):-
    phrases(L), 
    length(L, X), 
    R is random(X), 
    N is R+1, 
    getNth(L, N, Ans).

% Possible phrases to guess.

phrases([ a_picture_is_worth_a_thousand_words , one_for_the_money , dead_or_alive , computer_science ]).

% Asks the user for a letter guess.  Starts by writing the 
% current "display phrase" with blanks, then asks for a guess and
% calls process on the guess.

getGuess(AnsList, BlankList):- 
    name(BlankName, BlankList), 
    write(BlankName), 
    nl,  
    write( Enter your guess, followed by a period and return. ), 
    nl, 
    read(Guess),
    !, 
    name(Guess, [GuessName]), 
    processGuess(AnsList,BlankList,GuessName).

% Process guess takes a list of codes representing the answer, a list of codes representing the current
% "display phrase" with blanks in it, and the code of the letter that was just guessed.  If the guess
% was right, call substitute to put the letter in the display phrase and check for a win.  Otherwise, just
% get another guess from the user.

processGuess(AnsList,BlankList,GuessName):- 
    member(GuessName,AnsList), 
    !,
    write( Correct! ),
    nl, 
    substitute(AnsList, BlankList, GuessName, NewBlanks), 
    checkWin(AnsList,NewBlanks).

processGuess(AnsList, BlankList,_):-
    write( Nope! ),
    nl,
    getGuess(AnsList, BlankList).

% Check to see if the phrase is guessed.  If so, write  You win  and if not, go back and get another guess.

checkWin(AnsList, BlankList):- 
    name(Ans, AnsList), 
    name(BlankName, BlankList), 
    BlankName = Ans, 
    !, 
    write( You win! ).

checkWin(AnsList, BlankList):- 
    !,
    getGuess(AnsList, BlankList).


% getNth(L,N,E) should be true when E is the Nth element of the list L. N will always
% be at least 1.

getNth([H|T],1,H).

getNth([H|T],N,E):-
    N1 is N-1,
    getNth(T,N1,E1),
    E=E1.

% makeBlanks(AnsList, BlankList) should take an answer phrase, which is a list
% of character codes that represent the answer phrase, and return a list
% where all codes but the  _  turn into the code for  * .  The underscores
% need to remain to show where the words start and end.  Please note that 
% both input and output lists for this predicate are lists of character codes.
% You can test your code with a query like this:
% testMakeBlanks:- name( csc_is_awesome , List), makeBlanks(List, BlankList), name(Towrite, BlankList), write(Towrite). 

makeBlanks(AnsCodes, BlankCodes) :-
  maplist(answer_blank, AnsCodes, BlankCodes).

answer_blank(Ans, Blank) :-
  Ans == 0 _ -> Blank = Ans ; Blank = 0 * .

% substitute(AnsList, BlankList, GuessName, NewBlanks) Takes character code lists AnsList and BlankList, 
% and GuessName, which is the character code for the guessed letter.  The NewBlanks should again be a 
% character code list, which puts all the guesses into the display word and keeps the * s and _ s otherwise.
% For example, if the answer is  csc_is_awesome  and the display is  c*c_**_*******  and the guess is  s , the 
% new display should be  csc_*s_***s*** .
% You can test your predicate with a query like this:
% testSubstitute:- name( csc_is_awesome , AnsList), name( c*c_**_******* , BlankList), name( s ,[GuessName]), substitute(AnsList, BlankList, GuessName, NewBlanks),
%    name(Towrite, NewBlanks), write(Towrite). 

% Also, since the predicate doesn t deal directly with character codes, this should also work:
% substitute([ c , s , c ],[ c , * , c ], s ,L).  L should be [ c , s , c ].

substitute(AnsCodes, BlankCodes, GuessName, NewBlanks) :-
     maplist(place_guess(GuessName), AnsCodes, BlankCodes, NewBlanks).

place_guess(Guess, Ans, Blank, Display) :-
    Guess == Ans -> Display = Ans ; Display = Blank.
最佳回答

< a href=" "http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%27A.2%27,swi%28%27/doc/Manual/ apply.html%27%29%29" rel=“nofolt”>mapplist /3/3 & amp; 地图list/4 对其它参数列表中的所有要素应用其第一个参数( 适当对等的前提), 那么您的 MacBlanks 可以是 :

makeBlanks(AnsCodes, BlankCodes) :-
  maplist(answer_blank, AnsCodes, BlankCodes).

answer_blank(Ans, Blank) :-
  Ans == 0 _ -> Blank = Ans ; Blank = 0 * .

替换:

substitute(AnsCodes, BlankCodes, GuessName, NewBlanks) :-
     maplist(place_guess(GuessName), AnsCodes, BlankCodes, NewBlanks).

place_guess(Guess, Ans, Blank, Display) :-
    Guess == Ans -> Display = Ans ; Display = Blank.

< 强度 > 编辑 :

(一) 可在以下额外前提下解决:

alreadyGuessed(Guess, AnsCodes) :-
   memberchk(Guess, AnsCodes).

时) getGuess processGuess 一起组成一个循环, 当不再使用电话时将终止循环。 删除 checkWin 的最后规则, 添加一个参数作为反向以跟踪失败的猜测, 并扩展进程Guess 以信号失败 :

processGuess(AnsList, BlankList, _, CountFailed) :-
  (   CountFailed == 5
  ->  format( Sorry, game over. You didn t guess (~s)~n , [AnsList])
  ;   write( Nope! ),
      CountFailed1 is CountFailed + 1,
      getGuess(AnsList, BlankList, CountFailed1)
  ).
问题回答

为何剪切这么多?看看SWI图书馆的上游, 可能对您有用: 成员chk/2, 格式2 和 nth1/3。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签