Retired

Psychic Guesser for the guessing game (retired)

Description:

Introduction

A well-known introductory programming exercise is to write a guessing game, where the user should guess a random number between 0 and 1000, getting feedback in the form of "Your guess is too small" or "Your guess is too big".

Another well-known programming exercise is to write a guesser for said guessing game. The standard approach can guess the number in ten guesses or less.

In this exercise, you should write a guesser, but you need to always guess correctly on your first try.

Task

Implement the method public int MakeAGuess(int lastResult) that makes a guess for the guessing game. The parameter lastResult will be -1 if your last guess was too small, 1 if your last guess was too large and 0 on your first guess or if your last guess was correct, but you should not need it, as you have to be correct with your first guess!

The code for the guessing game is given as follows:

using System;

public sealed class GuessingGame
{
    private static readonly Random random = new Random();
    private const int MaxGuesses = 10;
    
    private readonly int secretNumber;
    
    public int Guesses { get; private set; }
    
    public GuessingGame() 
    {
        secretNumber = random.Next(0, 1000);
        Guesses = 0;
    }
    
    
    public int Guess(int number)
    {
        Guesses++;
        
        if (Guesses > MaxGuesses) throw new InvalidOperationException("Game over!");
        
        if (number == secretNumber)
        {
            Console.WriteLine("You guessed correctly!");
            return 0;
        }
        
        if (number <= secretNumber)
        {
            Console.WriteLine("Too small!");
            return -1;
        }
        else
        {
            Console.WriteLine("Too big!");
            return 1;
        }
    }
}

Credits

This kata is heavily inspired by the Java Kata Psychic.

Puzzles

Similar Kata:

Stats:

CreatedDec 9, 2018
Warriors Trained295
Total Skips35
Total Code Submissions366
Total Times Completed43
C# Completions43
Total Stars8
% of votes with a positive feedback rating91% of 17
Total "Very Satisfied" Votes14
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes0
Total Rank Assessments7
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • chribi Avatar
  • SpecialEd Avatar
Ad