Creating a program that guesses the colours( not a colour)??
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Natalia Wong
il 5 Dic 2015
Commentato: Image Analyst
il 6 Dic 2015
I am currently stuck at task 4 at checking how many colours the user entered are correct, and the colour and position are correct! I've tried so hard. Please don't say that I did not make an attempt. And is my false condition correct for breaking the loop? Thanks I am new to MATLAB.
%Written by:
% Student ID:
%ver 01, 8 Dec by JL
% Initiate Random Color Sequence
% r-red; b-blue; y-yellow; g-green; w-white; p-purple; o-orange;
all_colors = 'rbygwpo';
index = randperm(7);
hidden_seq = all_colors(index(1:5));
count = 0;
while 1
%Prompting user to enter input
result = input('Guess the 5 characters colour sequence: ');
count = count + 1;
%Validate user input
if numel(result) > 5 || numel(result) < 5
error('You have entered more or less than 5 characters')
end
%Check the user guesses
% 1. How many guesses has correct colors and position?
% 2. How many guesses has correct colors?
result == hidden_seq
end
0 Commenti
Risposta accettata
Image Analyst
il 5 Dic 2015
Close, but you need to sum your comparison to get the count of the number in the right location, and you can use ismember() to find the number of correct colors even if they're in the wrong location:
%Written by:
% Student ID:
%ver 01, 8 Dec by JL
% Initiate Random Color Sequence
% r-red; b-blue; y-yellow; g-green; w-white; p-purple; o-orange;
all_colors = 'rbygwpo';
index = randperm(7);
hidden_seq = all_colors(index(1:5))
count = 0;
while 1
%Prompting user to enter input
result = input('Guess the 5 characters colour sequence (Enter to quit): ', 's');
if isempty(result)
break;
end
count = count + 1;
% Get rid of any spaces they may have entered
result(result == ' ') = [];
%Validate user input
if numel(result) > 5 || numel(result) < 5
uiwait(warndlg('You have entered more or less than 5 characters'));
continue; % Skip to bottom of loop
end
% Check the user guesses
matches = result == hidden_seq
% 1. How many guesses has correct colors and position?
numColorsInCorrectLocation = sum(.........;
fprintf('%d of those are in the correct location\n', numColorsInCorrectLocation);
% 2. How many guesses are the correct colors (regardless of location)?
ia = ismember(unique(result), hidden_seq)
numColorMatches = sum(.....
end
5 Commenti
Image Analyst
il 6 Dic 2015
Don't use an apostrophe before the 5 and you need an "end" for the "if" and you need a %s in the format string if you're going to print out the hidden_seq variable.
if numColorsInCorrectLocation = 5
fprintf( 'Congratulation! You got the correct colour sequence: %s\n', hidden_seq);
end
Più risposte (1)
Steven Lord
il 5 Dic 2015
Since this is an assignment, I'll just give a hint. If you remove the exact matches from both (a copy of) the guess and (a copy of) the right answer and have 2 blue, 1 red, and 1 yellow left in your guess and 1 blue, 2 red, and 1 green left in your correct answer, how many "right color, wrong location" entries do you have? How did you figure that out? Can you automate that process?
0 Commenti
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!