Creating a program that guesses the colours( not a colour)??
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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
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?
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!