/*
Christian F Lewis 12.03.17
CS111a Intro to java
Extra Credit Program
*/
import java.util.Scanner;
public class blackjack
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int[] deck = new int[4];
char players_choice;
System.out.print("Do you want to play blackjack? (y/n): ");
players_choice = scan.next().charAt(0);
display(deck, players_choice);
}
//passes the card deck "array" and initial answer to play into method
//and displays the body of the blackjack game in full...
public static void display(int[] cards, char play)
{
char another_card;
int players_wins = 0, dealers_wins = 0;
while (move(play))
{
//first 2 cards of players hand...
cards[0] = (int) (Math.random() * 10) + 1;
cards[1] = (int) (Math.random() * 10) + 1;
int dealers_card = 0, dealers_total = 0;
//keeps running count of hands won, dealer vs player...
System.out.println("\n>>> Player has won " + players_wins + " hands <<<");
System.out.println(">>> Dealer has won " + dealers_wins + " hands <<<\n");
System.out.println("First cards: " + cards[0] + ", " + cards[1]);
cards[3] = cards[0] + cards[1];
if ((cards[0] == 10 && cards[1] == 1) || (cards[0] == 1 && cards[1] == 10))
{
System.out.println("\n!!! Blackjack !!! You win!");
players_wins = players_wins + 2;
play = answer();
if (move(play))
System.out.print("\nYou entered: YES\n");
else
System.out.print("\nYou entered: NO\n");
}
if (!((cards[0] == 10 && cards[1] == 1) || (cards[0] == 1 && cards[1] == 10)))
{
while (true)
{
System.out.println("\nTotal: " + cards[3] + "\n");
another_card = flop();
if (move(another_card))
System.out.print("\nYou entered: YES\n");
else
System.out.print("\nYou entered: NO\n");
if (move(another_card))
{
cards[2] = (int) (Math.random() * 10) + 1;
System.out.println("\nCard: " + cards[2]);
cards[3] += cards[2];
if (cards[3] > 21)
{
System.out.print("\nTotal: " + cards[3] + "\n");
System.out.println("\n *** Bust ***");
play = answer();
dealers_wins = dealers_wins + 1;
if (move(play))
System.out.print("\nYou entered: YES\n");
else
System.out.print("\nYou entered: NO\n");
break;
}
if (cards[3] == 21)
{
System.out.println("\n!!! Blackjack !!! You win!");
play = answer();
players_wins = players_wins + 1;
if (move(play))
System.out.print("\nYou entered: YES\n");
else
System.out.print("\nYou entered: NO\n");
break;
}
}
if (!move(another_card))
{
if (dealers_total < cards[3])
{
do
{
dealers_card = (int) (Math.random() * 10) + 1;
dealers_total += dealers_card;
if (dealers_total == 21 || dealers_total > cards[3])
break;
} while (dealers_total <= 21);
if (dealers_total > cards[3] && dealers_total <= 21)
{
System.out.println("\nYour total is: " + cards[3]);
System.out.println("\nThe dealers total is: " +
dealers_total + " You lose...");
dealers_wins = dealers_wins + 1;
}
else if (dealers_total > 21 || dealers_total < cards[3])
{
System.out.println("\nYour total is: " + cards[3]);
System.out.println("\nThe dealers total is: " +
dealers_total + " You win!");
players_wins = players_wins + 1;
}
else if (dealers_total == cards[3])
{
System.out.println("\nYour total is: " + cards[3]);
System.out.println("\nThe dealers total is: " +
dealers_total + " Push...");
}
}
play = answer();
System.out.println();
if (move(play))
System.out.print("You entered: YES\n");
else
System.out.print("You entered: NO\n");
break;
}
}
}
if (!move(play))
{
System.out.println("\n>>> Player hands won: " + players_wins + " <<<");
System.out.println(">>> Dealer hands won: " + dealers_wins + " <<<\n");
if (players_wins > dealers_wins)
System.out.println("Congratualations! You beat the house!");
else if (players_wins < dealers_wins)
System.out.println("Sorry, the house won...");
else
System.out.println("Even break. No winners no losers...");
}
}
}
//used in place of asking the player if they want
//to play another hand, returns a character...
public static char answer()
{
System.out.print("\nDo you want to play again? (y/n): ");
char next_move = scan.next().charAt(0);
return next_move;
}
//used in place of asking the player if they want
//another card throughout the program, returns character...
public static char flop()
{
System.out.print("Would you like another card? (y/n): ");
char a_card = scan.next().charAt(0);
return a_card;
}
//uses a boolean to simplify the condition of the
//various if statements throughout the program...
public static boolean move(char x)
{
if (x == 'y')
return true;
else
return false;
}
}
/*
>>> Player has won 7 hands <<<
>>> Dealer has won 11 hands <<<
First cards: 10, 8
Total: 18
Would you like another card? (y/n): n
You entered: NO
Your total is: 18
The dealers total is: 20 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 7 hands <<<
>>> Dealer has won 12 hands <<<
First cards: 10, 6
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 25 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 8 hands <<<
>>> Dealer has won 12 hands <<<
First cards: 10, 3
Total: 13
Would you like another card? (y/n): y
You entered: YES
Card: 9
Total: 22
*** Bust ***
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 8 hands <<<
>>> Dealer has won 13 hands <<<
First cards: 3, 2
Total: 5
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 9
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 25 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 9 hands <<<
>>> Dealer has won 13 hands <<<
First cards: 10, 6
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 26
*** Bust ***
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 9 hands <<<
>>> Dealer has won 14 hands <<<
First cards: 4, 6
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 11
Would you like another card? (y/n): y
You entered: YES
Card: 5
Total: 16
Would you like another card? (y/n): n
You entered: NO
Your total is: 16
The dealers total is: 17 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 9 hands <<<
>>> Dealer has won 15 hands <<<
First cards: 1, 3
Total: 4
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 8
Total: 18
Would you like another card? (y/n): n
You entered: NO
Your total is: 18
The dealers total is: 20 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 9 hands <<<
>>> Dealer has won 16 hands <<<
First cards: 3, 1
Total: 4
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 7
Total: 23
*** Bust ***
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 9 hands <<<
>>> Dealer has won 17 hands <<<
First cards: 8, 5
Total: 13
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 21 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 9 hands <<<
>>> Dealer has won 18 hands <<<
First cards: 3, 7
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 7
Total: 17
Would you like another card? (y/n): n
You entered: NO
Your total is: 17
The dealers total is: 18 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 9 hands <<<
>>> Dealer has won 19 hands <<<
First cards: 3, 1
Total: 4
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 11
Would you like another card? (y/n): y
You entered: YES
Card: 10
!!! Blackjack !!! You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 10 hands <<<
>>> Dealer has won 19 hands <<<
First cards: 4, 2
Total: 6
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 9
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 9
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 21 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 10 hands <<<
>>> Dealer has won 20 hands <<<
First cards: 2, 7
Total: 9
Would you like another card? (y/n): y
You entered: YES
Card: 5
Total: 14
Would you like another card? (y/n): y
You entered: YES
Card: 9
Total: 23
*** Bust ***
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 10 hands <<<
>>> Dealer has won 21 hands <<<
First cards: 5, 4
Total: 9
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 7
Total: 17
Would you like another card? (y/n): n
You entered: NO
Your total is: 17
The dealers total is: 18 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 10 hands <<<
>>> Dealer has won 22 hands <<<
First cards: 10, 8
Total: 18
Would you like another card? (y/n): n
You entered: NO
Your total is: 18
The dealers total is: 22 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 11 hands <<<
>>> Dealer has won 22 hands <<<
First cards: 2, 8
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 8
Total: 18
Would you like another card? (y/n): n
You entered: NO
Your total is: 18
The dealers total is: 27 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 12 hands <<<
>>> Dealer has won 22 hands <<<
First cards: 9, 3
Total: 12
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 15
Would you like another card? (y/n): y
You entered: YES
Card: 5
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 25 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 13 hands <<<
>>> Dealer has won 22 hands <<<
First cards: 7, 9
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 5
!!! Blackjack !!! You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 14 hands <<<
>>> Dealer has won 22 hands <<<
First cards: 9, 8
Total: 17
Would you like another card? (y/n): n
You entered: NO
Your total is: 17
The dealers total is: 18 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 14 hands <<<
>>> Dealer has won 23 hands <<<
First cards: 4, 8
Total: 12
Would you like another card? (y/n): y
You entered: YES
Card: 8
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 22 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 15 hands <<<
>>> Dealer has won 23 hands <<<
First cards: 8, 5
Total: 13
Would you like another card? (y/n): y
You entered: YES
Card: 8
!!! Blackjack !!! You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 16 hands <<<
>>> Dealer has won 23 hands <<<
First cards: 2, 6
Total: 8
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 11
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 15
Would you like another card? (y/n): y
You entered: YES
Card: 5
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 21 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 16 hands <<<
>>> Dealer has won 24 hands <<<
First cards: 10, 1
Blackjack! You win!!!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 18 hands <<<
>>> Dealer has won 24 hands <<<
First cards: 1, 3
Total: 4
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 7
Would you like another card? (y/n): y
You entered: YES
Card: 8
Total: 15
Would you like another card? (y/n): y
You entered: YES
Card: 5
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 21 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 18 hands <<<
>>> Dealer has won 25 hands <<<
First cards: 1, 9
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 24 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 19 hands <<<
>>> Dealer has won 25 hands <<<
First cards: 2, 6
Total: 8
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 9
Would you like another card? (y/n): y
You entered: YES
Card: 5
Total: 14
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 26 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 20 hands <<<
>>> Dealer has won 25 hands <<<
First cards: 3, 2
Total: 5
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 15
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 26
*** Bust ***
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 20 hands <<<
>>> Dealer has won 26 hands <<<
First cards: 3, 9
Total: 12
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 15
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 20 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 20 hands <<<
>>> Dealer has won 27 hands <<<
First cards: 10, 1
Blackjack! You win!!!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 22 hands <<<
>>> Dealer has won 27 hands <<<
First cards: 4, 4
Total: 8
Would you like another card? (y/n): y
You entered: YES
Card: 9
Total: 17
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 23
*** Bust ***
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 22 hands <<<
>>> Dealer has won 28 hands <<<
First cards: 7, 5
Total: 12
Would you like another card? (y/n): y
You entered: YES
Card: 7
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 29 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 23 hands <<<
>>> Dealer has won 28 hands <<<
First cards: 8, 7
Total: 15
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 5
!!! Blackjack !!! You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 24 hands <<<
>>> Dealer has won 28 hands <<<
First cards: 1, 3
Total: 4
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 25 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 25 hands <<<
>>> Dealer has won 28 hands <<<
First cards: 1, 6
Total: 7
Would you like another card? (y/n): y
You entered: YES
Card: 6
Total: 13
Would you like another card? (y/n): y
You entered: YES
Card: 1
Total: 14
Would you like another card? (y/n): y
You entered: YES
Card: 7
!!! Blackjack !!! You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 26 hands <<<
>>> Dealer has won 28 hands <<<
First cards: 5, 7
Total: 12
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 26 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 27 hands <<<
>>> Dealer has won 28 hands <<<
First cards: 1, 1
Total: 2
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 6
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 16
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 23 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 28 hands <<<
>>> Dealer has won 28 hands <<<
First cards: 4, 6
Total: 10
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 21 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 28 hands <<<
>>> Dealer has won 29 hands <<<
First cards: 2, 2
Total: 4
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 8
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 12
Would you like another card? (y/n): y
You entered: YES
Card: 8
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 23 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 29 hands <<<
>>> Dealer has won 29 hands <<<
First cards: 1, 1
Total: 2
Would you like another card? (y/n): y
You entered: YES
Card: 4
Total: 6
Would you like another card? (y/n): y
You entered: YES
Card: 5
Total: 11
Would you like another card? (y/n): y
You entered: YES
Card: 3
Total: 14
Would you like another card? (y/n): y
You entered: YES
Card: 10
Total: 24
*** Bust ***
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 29 hands <<<
>>> Dealer has won 30 hands <<<
First cards: 7, 4
Total: 11
Would you like another card? (y/n): y
You entered: YES
Card: 8
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 21 You lose...
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 29 hands <<<
>>> Dealer has won 31 hands <<<
First cards: 9, 10
Total: 19
Would you like another card? (y/n): n
You entered: NO
Your total is: 19
The dealers total is: 29 You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 30 hands <<<
>>> Dealer has won 31 hands <<<
First cards: 9, 2
Total: 11
Would you like another card? (y/n): y
You entered: YES
Card: 10
!!! Blackjack !!! You win!
Do you want to play again? (y/n): y
You entered: YES
>>> Player has won 31 hands <<<
>>> Dealer has won 31 hands <<<
First cards: 7, 5
Total: 12
Would you like another card? (y/n): y
You entered: YES
Card: 8
Total: 20
Would you like another card? (y/n): n
You entered: NO
Your total is: 20
The dealers total is: 25 You win!
Do you want to play again? (y/n): n
You entered: NO
>>> Player hands won: 32 <<<
>>> Dealer hands won: 31 <<<
Congratualations! You beat the house!
C:\Users\christianizm\Programming\Atom\Java>
*/
syntax highlighted by Code2HTML, v. 0.9