import java.util.Arrays;
import java.util.Random;

/**
 * Solution for PA5 of CS 139.
 * 
 * @author R.Grove
 * @author C.Mayfield
 * @version 0.1 2012Nov06
 */
public class Lottery {
    
    /**
     * Builds an array of the given size, and loads it with random numbers
     * from lowValue to highValue inclusively. No number can be used more
     * than once. The resulting array should be sorted in ascending order.
     */
    public static int[] generatePicks(int size, int lowValue,
                                      int highValue, long seed) {
        Random random;
        int[] picks;
        int i;
        int j;
        boolean valid;
        
        // validate arguments
        if (size < 3 || size > highValue - lowValue + 1) {
            return null;
        }
        
        // initialize objects
        random = new Random(seed);
        picks = new int[size];
        
        // generate unique random numbers
        for (i = 0; i < size; i++) {
            do {
                picks[i] = random.nextInt(highValue - lowValue + 1) + lowValue;
                
                // check for duplicates
                valid = true;
                for (j = 0; j < i; j++) {
                    if (picks[i] == picks[j]) {
                        valid = false;
                    }
                }
                
            } while (!valid);
        }
        
        return sortArray(picks);
    }
    
    /**
     * Checks the list of userNumbers against the numbers in the picks array to
     * count the number of numbers that the user guessed correctly.
     */
    public static int checkUserNumbers(int[] picks, int[] userNumbers) {
        int i;
        int j;
        int matches;
        
        // validate arguments
        if (picks == null || userNumbers == null
                || picks.length != userNumbers.length) {
            return -1;
        }
        
        // count the matches
        matches = 0;
        for (i = 0; i < picks.length; i++) {
            for (j = 0; j < userNumbers.length; j++) {
                if (picks[i] == userNumbers[j]) {
                    matches++;
                }
            }
        }
        return matches;
    }
    
    /**
     * Returns a copy of the given array sorted in ascending order.
     */
    public static int[] sortArray(int[] unsorted) {
        if (unsorted == null) {
            return null;
        }
        int[] sortedArray;
        sortedArray = Arrays.copyOf(unsorted, unsorted.length);
        Arrays.sort(sortedArray);
        return sortedArray;
    }
    
    /**
     * Creates an array of prize amounts based on the size of the lottery pick
     * and the total prizeMoney. The array positions correspond to the count of
     * correct numbers in a lottery pick (zero through size, inclusively).
     */
    public static int[] generatePrizes(int size, int prizeMoney) {
        int i;
        int[] prizes;
        int prizeMoneyRemaining;
        
        // validate arguments
        if (size < 3 || prizeMoney < 0) {
            return null;
        }
        
        // allocate the prize money
        prizes = new int[size + 1];
        prizeMoneyRemaining = prizeMoney;
        for (i = size; i > 3; i--) {
            prizes[i] = (int) Math.round(0.75 * prizeMoneyRemaining);
            prizeMoneyRemaining = prizeMoneyRemaining - prizes[i];
        }
        prizes[3] = prizeMoneyRemaining;        
        return prizes;
    }
    
}
