7 kyu

Bulls and Cows

188 of 437a.2.c.4

Description:

In this kata you will try to recreate a simple code-breaking game. It is called "Bulls and Cows". The rules are quite simple:

The computer generates a secret number consisting of 4 distinct digits. Then the player, in 8 turns, tries to guess the number. As a result he receives from the computer the number of matches. If the matching digits are in their right positions, they are "bulls", if in different positions, they are "cows".

To implement this you will use:

1)a constructor (int) - initiates the game, receives a number and then sets it as the secret number.

2)and a function "compare with (int)" - compares the given and the secret numbers and then returns a String formated as "X bull/bulls and Y cow/cows".

However, there are some notes:

1)if the given number matches the secret number instead of returning "4 bulls and 0 cows", return "You win!". Any next attempts to play the game (even with invalid numbers) should return "You already won!"

2)if the amount of turns in this game is more than 8 (invalid turns are not counted) the returned String should be "Sorry, you're out of turns!".

3)After checking the turns you should validate the given number. If it does not correspond to the conditions you should throw an exception :

IllegalArgumentException 
ValueError```

E.g.:
``` java
Game starts with the secret number 9041

compare with : 8091
result : "2 bulls and 1 cow" //The bulls are "0" and "1", the cow is "9"

compare with : -15555
result : Exception //A number should be positive and contain 4 distinct digits. 
                   //This turn is not counted
                   
compare with : 8237
result : "0 bulls and 0 cows"

compare with : 9041
result : "You win!"

//new comparations (even with invalid numbers) will result in : "You already won!"
//the same logic applies to being out of turns
Game starts with the secret number 9041

compare with : 8091
result : "2 bulls and 1 cow" # The bulls are "0" and "1", the cow is "9"

compare with : -15555
result : Exception # A number should be positive and contain 4 distinct digits. 
                   # This turn is not counted
                   
compare with : 8237
result : "0 bulls and 0 cows"

compare with : 9041
result : "You win!"

# new comparations (even with invalid numbers) will result in : "You already won!"
# the same logic applies to being out of turns
Games
Fundamentals
Strings
Object-oriented Programming

Similar Kata:

More By Author:

Check out these other kata created by a.2.c.4

Stats:

CreatedNov 6, 2018
PublishedNov 13, 2018
Warriors Trained1512
Total Skips18
Total Code Submissions4097
Total Times Completed437
Java Completions188
Python Completions258
Total Stars30
% of votes with a positive feedback rating84% of 126
Total "Very Satisfied" Votes94
Total "Somewhat Satisfied" Votes24
Total "Not Satisfied" Votes8
Total Rank Assessments4
Average Assessed Rank
7 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • a.2.c.4 Avatar
  • FArekkusu Avatar
  • cliffstamp Avatar
Ad