Bulls and Cows Game, also known as 4-digits Game, is an old code-breaking mind or paper and pencil game for one, two or more players, predating the commercially marketed board game Mastermind. In that tutorial, you are going to learn how to create a a Bulls and Cows Game in Java with a Swing User Interface.
You can also discover this tutorial in video on YouTube :
Generating a 4-digits number with no duplicates
First step is to generate a 4-digits number with no duplicates. Each digit must be unique. So, our number will be between 1023 and 9876. For that, we use nextInt method of the Random standard Java object. To be sure there is no duplicate in our generated number, we need to define a specific method iterating on each digit. A smart way to make that is to use the modulo operator and the classical divide operator :
Counting the Bulls and the Cows
When the user will enter a number, we will need to count the Bulls and the Cows for that number compared to the number to find. In our game, Bulls are correct digits well placed and Cows are correct digits bad placed. So, we define a bullsAndCows method taking in parameter a number and returning a tuple with Bulls and Cows compared to the number to find.
For that, we convert the entered number and the number to find in Strings. Then, we iterate on each char of the entered number converted to String. When we have the same char at the same position, we increment the Bulls counter. And when we have a char contained in the number to find but not at the same position, we increment the Cows counter.
At the end of the method, we have just to return the tuple with Bulls and Cows counters :
Building the core of Bulls and Cows Game
The core of our Bulls and Cows Game will be quite simple. The user will enter a number. Then, we will display the number of Bulls and Cows to the screen. If the number of Bulls is equal to 4, the user won. Otherwise, he must continue to play by entering a new number. We will record the number of guesses before the user find the solution to display him at the end of the game.
So, the core of our Bulls and Cows game is defined in a okClick method which will be used in the ActionListener of the OK button of our User Interface built with the Swing API.
First, we get the number entered by the user by calling the getText method of the JTextField component used in the UI. Then, we check if it is an integer. If it’s not an integer, we display an error message to the user thanks to the showMessageDialog static method of the JOptionPane class. The next step is to check if the number entered has duplicates by calling our hasDuplicates method defined previously. We check also that the number has well 4-digits. If not, we display an error message to the user.
If all is valid with the entered number, we increment the number of guesses counter and we calculate the Bulls and Cows for this number. If Bulls counter is equal to 4, the game is ended and the user won. We display it a congratulations message with the number of guesses before he found the correct number. Otherwise, we display the number of Bulls and Cows in the JTextPane of our UI. The game continues and the user must enter an other number to try to find our number.
The game continues like that until the user find the 4-digits. It gives us the following okClick method :
Building the UI of Bulls and Cows Game
Last step is to build the User Interface of our Bulls and Cows Game. It will be made with the Swing API. We will define simple components :
- a JFrame with a title
- a JPanel to display buttons, label and text field
- a JTextPane nested in a JScrollPane
We dispose the UI components with the default BorderLayout which is relevant for our simple needs. It gives us the following code for the play method :
Like you can see in this code, we add ActionListener on the OK and the New Game buttons. In these callbacks, we will call respectively the okClick and the init methods. In the init method, we reset all the properties to restart the game and we generate a new number.
Complete code of our Bulls and Cows Game
The complete code of our Bulls and Cows Game is below :
Bulls and Cows Game in Action!
Now, it’s time to put our Bulls and Cows Game in Action. When the game is launched, you should have the following windows at the center of your screen :
Like you can see, I found the solution after 11 guesses. Now, it’s your turn. Try to beat me !
To discover more tutorials, don’t hesitate to visit the SSaurel’s Channel on YouTube :
This channel will offer you tutorials on Android, Web Development and some others programming tips. Some tutorials and…
If you want to discover some books to learn Java programming, I advice you to read the following article with my selection of the Top 6 Best Books for Java programming :