package math.paradoksy;

import math.utils.MathUtil;

public class ParadUtil {
    public ParadUtil() {
    }

    public static int third(int one, int two) {
        int result = -1;
        for (var i = 1; i < 4; i++) {
            if ((i != one) && (i != two)) {
                result = i;
                break;
            }
        }
        return result;
    }

    public static int[] empty(int gate, int choice) {
        var len = 0;
        int[] array;
        if (gate == choice) {
            len = 2;
            array = new int[len];
            switch (gate) {
                case 1:
                    array[0] = 2;
                    array[1] = 3;
                    break;
                case 2:
                    array[0] = 1;
                    array[1] = 3;
                    break;
                case 3:
                    array[0] = 1;
                    array[1] = 2;
                    break;
            }
        } else {
            len = 1;
            array = new int[1];
            array[0] = third(gate, choice);
        }
        return array;
    }

    public static int prize(boolean change, int prize) {
        int gate = MathUtil.randomInRange(1, 3);
        int choice = MathUtil.randomInRange(1, 3);
        int[] empties = empty(gate, choice);
        int len = empties.length;
        int sum = 0;
        int new_choice = 0;
        int emptychoice = -1;
        if (len == 1) {
            emptychoice = empties[0];
        } else {
            emptychoice = empties[MathUtil.randomInRange(0, 1)];
        }
        if (!change) {
            sum = (gate == choice) ? prize : 0;
        } else {
            new_choice = third(choice, emptychoice);
            sum = (gate == new_choice) ? prize : 0;
        }
        return sum;
    }

    public static int monty(boolean change, int iters) {
        int sum = 0;
        for (var i = 0; i < iters; i++) {
            sum += prize(change, 100);
        }
        return sum;
    }

}
