[Solved] Find out if guessed number is higher, lower or equal to Random number, or guessed before etc


Store your guesses in a list

private List<int> _guesses = new List<int>();

After calling MakeGuess(guess) add the guess to the list

_guesses.Add(guess);

Instead of the foreach loop do this

if (_guesses.Contains(guess)) {
    return outcome.PreviousGuess;
}

The last else can be simplified from

else if (Number > guess) {
    return Outcome.Low;
}

to

else {
    return Outcome.Low;
}

since this is the only possible case remaining.


I would simplify the method further

public Outcome MakeGuess(int guess)
{
    if (_guesses.Contains(guess)) {
        return outcome.PreviousGuess;
    }
    if (guess == Number) {
        return Outcome.Correct;
    }   
    if (guess > Number) {
        return Outcome.High;   
    }   
    return Outcome.Low;             }
}

The else keyword is not necessary because the return statements end the method execution if a case applies.

The error handling should not be necessary if you calculate the random numbers correctly. You should not trap programming errors with error handling; instead, correct the errors! Error handling makes sense when calling a method can lead to an exception. An example is opening a file that might not exist or might be locked or the like. In libraries used by other persons, it makes sense to check if the parameters passed to a method are correct and to throw an exception if not.

solved Find out if guessed number is higher, lower or equal to Random number, or guessed before etc