#Number guessing game

52 messages · Page 1 of 1 (latest)

tidal badge
rose axle
#
unsigned short getMaxAttempts(Difficulty mode)
{
	switch (mode)
	{
	case Difficulty::Easy:
		return 10;
	case Difficulty::Medium:
		return 7;
	case Difficulty::Hard:
		return 5;
	}

	return 10;
}
``` the switch handles all possible enum values, so it might make more sense to either use `std::unreachable();` at the end to show that all cases have indeed been handled, or make `10` be the default case, like ```cpp
switch (mode)
{
case Difficulty::Medium:
    return 7;
case Difficulty::Hard:
    return 5;
default:
    return 10;
}
tidal badge
#

hmm.. sounds good

#

thanks for the feedback

rose axle
#
unsigned short generateRandom(unsigned short max)
{
	static std::random_device entropy;
	static std::mt19937 engine(entropy());
	std::uniform_int_distribution <unsigned short> dist(1, max);
	return dist(engine);
}
``` you don't need to store `entropy`, you only use it one time. Could do ```cpp
static std::mt19937 engine(std::random_device()());
#

and dist could be in-line too, if you want: ```cpp
return std::uniform_int_distribution<unsigned short>(1, max)(engine);

#

why do you use unsigned short rather than something more common like int?

tidal badge
#

cuz the random number generated is going to be 1 to 100 so i thought for a such a short range why should i use int. i can use smaller data type like short

rose axle
#

I mean, sure, but it doesn't make any difference

#

just looks strange imo

#

formatting is wonky here

#

otherwise I think your code is fine

#

good exercise

tidal badge
#

okay. but i dont get it what you mean by wonky

#

should i simplify it more?

rose axle
#

the second line inside the brackets starts 1 character earlier than the other lines

tidal badge
#

ohh

#

okay

#

i will change it

#

thanks for the feedback

#

i will surly make necessary changes

rose axle
#

awesome

tidal badge
rose axle
#

I think you forgot to address std::random_device

tidal badge
#

ohh yeah

#

i will fix it

#

i forgot

rose axle
#

also, question

#
// Track best (fewest) attempts per difficulty; initialize to max int
	int best_easy = std::numeric_limits<int>::max();
	int best_medium = std::numeric_limits<int>::max();
	int best_hard = std::numeric_limits<int>::max();
#

why numeric_limits::max() instead of something invalid like -1?

#

or 0, since you always take at least 1 attempt to guess a number

tidal badge
#

hmm you are right

#

i will do that as well

rose axle
#

I mean your way works too but 0 or -1 might be simpler

#

I think you also use -1 for something else

tidal badge
#

the playGame funtion returns -1 if the user fails to guess the number

rose axle
#

yeah

tidal badge
#

is there anything else?

rose axle
#

do you have C++20?

tidal badge
#

yes its c++20

rose axle
#

nice, then there's something cool in the standard library

#

instead of having your own GuessResult/evaluateGuess, you could include <compare> which allows you to do this: ```cpp
switch (guess <=> target) {
case std::strong_ordering::less:
std::cout << "Try higher.\n";
break;
case std::strong_ordering::greater:
std::cout << "Try lower.\n";
break;
case std::strong_ordering::equal:
// ...
return attempts;
}

tidal badge
#

wow

#

three way comparison

rose axle
#

yesss, fancy

tidal badge
#

thats some cool stuff

#

i will definitely try it out and play around with it

#

thanks