-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordle.pl
130 lines (118 loc) · 3.5 KB
/
Wordle.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
main:-
write('Welcome to Pro-Wordle!'), nl,
write('----------------------'), nl,
build_kb,
play.
build_kb:-
write('Please enter a word and its category on separate lines:'), nl,
read(W),
(
var(W),
write('You cannot enter variables, try again.'), nl,
build_kb
;
W = 'done',
write('Done building the words database...'), nl
;
read(C),
(
var(C),
write('You cannot enter variables, try again.'), nl,
build_kb
;
assert(word(W, C)),
build_kb
)
).
play:-
write('The available categories are: '),
categories(Categories),
write(Categories), nl,
choose_category(C),
choose_length(L, C),
Guesses is L + 1,
write('Game started. You have '), write(Guesses), write(' guesses.'), nl, nl,
setof(W, pick_word(W, L, C), Words),
random_member(ActualWord, Words),
guess_word(ActualWord, L, Guesses).
pick_word(W, L, C):-
word(W, C),
atom_length(W, L).
choose_category(C):-
write('Choose a category: '), nl,
read(T),
(
var(T),
write('You cannot enter variables, try again.'), nl,
choose_category(C)
;
is_category(T),
C = T
;
write('This category does not exist.'), nl,
choose_category(C)
).
choose_length(L, C):-
write('Choose a length: '), nl,
read(T),
(
var(T),
write('You cannot enter variables, try again.'), nl,
choose_length(L, C)
;
\+integer(T),
write('You must enter a number, try again.'), nl,
choose_length(L, C)
;
pick_word(_, T, C),
L = T
;
write('There are no words of this length. '), nl,
choose_length(L, C)
).
guess_word(ActualWord, RequiredLength, Guesses):-
write('Enter a word composed of '), write(RequiredLength), write(' letters:'), nl,
read(GuessWord),
(
var(GuessWord),
write('You cannot enter variables, try again.'), nl,
guess_word(ActualWord, RequiredLength, Guesses)
;
GuessWord = ActualWord,
write('You won!'), nl
;
Guesses = 1,
write('You lost!'), nl
;
(
atom_length(GuessWord, RequiredLength),
atom_chars(ActualWord, ActualLetters),
atom_chars(GuessWord, GuessLetters),
correct_letters(ActualLetters, GuessLetters, CorrectLetters),
correct_positions(ActualLetters, GuessLetters, CorrectPositions),
write('Correct letters are: '), write(CorrectLetters), nl,
write('Correct letters in correct positions are: '), write(CorrectPositions), nl,
NewGuesses is Guesses - 1
;
write('Word is not composed of '), write(RequiredLength), write(' letters. Try again.'), nl,
NewGuesses is Guesses
),
write('Remaining Guesses are '), write(NewGuesses), nl, nl,
guess_word(ActualWord, RequiredLength, NewGuesses)
).
correct_letters(ActualLetters, GuessLetters, CorrectLetters):-
intersection(GuessLetters, ActualLetters, CL),
list_to_set(CL, CorrectLetters).
correct_positions([], [], []).
correct_positions([H|T1], [H|T2], [H|T3]):-
correct_positions(T1, T2, T3).
correct_positions([H1|T1], [H2|T2], T3):-
H1 \= H2,
correct_positions(T1, T2, T3).
is_category(C):-
word(_, C).
categories(L):-
setof(X, is_category(X), L).
available_length(L):-
word(W, _),
atom_length(W, L).