package _04_双色球;

import java.util.Random;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        // 创建中奖的号码
        int[] prizeArr = createPrizeNumber();
        System.out.println("中奖号码");
        for (int i = 0; i < prizeArr.length; i++) {
            System.out.print(prizeArr[i] +" ");
        }
        System.out.println();
        int[] userPrizeArr = userInputPrizeNumber();
        System.out.println("用户购买号码");
        for (int i = 0; i < userPrizeArr.length; i++) {
            System.out.print(userPrizeArr[i] +" ");
        }
        System.out.println();
        // 中奖红球数量
        int redCountNumber= 0;
        // 中奖篮球数量
        int blueCountNumber =0;
        // 先比较红球
        for (int i = 0; i < 6; i++) {
            // 用户输入红球和中奖红球进行一次比较
            int userInputRedNumber = userPrizeArr[i];
            for (int j = 0; j < 6; j++) {
                int prizeRedNumber = prizeArr[j];
                if(prizeRedNumber == userInputRedNumber){
                    // 相等说明中了,让红球中奖数量+1
                    redCountNumber++;
                    break;
                }
            }
        }
        // 在比较蓝球
        if(prizeArr[prizeArr.length-1] == userPrizeArr[userPrizeArr.length-1]){
            // 中奖蓝球数量+1
            blueCountNumber++;
        }

        System.out.println("中奖红球数量为:"+ redCountNumber+" -中奖篮球数量为:"+ blueCountNumber);
        if((redCountNumber == 0 && blueCountNumber == 1)
                || (redCountNumber == 1 && blueCountNumber == 1)
        ){
            System.out.println("恭喜你中了六等奖 5元");
        }else if((redCountNumber == 2 && blueCountNumber == 1)
                || (redCountNumber == 3 && blueCountNumber == 1)
                || (redCountNumber == 4 && blueCountNumber == 0)
        ){
            System.out.println("恭喜你中了五等奖 10元");
        }else if((redCountNumber == 4 && blueCountNumber == 1)
                || (redCountNumber == 5 && blueCountNumber == 0)
        ){
            System.out.println("恭喜你中了四等奖200元");
        }else if((redCountNumber == 5 && blueCountNumber == 1)){
            System.out.println("恭喜中了三等奖 3000元");
        }else if((redCountNumber == 6 && blueCountNumber == 0)){
            System.out.println("恭喜中了二等奖 500万");
        }else if((redCountNumber == 6 && blueCountNumber == 1)){
            System.out.println("恭喜中了一等奖 1000万");
        }
    }

    private static int[] userInputPrizeNumber() {
        // 1 创建Scanner 用户后续录入
        Scanner scanner = new Scanner(System.in);
        // 2 创建长度为7数组存储用户数输入的中奖号码
        int[] prizeArr = new int[7];
        // 3 遍历6 让用户输入6次红球 1-33
        for (int i = 0; i < 6;) {
            System.out.println("请输入第"+(i+1)+"红球,红球数字在1-33之间");
            int redNumber = scanner.nextInt();
            // 判断红球是否已经存在了
            boolean flag = contains(prizeArr, redNumber);
            if(!flag){
                prizeArr[i]= redNumber;
                i++;
            }else{
                System.out.println("这个号码已经存在了, 请输入其他的号码");
            }
        }
        // 4 让用户输入蓝球  1-16
        System.out.println("请输入蓝球,蓝球范围在1-16之间");
        int blueNumber = scanner.nextInt();
        prizeArr[prizeArr.length-1] = blueNumber;
        return prizeArr;
    }

    private static int[] createPrizeNumber() {
        //1 创建长度为7的数组
        int[] arr = new int[7];
        //2 生成红球
        /**
         * 红球规则 1-33 之间
         * 不允许有重复的号码
         */
        Random random = new Random();
        for (int i = 0; i < 6; ) {
            // 生成红球号码 1-33 之间
            int redNumber = random.nextInt(33) + 1;
            //判断红球是否已经存在了
            boolean flag = contains(arr,redNumber);
            // 如果元素不存在在把添加到数组中
            if(!flag){
                // 把红球放到数组中
                arr[i] = redNumber;
                i++;
            }
        }
        // 生成蓝球
        // 1-16
        arr[arr.length - 1] = random.nextInt(16)+1;
        return arr;

    }
    // 判断元素是否在数组当中
    private static boolean contains(int[] arr, int redNumber) {
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            if(redNumber == arr[i]){
                flag = true;
                break;
            }
        }
        return flag;
    }
}
