Creating a program that guesses the colours( not a colour)??

4 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 5 Dec 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 Comments
Natalia Wong
Natalia Wong on 6 Dec 2015
%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 all correct 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(matches);
fprintf('There are %d correct guess(es) of color and position.\n', numColorsInCorrectLocation);
% 2. How many guesses are the correct colors (regardless of location)?
ia = ismember(unique(result), hidden_seq)
numColorMatches = sum(ia);
fprintf('There are %d correct guess(es) of color only.\n',numColorMatches)
if numColorsInCorrectLocation = '5
fprintf( 'Congratulation! You got the correct colour sequence: \n', hidden_seq)
end now i'm having problems with the last few lines of my program can you pls help me out thanks Image Analyst
Image Analyst
Image Analyst on 6 Dec 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

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 5 Dec 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?

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!