# [project display] a little difficult number guessing game (written in Java)

Posted by Sevengraff on Sat, 19 Feb 2022 15:30:12 +0100

# 1. Foreword

I have uploaded some C language programs compiled in my undergraduate course and recently improved again in my blog. It has been some time. Good guy, if I don't do some Java, I really think my main direction is C language!
So today, I will bring a project written in Java, which is a number guessing game brought by PHS that I played when I was a child (about 2007).
When it comes to guessing numbers, what do comrades think of first? Is the system generating a random number for users to guess, and then the system will tell you the guessing game of "big" and "small"? Please, this is too simple! In addition to inspiring binary search thinking, the exercise of logical reasoning ability can be said to be useless.
The guessing numbers I brought here are not like this. Please rest assured. This figure is still a little difficult for ordinary people below the master. It will also give hints, but it will not remind you that you are big or small. It will only tell you how correct you are. What do you mean? See you in the specifications. In short, when I was a child, I especially liked to guess numbers. The main reason is that there is a very cute little frog on its interface. Every time I play, it always reminds me of the big eyed frog adventure I played on the little overlord game console when I was younger. Most of the time I can't guess the numbers. Occasionally I can guess them accurately. If you guess right, the little frog will jump happily and say you are "extremely smart".
In this programming, I also tried to restore the original appearance of childhood mobile games, including the format of tips and the slogan when guessing correctly. As long as I can remember, I will try my best to restore it, so as to pay tribute to the classics.

# 2. Specification

[external specification]
The system randomly generates a non repeated four digit number, and the user also guesses a non repeated four digit number.
The system will compare the number you want with the number you guessed, and tell you how correct it is. If the user has a number and guesses both the value and the position correctly, the number will be counted as a. If the user guesses the number correctly but does not guess the position correctly, the count is B. The user has a total of 10 opportunities. Each time he guesses, the system will tell the user how many a and B there are. Guess until 4A0B, or all 10 opportunities are exhausted, and the system will announce the answer.

```Running example
(The number generated by the system is: 1295)←Of course, this sentence will not be displayed when the program actually runs
I've thought of a number. What do you think it is? one thousand two hundred and thirty-four
2A0B
I've thought of a number. What do you think it is? five thousand six hundred and seventy-eight
0A1B
I've thought of a number. What do you think it is? one thousand two hundred and seventy-eight
2A0B
I've thought of a number. What do you think it is? one thousand two hundred and fifty
2A1B
I've thought of a number. What do you think it is? one thousand two hundred and fifty-nine
2A2B
I've thought of a number. What do you think it is? one thousand two hundred and ninety-five
4A0B

♬clever☆
Top♪
```

[internal specification]
Development language: Java
Number of class files: 2
Total lines of code: 71 lines of Test class + 29 lines of Rule class = 100 lines
Total number of methods: 1 Test class (main method) + 7 Rule classes = 8

# 3. Source code

Rule.java

```public class Rule{
private int digit;

public Rule(int digit){this.digit = digit;}

public int hitCounter(int[] num, int[] guess){
int hit = 0;
for(int i = 0; i < this.digit; i++)
if(num[i] == guess[i])	hit++;
return hit;
}

public int blowCounter(int[] num, int[] guess){
int blow = 0;
for(int i = 0; i < this.digit; i++)
for(int j = 0; j < this.digit; j++)
if(guess[i]==num[j] && i!=j) blow++;
return blow;
}

public int getThousands(int num){return num / 1000;}

public int getHundreds(int num){return num / 100 % 10;}

public int getTens(int num){return num / 10 % 10;}

public int getOnes(int num){return num % 10;}

}
```

Test.java

```import java.util.*;
public class Test{
static final int DIGIT = 4;

public static void main(String[] args){
Random rand = new Random();
Scanner input = new Scanner(System.in);
Rule judge = new Rule(DIGIT);

int[] number = new int[DIGIT];
int[] guess = new int[DIGIT];
int i = 0, j;
int chance = 10, userInput = 0;
int hit, blow;
boolean isSame = false;

//Generate four non repeating digits
do{
isSame = false;
number[i] = rand.nextInt(10);
for(j = 0; j < i; j++){
if(number[i] == number[j]){
isSame = true;
break;
}
}
if(isSame) continue;
else i++;
}while(i<DIGIT);

while(true){
System.out.print("I've thought of a number. What do you think it is?");
userInput = input.nextInt();
if(userInput > 9999){
System.out.println("Wrong number! Four digits should be entered.");
continue;
}
guess[0] = judge.getThousands(userInput);
guess[1] = judge.getHundreds(userInput);
guess[2] = judge.getTens(userInput);
guess[3] = judge.getOnes(userInput);
isSame = false;
for(i = 0; i < DIGIT; i++){
for(j = 0; j < i; j++){
if(guess[i] == guess[j]){
System.out.println("Duplicate numbers cannot be entered! Please re-enter.");
isSame = true;
break;
}
}
if(isSame) break;
}
if(isSame) continue;
else chance--;

hit = judge.hitCounter(number, guess);
blow = judge.blowCounter(number, guess);
System.out.printf("%dA%dB\n", hit, blow);

if(hit == 4){
System.out.println("♬clever☆\n\t Top♪");
break;
}else{
if(chance == 0){
System.exit(0);
}
}
}
}
}

```

# 4. Operation results

I've thought of a number. What do you think it is? one thousand and twenty-four
0A1B
I've thought of a number. What do you think it is? one thousand two hundred and thirty-four
0A0B
I've thought of a number. What do you think it is? five thousand six hundred and seventy-eight
1A2B
I've thought of a number. What do you think it is? five thousand six hundred and seventy
1A2B
I've thought of a number. What do you think it is? five thousand six hundred and eight
1A2B
I've thought of a number. What do you think it is? five thousand and seventy-eight
0A3B
I've thought of a number. What do you think it is? 0678
2A2B
I've thought of a number. What do you think it is? 0687
4A0B
♬ smart ☆
♪ top ♪

# 5. Feelings

I don't have any technical experience, so let's talk about my feelings. It's a purely personal point of view. It's for reference only. Please take your own point of view as the standard.
① Some machines should be well preserved and should not be discarded because they are no longer in use. Because technology is developing rapidly now, maybe in a few years you want to be nostalgic. The kind of machine you want to be nostalgic has long been eliminated by the market and is no longer so easy to start.
② Do more small projects to practice Java OOP (object-oriented) programming. The most typical examples are games like this, management systems like libraries, classes, restaurants, etc.
Today's content is a little short. Let's stop here. Xiaohu, see you next time!