0

I am new in Java and for the moment basic with methods, classes and constructors. For practice I am trying to write a basic game (safecracker). So my logic is get a 3 digit random number and try to guess it.

private static int takeRandomSafeCode(int min, int max) {
    Random random = new Random();
    int result = random.nextInt(max - min) + min;
    return result;  

 private static void playGame() {
    int safeCode = takeRandomSafeCode(100, 999);
    int guess = takeGuess();

These are my random number methods. But if player guess a number and first digit is correct but on a wrong place I want to say "1 digit is correct but on a wrong position" or if one digit is correct "1 digit is correct and correct position"

I need to use here if-else statement i guess but I get my numbers int variable. What is the way of checking numbers one by one? Do I need to use a String? I am a little bit lost at this point. I would appreciate with your help.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • Yes, you parse your "result" number in String and try to find guess numbers in it. – DamCx Apr 16 '18 at 07:49
  • 1
    you can also use the mod operator % , like here: https://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number – Lior Alon Apr 16 '18 at 07:50
  • Give us some code where you try to check and we can try to help – Veselin Davidov Apr 16 '18 at 07:52
  • You want your user to guess *human readable digits*. But computers work with *binary numbers*. So the best approach might be to convert your number to guess into a *string representation* and work with that. – Timothy Truckle Apr 16 '18 at 07:52

2 Answers2

1

It may be preferable - and result in simpler code - if you generate an integer array with three elements. Logically, the safe code 1-2-3 is not one hundred and twenty three but actually 1 followed by 2 followed by 3.

private static int takeRandomDigit() {
    Random random = new Random();
    int result = random.nextInt(10);
    return result;
}

private static void playGame() {
    int[] safeCode = {takeRandomDigit(), takeRandomDigit(), takeRandomDigit()};
    int guess = takeGuess();

    for (int safeDigit : safeCode) // for each digit in the safe code
    {
        // if the digit matches the guess, do something
    }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
0

You can use some math to take the numbers for example if your number is: d=253

d % 10 -> gives you 3
d / 10 % 10 -> gives you 5
d / 100 -> gives you 2

Changing it to string is also an option because you can then access the different characters using the string functions like charAt etc.

But if you want to make it the Java and OOP way in order to learn you can make an object and add the 3 numbers as properties of that object and input some of the logic for checking there instead of doing some magic tricks like the above. For example:

class Combination {
  private  int firstPos;
  private  int secondPos;
  private  int thirdPos;

  public  Combination(){
    firstPos=random.nextInt(10);
    secondPos=random.nextInt(10);
    thirdPos=random.nextInt(10);
 }

 public boolean checkCombination(Combination testedCombination){
       .............
 }
 some methods, getter, setters etc
}
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • `checkCombination` is really just [`Object.equals`](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) if you think about it. – Michael Apr 16 '18 at 08:04
  • yes that's true especially with the boolean return type. I just wanted to give an example of how to do it OOP way instead of doing everything using mixing primitives and logic in one big method. If you just want to see if the combination matches equals would be nice but you might need other methods if you want to check for "correct number in the wrong spot" etc – Veselin Davidov Apr 16 '18 at 08:08
  • Do I need to do also three different int variable for user's guess Veselin? Or it is not necessary? – Eren Arican Apr 16 '18 at 08:42
  • You can use the same class for the user guess. It is again a "combination" just a different one but still consisting of 3 ints. But you will need to move from functional static programming into object oriented one which is not all static ;) – Veselin Davidov Apr 16 '18 at 08:54