/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.mavenproject1;

/**
 *
 * @author caleb
 */
import java.util.Scanner;

public class Tyler {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        double percentage = 0.3; //initializing variables
        System.out.print("Enter the number of players: ");
        int size = in.nextInt();

        System.out.println("Contest with percentage set to 0.3: "); //each contest with percentages assigned
        System.out.println();
        contest(size, percentage);

        percentage = 0.2;
        System.out.println("Contest with percentage set to 0.2: ");
        System.out.println();
        contest(size, percentage);

        percentage = 0.1;
        System.out.println("Contest with percentage set to 0.1: ");
        System.out.println();
        contest(size, percentage);
    }

    public static int getWinner(int one, int two) { //randomly decides winner, if loop says all
        double random = Math.random();
        if (random <= 0.5) {
            return one;
        }
        else {
            return two;
        }
    }

    public static boolean contestRound(int[] array) { //changes the values in the array
        int first = (int) Math.round(Math.random() * array.length); //randomly assigns variables for the two indecies of
        if (first == 10) {                                          //the values that we will change. None of the variables
            first--;                                                //will equal the length of the array
        }

        while (array[first] == 0) { //Finds an index of the array where the value is positive when the value is zero
            first = (int) Math.round(Math.random() * array.length);
            if (first == 10) {
                first--;
            }
        }

        int second = (int) Math.round(Math.random() * array.length );
        if (second == 10) {
            second--;
        }
        while (array[second] == 0 && second != first) {
            second = (int) Math.round(Math.random() * array.length);
            if (second == 10) {
                second--;
            }
        }

        int returned = getWinner(first, second); //sees who wins

        if (returned == array[first]) { //changes index values based on who won
            array[first]++;
            array[second]--;
        }
        else {
            array[second]++;
            array[first]--;
        }

        if (array[first] == 0 || array[second] == 0) { //return statement
            return true;
        }
        else {
            return false;
        }
    }

    public static int countPlayers(int[] array) { // counts all the non-zero numbers in the token array
        int count = 0;
        for (int element : array)
            if (element != 0)
                count++;
        return count;
    }

    public static void printPlayers(int[] array) { // prints out the non-zero parts of the token array
        int highest=0, highestAt=0, lowest=100, lowestAt=0; //initializes variables

        System.out.print("Players   "); //prints out the index of each player with a positive value
        for (int i = 0; i < array.length; i++)
            if (array[i] != 0)
                System.out.print(i + "   ");

        System.out.println();
        System.out.print("Values    "); //prints the values of the indexes from before
        for (int i = 0; i < array.length; i++) {
            if (array[i] != 0) {
                System.out.print(array[i] + "   ");
            }
        }

        int count = 0;
        for (int i = 0; i < array.length; i++) { //Could not find a better way to figure out which is highest and which
            if (array[i] > 0) {                  //is lowest, so created new array with only the positive values.
                count++;                         //this loop calculates how many positive values there are to set the
            }                                    //length of the new index
        }

        int[] positives = new int[count];
        int pos = 0;
        for (int i = 0; i < array.length; i++) { //finds the values of the initialized variables
            if (array[i] > 0) {
                positives[pos] = array[i];
                pos++;
            }
            if (array[i] > highest) {
                highest = array[i];
                highestAt = i;
            }
            if (array[i] < lowest && array[i] != 0){
                lowest = array[i];
                lowestAt = i;
            }
        }

        System.out.println(); //prints out the variables
        System.out.println("Highest Player Index: " + highestAt + ", Value : " + highest + "; Lowest Player Index: " + lowestAt + ", Value: " + lowest);
        System.out.println();
    }

    public static void contest(int size, double percentage) { // calls other methods to do the contest
        int square = (int) Math.ceil(Math.sqrt(size));
        int[] array = new int[size]; //creates the array we'll use for the contest

        for (int i = 0; i < array.length; i++) { //assigns each value of the array the same thing
            array[i] = square;
        }

        printPlayers(array); //prints the first line

        int perc = (int) Math.round(array.length * percentage); //calculates which is higher, 2 or the percentage of the length
        if (perc < 2) {
            perc = 2;
        }


        int count = countPlayers(array);
        boolean contesting = contestRound(array); //loop that does the contests
        while (count > perc) {
            contesting = contestRound(array);
            if (contesting == true) {
                printPlayers(array);
                count = countPlayers(array);
            }
        }
    }
}