# Blackjack Assignment Solution

Something similar to a solution to the Blackjack Assignment in Java. If you were to base your own solution on this code, it would be obvious and it would be considered plagiarism for the purposes of our course.

## Code

```package edu.nyu.cs.fb1258;

import java.util.Random;
import java.util.Scanner;

public class BlackjackSimple {

public static void main(String[] args) {

//a string that keeps track of which cards we've dealt to the player
String playerCards = ""; //starts off blank

//two sentinel variables that keep track of whether to ask each player if they want a new card

//two sentinel variables that keep track of whether either player has gone "bust".
boolean dealerIsBust = false;
boolean playerIsBust = false;

//two integers to keep a running total of the points
int playerTotal = 0;

//create a Random "object" to use to generate random numbers
//the default "seed" used by this built-in Random object is the current time in milliseconds.
//two separate pseudo-random number generators based on the same seed will produce the same pseudo-random numbers... beware!
Random randomGenerator = new Random();

//deal first "hand" to player
int playerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
int playerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
playerCards = playerCard1 + ", " + playerCard2; //store these cards as a string we use to tell the user what they have been dealt so far

//deal first "hand" to dealer
int dealerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
int dealerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10

//calculate totals
playerTotal += playerCard1 + playerCard2; //add these two cards to the total

//tell the user what they have

//as long as neither player is bust and either player wants a new card, keep iterating the loop

//ask the user whether they would like to hit
System.out.println("Would you like to 'hit' or 'stand'?");
Scanner scnr = new Scanner(System.in);
String playerResponse = scnr.nextLine(); //get the response

//handle the player's response
switch (playerResponse.toLowerCase()) {
case "hit":
//if they asked for a hit, give them a card.
int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
playerTotal += newCard; //add this card to the numeric total we are keeping
playerCards+= ", " + newCard; //add this card to the list of cards we show the user

//tell the user what they have
System.out.println("Sure...  You now have: " + playerCards);

//check whether player went "bust"
if (playerTotal > 21) {
playerIsBust = true; //flip the flag
}

break; //break out of the switch statement
case "stand":
//if they stand, we don't ask them if they want a hit again
askPlayerToHit = false; //change the flag
break; //break out of the switch statement
default:
//we only get here if the user entered something other than 'hit' or 'stand'
System.out.println("Sorry, I can't understand you... I guess you want to stand.");
askPlayerToHit = false; //change the flag
break; //break out of the switch statement
} //switch

//if the player went "bust", quit right now
if (playerIsBust) break; //break out of the while loop

//determine whether dealer whether they would like to hit
boolean dealerWantsCard = randomGenerator.nextBoolean(); //generate a pseudo-random boolean value

//pretend as if the dealer answered 'hit' or 'stand', like a real player
String dealerResponse = ""; //this will store the dealer's response that we generate
if (dealerWantsCard) {
dealerResponse = "hit"; //simulate a "hit" response
}
else {
dealerResponse = "stand"; //simulate a "stand" response
}

//handle the dealer's response
switch(dealerResponse.toLowerCase()) {
case "hit":
//if they asked for a hit, give them a card.
int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
dealerTotal += newCard; //add this card to the numeric total we are keeping

//check whether dealer went "bust"
dealerIsBust = true; //flip the flag
}

break; //break out of the switch statement
case "stand":
//if they stand, we don't ask them if they want a hit again
break; //break out of the switch statement
} //switch

scnr.close(); //close Scanner to conserve resourcres
} //while

//players are both done hitting... now check for any busts and tell them who won
if (playerIsBust) {
//the player busted
System.out.println("You went bust with " + playerTotal + ".\nThe dealer wins with " + dealerTotal + "!");
}
else if (dealerIsBust) {
//the dealer busted
System.out.println("The dealer went bust with " + dealerTotal + ".\nYou win with " + playerTotal + "!");
}
else if (playerTotal > dealerTotal) {
System.out.println("The dealer has " + dealerTotal + ".\nYou win with " + playerTotal + "!");
}
else if (playerTotal < dealerTotal) {
System.out.println("The dealer has " + dealerTotal + " and you have " + playerTotal + ".\nDealer wins!");
}
else {
System.out.println("It's a tie!  You and the dealer both have " + playerTotal + ".");
}

} //main method

} //class
```