/*************************************************************************
*                          Black Jack program                            *
*                              Arup Guha                                 *
*                               2/8/99                                   *
*                                                                        * 
*  Brief Desciption : This program lets the user play one had of         *
*  blackjack against the computer. The main difference beteweent his     *
*  implementation versus normal blackjack is that the user must decide   *
*  immediately if an ace is to be worth 1 point or 11.                   *
*									 *
*  Edited on 7/20/05 for 2005 BHCSI: Main change made was that the old   *
*  code didn't have any methods. This code has been streamlined with	 *	
*  the use of several methods.						 *
*************************************************************************/

// Edited on 7/22/07 to utilize the Scanner for I/O.

import java.util.*;
import java.lang.Math;

class Blackjack {

    public static void main(String argv[]) {

		Scanner stdin = new Scanner(System.in);

		Random generator = new Random(); // used to generate random cards.

		int score, computerscore; // keeps track of scores
	
		System.out.println("Welcome to Blackjack against the computer.");
	
		// Prints out player's final score
		score = playPlayer(stdin, generator);
		System.out.println("So, your final score = " + score);

		// Prints out computer's final score.
		computerscore = playComputer(generator);
		System.out.println("The computer score = " + computerscore);

		printWinner(score, computerscore);
    }

    // This method executes the game for the user/player.
    public static int playPlayer(Scanner stdin, Random r) {

		boolean hit = true; // true while player wants to hit.
		int numcards = 0; // keeps track of the number of cards the user has.
		int score = 0;

		// Loop runs till user doesn't want another card or has busted.
		while (hit) {

	    	int singlecard = 0; // Temp. var. to make sure following loop runs
	        	                // only once after initial two cards are given.

	    	// Gives player 2 cards initially, then 1 every time it's run thereafter.
	    	while (numcards < 2 || singlecard < 1) {

				int rand = Math.abs(r.nextInt())%13; // next card.

				// Handles case of player getting an ace. Let's user choose value.
				if (rand == 0) {
		    		score = score + getAce(stdin);
				}

				// Scores other cases.
				else if (rand < 10) {
		    		System.out.println("Your card value = " + (rand + 1));
		    		score = score + rand + 1;
				}
				else {
		    		System.out.println("Your card value = 10");
		    		score = score + 10;
				}

				// Increment appropriate counters.
				numcards++;
				singlecard++;
	    	}	

	    	// Prints out player's current score and asks if he/she wants
	    	// another hit.
	    	System.out.println("So, your current score is = " + score);
	    	if (score <= 21) {
	    		
				System.out.println("Would you like to hit again?");
				char ans = (stdin.next()).charAt(0);
		
				if (ans != 'y' && ans != 'Y')
		    		hit = false;
	    	}
	    	else
				hit = false;
		}	

		return score;

    }

    // This method executes the game for the computer.
    public static int playComputer(Random r) {

        int computerscore = 0;

		// Executes computers turn. It hits till 17, just like Vegas.
		while (computerscore < 17) {

	    	int rand = Math.abs(r.nextInt())%13; //next random card.
	    	int cardvalue; // used to store current card's point value.

	    	// Picks score of an ace, so as not to bust.
	    	if (rand == 0) {
		
				if (computerscore < 11) 
		    		cardvalue = 11;
				else 
		    		cardvalue = 1;
	    	}
	    	else if (rand < 10) 
				cardvalue = rand + 1;
	    	else 
				cardvalue = 10;

	    	// Prints out computer's score after every card.
	    	computerscore = computerscore + cardvalue;
	    	System.out.println("Computer card value = " + cardvalue);
		}

		return computerscore;
    }

    // Given both the player's score and the computer's score, this method
    // prints out who won the game.
    public static void printWinner(int playerscore, int computerscore) {

		// Prints out winner to the screen.
		if (playerscore > 21 && computerscore > 21)
	    	System.out.println("We have both busted. No winners today.");
		else if (playerscore > 21)
	    	System.out.println("You have busted. Ha ha, I win!!!");
		else if (computerscore > 21)
	    	System.out.println("I have busted. You win by default.");
		else {
	    
	    	if (playerscore > computerscore)
				System.out.println("You won!!!");
	    	else if (playerscore < computerscore)
				System.out.println("I, a lowly computer, have beat you at blackjack.");
	    	else
				System.out.println("We have tied.");
		}	

    }

    // This method retrieves the score of an Ace for the user by asking
    // him/her what value they would like their ace to be.
    public static int getAce(Scanner stdin) {

  		System.out.println("You have gotten an ace, would you like it to count as 11 or 1.");
		int acescore = stdin.nextInt();
	
		// Continue prompting for a score until you get a valid one.
		while (acescore != 11 && acescore != 1) {

        	System.out.println("Sorry, that is not valid, please enter either 1 or 11 for the score of your ace.");	
           	acescore = stdin.nextInt();
		}

		return acescore;
    }

}

