Hangman Game in SWI Prolog

前端 未结 2 1807
庸人自扰
庸人自扰 2021-01-16 06:59

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:

相关标签:
2条回答
  • 2021-01-16 07:22

    maplist/3 & maplist/4 apply their first argument (a predicate of appropriate arity) against all elements of other arguments lists, then your makeBlanks could be:

    makeBlanks(AnsCodes, BlankCodes) :-
      maplist(answer_blank, AnsCodes, BlankCodes).
    
    answer_blank(Ans, Blank) :-
      Ans == 0'_ -> Blank = Ans ; Blank = 0'* .
    

    and substitute:

    substitute(AnsCodes, BlankCodes, GuessName, NewBlanks) :-
         maplist(place_guess(GuessName), AnsCodes, BlankCodes, NewBlanks).
    
    place_guess(Guess, Ans, Blank, Display) :-
        Guess == Ans -> Display = Ans ; Display = Blank.
    

    edit:

    on additional requests: 1) can be solved with an additional predicate:

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

    while regards 2) getGuess and processGuess together make a loop, that will just terminate when no more calls happen. Remove the last rule of checkWin, add an argument as counter to keep track of failed guesses, and extend processGuess to signal failure:

    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)
      ).
    
    0 讨论(0)
  • 2021-01-16 07:34

    Why so many cuts? Check out SWI library predicates that may be useful to you: memberchk/2, format/2 and nth1/3.

    0 讨论(0)
提交回复
热议问题