public class pig
{
    public static void main (String args[])
    {
	new pig ();
    }


    public pig ()
    {
	System.out.println ("W E L C O M E   T O   P I G\n");
	System.out.println ("      '..'___ ");
	System.out.println ("      (@@)   |@ ");
	System.out.println ("          |--|  \n\n");
	instructions ();

	char again = 'y';
	while (again == 'y')
	{
	    playPig ();
	    again = IO.inputChar ("\n Play again? (y/n) ");
	}
	System.out.println ("Thanks for playing PIG!");
    }


    public void playPig ()
    {
	int turn = 1;
	int p1score = 0;
	int p2score = 0;

	while (!gameOver (p1score, p2score))
	{
	    System.out.println ("\nPlayer #1 has " + p1score + " points, Player #2 has " + p2score + " points.\n");
	    int total = 0;
	    int roll = 1;
	    while (roll != 0 && roll != -1)
	    {
		roll = takeTurn (total, turn);

		if (roll != 0 && roll != -1)
		    total += roll;
	    }

	    if (roll == 0)
		total = 0;

	    if (turn == 1)
	    {
		p1score += total;
		turn = 2;
	    }
	    else
	    {
		p2score += total;
		turn = 1;
	    }
	}


	if (p1score >= 100)
	    System.out.println ("\nPlayer #1 wins with " + p1score + " points!");
	else if (p2score >= 100)
	    System.out.println ("\nPlayer #2 wins with " + p2score + " points!");
    }


    public boolean gameOver (int p1sc, int p2sc)
    {
	if (p1sc >= 100)
	    return true;
	else if (p2sc >= 100)
	    return true;
	else
	    return false;
    }


    public boolean inValid (char c)
    {
	if (c == 'H')
	    return true;
	else if (c == 'h')
	    return true;
	else if (c == 'R')
	    return true;
	else if (c == 'r')
	    return true;
	else
	    return false;
    }


    public int takeTurn (int total, int p)
    {
	System.out.println ("Player #" + p + ", so far this round, you have " + total + " points.");
	char doWhat = IO.inputChar ("Would you like to (r)oll or (h)old? ");
	while (!inValid (doWhat))
	{
	    System.out.println ("Sorry, that input is invalid. Try again.");
	    doWhat = IO.inputChar ("Would you like to (r)oll or (h)old? ");
	}


	if (doWhat == 'h' || doWhat == 'H')
	    return -1;
	else
	{
	    int dice = (int) (Math.random () * 6) + 1;
	    System.out.println ("You rolled a " + dice);
	    if (dice == 1)
	    {
		System.out.println ("PIG! Too bad. Turn over");
		return 0;
	    }
	    else
		return dice;
	}
    }


    public void instructions ()
    {
	System.out.println ("* * * P I G   I N S T R U C T I O N S  * * *");
	System.out.println ("Each turn, a player repeatedly rolls a die:");
	System.out.println ("(a) If you roll a 1, you score nothing and it is the next player's turn.");
	System.out.println ("(b) If you roll any number other than 1, it is added to your turn total");
	System.out.println ("(c) If you 'hold', your turn total is added to your score, and it is the next player's turn.");
	System.out.println ("The first player to score 100 or more points wins.");
    }
}
