Kotlinlearncs.online LogoJava
Return to List

Test Writing: Guessing Game With Tries

Created By: Geoffrey Challen
/ Version: 2022.7.0

Let's complete a class to implement a guessing game. When the class is created we'll save a secret number and set a maximum number of tries. Then we'll provide a method allowing someone to guess the secret number, but limit the number of guesses appropriately.

Create a public class GuessingGame with a primary constructor accepting the secret value and the maximum number of tries, in that order, both as Int values. If the maximum number of tries is negative or zero, throw an IllegalArgumentException.

Provide a public method guess accepting a guess (as an Int) and returning a Boolean. If the guess matches the secret, return true; otherwise return false. If the number of tries is exceeded, or if the secret has already been successfully guess, throw an IllegalArgumentException. Your tries property should be publicly readable but not publicly writable. Do not expose any other object state publicly.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestGuessingGame with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods