Guess my favourite colour!

3 次查看(过去 30 天)
Sohail Nadeem
Sohail Nadeem 2019-2-27
回答: Stephen23 2019-4-2
I want to make a game where the user has to guess my favourite colour
They will get 3 attempts and if they fail to guess the right colour the game will end.
So far i have the basics on how to make this for numbers. I know i can choose the answer as let say 4 and if the user says anything higher or lower theyre wrong. If they say 4 theyre correct and the game has ended. I dont know how to convert this to colours, could i assign each colour a number?
Could problems also arise if i code in the answer as 'black' and the user types 'Black', would matlab recognise these as different?

回答(2 个)

Bob Thompson
Bob Thompson 2019-4-2
You are going to have several problems with doing any kind of comparison of the color answers to make them relate to numbers. This is simply because you have to find a relationship that already exists before you can do so. I recommend looking something up on google and declaring that that is what you are using in the rules of you game if you decide to go this way.
As for checking of something is correct or not in MATLAB, you can use strcmp, regexp, or just == to determine if an input text is equal to a predefined answer. I believe there are ways to check for upper and lower case conditions, but I haven't personally tried exploring them.
For a more detailed answer than that, feel free to post what you have so far for your code and we can work with it.
Also, I recommend looking over the File Exchange, as there is likely something similar to what you're trying to do available on there.
  1 个评论
Walter Roberson
Walter Roberson 2019-4-2
strcmpi(). Or strcmp() after using lower() or upper() on both parts.

请先登录,再进行评论。


Stephen23
Stephen23 2019-4-2
You will need to:
  • Decide on a colorspace to measure the color differences in: https://en.wikipedia.org/wiki/Color_difference . For a simple project like this CIELAB is probably adequate.
  • Decide how to encode the three dimensions (which most common colorspaces have) into feedback for the users, e.g. "My color is more blue", "My color is lighter". Using CIELAB, CAM02, DIN 99, or any similar colorspace makes this quite easy (just compare and give feedback for each dimension independently).
If you want to display the colors as well then you will need to obtain a list of color RGB values and their names: one easy solution is to download my FEX submssion colornames, which converts from RGB to names and from names to RGB:
It already handles colornames ignoring the character case and even the space characters (in most cases). Otherwise you can simply use strcmpi to perform a case-insenstive match of character vectors / strings.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by