This is my first beginner-level project. I’ve done my best with my current knowledge, and I’d appreciate any feedback or guidance from more experienced members.
#Number guessing game
52 messages · Page 1 of 1 (latest)
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;
}
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?
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
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
the second line inside the brackets starts 1 character earlier than the other lines
ohh
okay
i will change it
thanks for the feedback
i will surly make necessary changes
awesome
i did some changes. how are they?
it looks good
I think you forgot to address std::random_device
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
I mean your way works too but 0 or -1 might be simpler
I think you also use -1 for something else
the playGame funtion returns -1 if the user fails to guess the number
yeah
is there anything else?
do you have C++20?
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;
}
yesss, fancy