Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class BlackJack {
- private final Deck deck = new Deck();
- private final ArrayList<Card> player = new ArrayList<>();
- private final ArrayList<Card> dealer = new ArrayList<>();
- public BlackJack() {
- deck.shuffle();
- }
- public void play() {
- player.add(deck.dealACard());
- player.add(deck.dealACard());
- dealer.add(deck.dealACard());
- dealer.add(deck.dealACard());
- System.out.printf("Player Cards: %s%n", player);
- System.out.printf("Player Value: %s%n", value(player));
- System.out.printf("Dealer First Card: %s%n%n", dealer.get(0));
- Scanner in = new Scanner(System.in);
- while(value(player) <= 21 && player.size() < 5) {
- System.out.printf("Player Cards: %s%n", player);
- System.out.printf("Player Value: %s%n", value(player));
- System.out.println("Hit or Stay?");
- String line = in.next();
- if(line.equals("Hit")) {
- player.add(deck.dealACard());
- System.out.println();
- } else if (line.equals("Stay")) {
- System.out.println();
- break;
- }
- }
- System.out.printf("Player Cards: %s%n", player);
- System.out.printf("Player Value: %s%n", value(player));
- if(value(player) > 21) {
- System.out.println("The player busted and lost!");
- } else if (player.size() >= 5) {
- System.out.println("The player won with a Five Card Charlie!");
- } else {
- while(value(dealer) < 17) {
- dealer.add(deck.dealACard());
- }
- System.out.printf("Dealer Cards: %s%n", dealer);
- System.out.printf("Dealer Value: %s%n", value(dealer));
- if(value(dealer) > 21) {
- System.out.println("The player won as the dealer busted!");
- } else if(value(dealer) > value(player)) {
- System.out.println("The dealer won with more value!");
- } else if (value(dealer) == value(player)) {
- System.out.println("The dealer and player tied!");
- } else {
- System.out.println("The player won with more value!");
- }
- }
- }
- private static int value(List<Card> cards) {
- int aces = 0;
- int value = 0;
- for(int i = 0; i < cards.size(); i++) {
- Card c = cards.get(i);
- if(c.getRank() == 1) {
- aces++;
- value += 1;
- } else if (c.getRank() >= 2 && c.getRank() <= 10) {
- value += c.getRank();
- } else {
- value += 10;
- }
- }
- for(int i = 0; i < aces && value <= 11; i++) {
- value += 10;
- }
- return value;
- }
- public static void main(String[] args) {
- new BlackJack().play();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement