Given a float, how to map it to an enum like
{FAIL, POOR, OK, GOOD, EXCELLENT, PERFECT}
if the divisions aren’t even.
0.0-0.4 is FAIL
0.4-0.6 is POOR
…
0.8-0.999.. is EXCELLENT
1.0 is PERFECT
The float is a rating value calculated from all the played levels in a game. It ranges from 0..1, both inclusive. There are normally no more than about 10 divisions needed, but the spacings are subject to tuning during development.
I’m currently using a stack of if..else statements. Is that the right way to do it? It seems a bit brittle.
Use an array of structs – either statically allocated or dynamic – and then a simple routine to search it – if its small just iterate, if its large you can do binary search.
As you know the minimum (0.0) and maximum (1.0) you only need to store the upper-bound of the range and enum value. E.g:
Now
sizeof(Divisions)/sizeof(RatingDivision)will tell you the number of entries (needed for binary search), or just iterate until the value you’re looking for is<= Divisions[i].upperBoundreturningDivisions[i].scoreor theupperBoundreaches1.0with no match and handle the error.