필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Natalia Wong
Natalia Wong 2015년 12월 5일
댓글: Image Analyst 2015년 12월 6일
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

채택된 답변

Image Analyst
Image Analyst 2015년 12월 5일
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
Natalia Wong
Natalia Wong 2015년 12월 6일
%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 2015년 12월 6일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2015년 12월 5일
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?

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by