In Prolog Write the recursive predicate: allgreen(LIST) that checks if the player has received "all green". Write a recu

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

In Prolog Write the recursive predicate: allgreen(LIST) that checks if the player has received "all green". Write a recu

Post by answerhappygod »

In Prolog
Write the recursive predicate: allgreen(LIST) that checks if the
player has received "all green".
Write a recursive predicate: charbyindex(INDEX,LIST,ELEMENT)
that returns the ELEMENT in position INDEX in the given LIST.
The recursive predicate
do_guess(TARGETLIST,GUESSLIST,RESPONSELIST) builds the response
list (e.g. [’g’,’y’,’b’,’g’,’g’]). The code is shown below, but the
first two rules are missing:
wordle :- write('Enter puzzle number: '),
read(PUZNO),
write('Turn 1 - Enter your guess: '),
read(GUESS),
process(GUESS,PUZNO,1).

wordle(TURN,PUZNO) :- TURN == 7,
target(PUZNO,WORD),
write('Sorry! - The word was '),
write(WORD), nl,
process(stop, 0, TURN).

wordle(TURN,PUZNO) :- write('Turn '),
write(TURN), write(' - Enter your guess: '),
read(GUESS),
process(GUESS,PUZNO,TURN).

process(stop,_,_) :- !.
process(GUESS,PUZNO,_) :- wordle_guess(PUZNO,GUESS,RESULT),
allgreen(RESULT),
write(RESULT),nl, write('Got it!'), nl, !.

process(GUESS,PUZNO,TURN) :- string_chars(GUESS, GLIST),
length(GLIST,LEN), LEN =\= 5,
write('Invalid - guess must be 5 characters long!'), nl, !,
wordle(TURN,PUZNO).

process(GUESS,PUZNO,TURN) :- string_chars(GUESS, GLIST),
not(no_dups(GLIST)),
write('Invalid - guess must no duplicates!'), nl, !,
wordle(TURN,PUZNO).

process(GUESS,PUZNO,TURN) :-
wordle_guess(PUZNO,GUESS,RESULT),
write(RESULT),nl, NEXTTURN is TURN+1,
wordle(NEXTTURN,PUZNO).

wordle_guess( PUZNO, GUESS , RESULT ) :-
wordle_target(PUZNO, TLIST),
string_chars(GUESS, GLIST),
do_guess(TLIST, GLIST, RESULT).

wordle_target(PUZNO, LIST) :- target(PUZNO,WORD),
string_chars( WORD, LIST ).
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply