-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordleProject
82 lines (63 loc) · 3.01 KB
/
WordleProject
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
function [] = A10Prob3_wordle_abathin()
%% ____________________
%% INITIALIZATION
%Set game output up and space for each guess
gameOutput = '-----';
userGuess = ' ';
%Set initial guess count and number of guesses permitted and while loop count
guessCount = 0;
max_guess = 6;
countIteration = 0;
% Call UDF to get 5 letter solution word from pre-defined dictionary
solution = wordle_dictionary(4004);
%% ____________________
%% CALCULATIONS
% Display instructions to user
disp('Enter a 5 Letter Word Guess! Try to guess the word in 6 guesses or less!');
% Loop until the max guesses is reached
while countIteration ~= max_guess && ~strcmp(solution, userGuess)
%Make sure user guess is 5 letters long
userGuess = input(sprintf('Guess %d: ', guessCount + 1), 's');
while length(userGuess) ~= 5
disp('Not a Valid Entry. Please Try Again!');
userGuess = input(sprintf('Guess %d: ', guessCount + 1), 's');
end
% Check if correct letters are in the right position
if strcmp(userGuess(1), solution(1))
gameOutput(1) = solution(1);
elseif strcmp(userGuess(1), solution(2)) || strcmp(userGuess(1), solution(3)) || strcmp(userGuess(1), solution(4)) || strcmp(userGuess(1), solution(5))
gameOutput(1) = '*';
end
if strcmp(userGuess(2), solution(2))
gameOutput(2) = solution(2);
elseif strcmp(userGuess(2), solution(1)) || strcmp(userGuess(2), solution(3)) || strcmp(userGuess(2), solution(4)) || strcmp(userGuess(2), solution(5))
gameOutput(2) = '*';
end
if strcmp(userGuess(3), solution(3))
gameOutput(3) = solution(3);
elseif strcmp(userGuess(3), solution(1)) || strcmp(userGuess(3), solution(2)) || strcmp(userGuess(3), solution(4)) || strcmp(userGuess(3), solution(5))
gameOutput(3) = '*';
end
if strcmp(userGuess(4), solution(4))
gameOutput(4) = solution(4);
elseif strcmp(userGuess(4), solution(1)) || strcmp(userGuess(4), solution(2)) || strcmp(userGuess(4), solution(3)) || strcmp(userGuess(4), solution(5))
gameOutput(4) = '*';
end
if strcmp(userGuess(5), solution(5))
gameOutput(5) = solution(5);
elseif strcmp(userGuess(5), solution(1)) || strcmp(userGuess(5), solution(2)) || strcmp(userGuess(5), solution(3)) || strcmp(userGuess(5), solution(4))
gameOutput(5) = '*';
end
% Display the guess' result and increment the guess and while loop count
disp(['Result ', num2str(guessCount+1), ': ', gameOutput]);
guessCount = guessCount + 1;
countIteration = countIteration + 1;
end
%% ____________________
%% FORMATTED TEXT/FIGURE DISPLAYS
%If wordle solved under max number of guesses, then display congrats
if strcmp(solution, userGuess)
disp(['Congrats! You solved this wordle in ', num2str(guessCount), ' attempts']);
else
fprintf('You could not solve this wordle in %d guesses. The word was %s.\n', max_guess, solution); %Otherwise, display correct word.
end