Contents

How to Control Result Values by Multiple Conditions (Dice Game 3, with.Java)

   Sep 2, 2023     11 min read

In this article, we learned about How to control a result by multiple conditions (Dice Game 3, with.Java)

We’re going to learn by solving the Coding Test problem, reflecting on the problem we solved, and exploring other ways to solve it. Let’s start with the problem.

Problem

You have four dice with the numbers 1 through 6 written on them. When you roll the four dice, you get the following number of points based on the numbers that come up.

If the numbers on all four dice are equal to p, you get 1111 × p points. If the numbers on three dice are equal to p and the number on the other die is q (p ≠ q), you get (10 × p + q)2 points. If two dice come up with the same value and the numbers are p and q (p ≠ q), respectively, you get (p + q) × |p - q| points. If the numbers on any two dice are the same, p, and the numbers on the other two dice are different, q, r (q ≠ r), you get q × r points. If the numbers on all four dice are different, you score as many points as the smallest of the numbers that came up. Write a solution function that returns the number of points you get when you roll the four dice, given the integer parameters a, b, c, and d.

Example input and output

my_string: “cvsgiorszzzmrpaqpe” index_list: [16, 6, 5, 3, 12, 14, 11, 11, 17, 12, 7] result: “programmers”

In my_string in Example 1, the letters corresponding to indexes 3, 5, 6, 11, 12, 14, 16, and 17 are g, o, r, m, r, a, p, and e, respectively, so the letters of the indexes corresponding to the elements in index_list in my_string are p, r, o, g, r, a, m, m, m, e, r, and s, respectively, in that order. Therefore, it returns “programmers”.

#abcdresult     
12222222222   
2414441681    
36336273362727
42526301    
56425252   

In 1, all four dice numbers are equal to 2, so we get a score of 1111 × 2 = 2222. Therefore, we return 2222.

In number 2, the numbers on three dice are equal to 4 and the number on the other die is 1, so we get (10 × 4 + 1)2 = 412 = 1681 points. Therefore, we return 1681.

In number 3, A and D are equal to 6 and B and C are equal to 3, so we get (6 + 3) ×6 - 3= 9 × 3 = 27 points. Therefore, return 27.

In question 4, two of the dice came up with a 2, and the other two dice came up with a 5 and a 6, respectively, so we get 5 × 6 = 30 points. Therefore, return 30.

In number 5, all four dice have different numbers and the smallest number is 2, so we get 2 points. So return 2.

My solution to the problem

class Solution {
    public int solution(int a, int b, int c, int d) {
        int answer = 0;

        if(a == b && b == c && c == d){
            answer = saveAnswer(a,a,a,a,0);
        }

        if(a == b && b == c && a != d){
            answer = saveAnswer(a,d,a,1);
        } else if(a == b && b == d && a != c){
            answer = saveAnswer(a,c,a,1);
        } else if(a == c && c == d && a != b){
            answer = saveAnswer(a,b,a,1);
        } else if(b == c && c == d && b != a){
            answer = saveAnswer(b,a,a,a,1);
        }

        if(a == b && a != c && c == d){
            answer = saveAnswer(a,c,a,2);
        } else if(a == c && a != b && b == d){
            answer = saveAnswer(a,b,a,2);
        } else if(a == d && a != b && b == c){
            answer = saveAnswer(a,b,a,2);
        }

        if(a == b && a != c && a != d && c != d){
            answer = saveAnswer(a,c,d,3);
        } else if(a == c && a != b && a != d && b != d){
            answer = saveAnswer(a,b,d,3);
        } else if(a == d && a != b && a != c && b != c){
            answer = saveAnswer(a,b,c,3);
        } else if(b == c && b != a && b != d && a != d){
            answer = saveAnswer(a,a,d,3);
        } else if(b == d && b != a && b != c && a != c){
            answer = saveAnswer(a,a,c,3);
        } else if(c == d && c != a && c != b && a != b){
            answer = saveAnswer(a,a,b,3);
        }

        if(a != b && b != c && c != d && a != c && a != d && b != d){
            if(a < b && a < c && a < d){
                answer = a;
            } else if(b < c && b < d){
                answer = b;
            } else if(c < d){
                answer = c;
            } else {
                answer = d;
            }
        }
        } return answer;
    }
    private int saveAnswer(int p, int q, int r, int cases){
        int result = 0;
        if(cases == 0){
            result = 1111 * p;
        } else if(cases == 1){
            result = (int)Math.pow(10 * p + q, 2);
        } else if(cases == 2){
            result = (p + q) * Math.abs(p - q);
        } else if(cases == 3){
            result = q * r;
        }
        } return result;
    }
}
Solution Explanation

We’ve created a saveAnswer function that allows us to derive a fluid value for each case. We’ve then set up the logic so that, depending on the conditions of the problem, we can put argument values into that function and control it based on the value of the case.

In my case, I solved the problem by responding to all cases, but there’s a better way. It’s to solve the problem by sorting the number of dice by size. I’ll show you the sorted solution first and then continue with the review.

Solve the problem by sorting by size for the number of dice

import java.util.Arrays;

class Solution {
    public int solution(int a, int b, int c, int d) {

        int[] dice = { a, b, c, d };
        Arrays.sort(dice);

        int ans = 0;

        if (dice[0] == dice[3]) {
            ans = 1111 * dice[3];
        } else if (dice[0] == dice[2] || dice[1] == dice[3]) {
            ans = (int) Math.pow(dice[1] * 10 + (dice[0] + dice[3] - dice[1]), 2);
        } else if (dice[0] == dice[1] && dice[2] == dice[3]) {
            ans = (dice[0] + dice[3]) * (dice[3] - dice[0]);
        } else if (dice[0] == dice[1]) {
            ans = dice[2] * dice[3];
        } else if (dice[1] == dice[2]) {
            ans = dice[0] * dice[3];
        } else if (dice[2] == dice[3]) {
            ans = dice[0] * dice[1];
        } else {
            ans = dice[0];
        }

        return ans;
    }
}

Declare an int[] called dice and put the dice values in it, and use Arrays.sort to sort the values in the dice element. Then I write the code to match the logic. If you look at it, you’ll notice that there are fewer conditional statements compared to my solution. Because I didn’t sort it, I was comparing the values of all a,b,c,d to categorize the cases, whereas if I had sorted it, it would be more concise and readable because I wouldn’t have to compare all the dice values.

Conclusion

In the future, when you need to categorize values based on certain conditions and you don’t know what values will come in, you can write much more readable code if you approach the problem with sorting.