import java.util.*;

public class SurvivalGame {

    // instance variable for random number generation
    private Random myRandom;

    // default constructor
    public SurvivalGame() {
		myRandom = new Random();
    }

   /**
    * competition plays a round of survival game
    *
    * @param player1, player2 - each player guesses a number and
    *        the player with the closer guess wins the round
    * @return nothing void
    */
    public void competition(Player player1, Player player2) {

        // asks two players for their guesses
        Scanner stdin = new Scanner(System.in);
        System.out.println("Pick a value from 0 to 10");
        System.out.print(player1.getName()+", what's your value? ");
        int value1 = stdin.nextInt();
        System.out.print(player2.getName()+", what's your value? ");
        int value2 = stdin.nextInt();

        // generate a random number
        int value = myRandom.nextInt(10);

        // Compare the differences between each guess and the random
        // number and decide which player wins. Adjust the lifepoints
        // accordingly
        if (Math.abs(value-value1) < Math.abs(value-value2)) {
            player2.removeLifePoints(10);
        } else if (Math.abs(value-value1) > Math.abs(value-value2)) {
            player1.removeLifePoints(10);
        } else {
            player1.removeLifePoints(5);
            player2.removeLifePoints(5);
        }
    }

    public static void main(String[] args) {

	// get the player names from the user
	Scanner stdin = new Scanner(System.in);
	System.out.println("Let's start a survival game");

	Player[] player = new Player[2];
	// create player 1 with the given name
	System.out.print("Player 1, what is your name? ");
	player[0] = new Player(stdin.nextLine());

	// create player 2 with the given name
	System.out.print("Player 2, what is your name? ");
	player[1] = new Player(stdin.nextLine());

	// generate an instance of SurvivalGame
	SurvivalGame sg = new SurvivalGame();

	// repeat competitions until one of the players runs out of lifePoints
	while (player[0].getLifePoints()>0 && player[1].getLifePoints()>0) {
	    sg.competition(player[0], player[1]);
	    System.out.println(player[0]);
	    System.out.println(player[1]);
	}

	// print which player has won the game
	if (player[0].getLifePoints() < player[1].getLifePoints()) {
	    System.out.println(player[1].getName()+" wins!!");
	} else if (player[0].getLifePoints() > player[1].getLifePoints()) {
	    System.out.println(player[0].getName()+" wins!!");
	} else {
	    System.out.println("Nobody wins..");
	}
    }
}
