Black jack Highest Coderbyte Solution

0

Using the Java language, have the function BlackjackHighest(strArr) take the strArr parameter being passed which will be an array of numbers and letters representing blackjack cards. Numbers in the array will be written out. 

So for example strArr may be ["two","three","ace","king"]. The full list of possibilities for strArr is: two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace. Your program should output below, above, or blackjack signifying if you have blackjack (numbers add up to 21) or not and the highest card in your hand in relation to whether or not you have blackjack. If the array contains an ace but your hand will go above 21, you must count the ace as a 1. You must always try and stay below the 21 mark. So using the array mentioned above, the output should be below king. The ace is counted as a 1 in this example because if it wasn't you would be above the 21 mark. Another example would be if strArr was ["four","ten","king"], the output here should be above king. If you have a tie between a ten and a face card in your hand, return the face card as the "highest card". If you have multiple face cards, the order of importance is jack, queen, then king.

Blackjack highest coderbyte solution Java


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


public class BlackjackHighest {
String BlackjackHighest(String[] strArr) {
Card[] cards = new Card[strArr.length];
for (int i = 0; i < strArr.length; i++) {
cards[i] = new Card(strArr[i]);
}

Arrays.sort(cards, new Comparator<Card>() {
@Override
public int compare(Card card, Card card2) {
if (card.rating > card2.rating) return 1;
else if (card.rating < card2.rating) return -1;
return 0;
}
});

int total = 0;
boolean hasAce = false, aceUsed = false;
for (Card card : cards) {
if (card.rating == 14) {
hasAce = true;
}

if (total + card.topVal > 21) {
total += card.lowVal;
} else {
if (card.rating == 14) {
aceUsed = true;
}
total += card.topVal;
}
}

Card highest = cards[cards.length - 1];
if (hasAce && !aceUsed) {
highest = cards[cards.length - 2];
}

if (total < 21) {
return "below " + highest.repr;
} else if (total == 21) {
return "blackjack " + highest.repr;
}
return "above " + highest.repr;
}

class Card {
int lowVal = 0, topVal = 0, rating = 0;
String repr;

Card(String card) {
card = card.toLowerCase();
repr = card;

if (card.equals("two")) {
lowVal = 2; topVal = 2; rating = 2;
} else if (card.equals("three")) {
lowVal = 3; topVal = 3; rating = 3;
} else if (card.equals("four")) {
lowVal = 4; topVal = 4; rating = 4;
} else if (card.equals("five")) {
lowVal = 5; topVal = 5; rating = 5;
} else if (card.equals("six")) {
lowVal = 6; topVal = 6; rating = 6;
} else if (card.equals("seven")) {
lowVal = 7; topVal = 7; rating = 7;
} else if (card.equals("eight")) {
lowVal = 8; topVal = 8; rating = 8;
} else if (card.equals("nine")) {
lowVal = 9; topVal = 9; rating = 9;
} else if (card.equals("ten")) {
lowVal = 10; topVal = 10; rating = 10;
} else if (card.equals("jack")) {
lowVal = 10; topVal = 10; rating = 11;
} else if (card.equals("queen")) {
lowVal = 10; topVal = 10; rating = 12;
} else if (card.equals("king")) {
lowVal = 10; topVal = 10; rating = 13;
} else if (card.equals("ace")) {
lowVal = 1; topVal = 11; rating = 14;
}
}
}

public static void main (String[] args) {
Scanner s = new Scanner(System.in);
BlackjackHighest c = new BlackjackHighest();
System.out.print(c.BlackjackHighest(new String[] {"jack","ace"}));
}
}


Blackjack highest coderbyte solution python

import os
import random

deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4

def deal(deck):
hand = []
for i in range(2):
random.shuffle(deck)
card = deck.pop()
if card == 11:card = "J"
if card == 12:card = "Q"
if card == 13:card = "K"
if card == 14:card = "A"
hand.append(card)
return hand

def play_again():
again = raw_input("Do you want to play again? (Y/N) : ").lower()
if again == "y":
dealer_hand = []
player_hand = []
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
game()
else:
print "Bye!"
exit()

def total(hand):
total = 0
for card in hand:
if card == "J" or card == "Q" or card == "K":
total+= 10
elif card == "A":
if total >= 11: total+= 1
else: total+= 11
else:
total += card
return total

def hit(hand):
card = deck.pop()
if card == 11:card = "J"
if card == 12:card = "Q"
if card == 13:card = "K"
if card == 14:card = "A"
hand.append(card)
return hand

def clear():
if os.name == 'nt':
os.system('CLS')
if os.name == 'posix':
os.system('clear')

def print_results(dealer_hand, player_hand):
clear()
print "The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand))
print "You have a " + str(player_hand) + " for a total of " + str(total(player_hand))

def blackjack(dealer_hand, player_hand):
if total(player_hand) == 21:
print_results(dealer_hand, player_hand)
print "Congratulations! You got a Blackjack!\n"
play_again()
elif total(dealer_hand) == 21:
print_results(dealer_hand, player_hand)
print "Sorry, you lose. The dealer got a blackjack.\n"
play_again()

def score(dealer_hand, player_hand):
if total(player_hand) == 21:
print_results(dealer_hand, player_hand)
print "Congratulations! You got a Blackjack!\n"
elif total(dealer_hand) == 21:
print_results(dealer_hand, player_hand)
print "Sorry, you lose. The dealer got a blackjack.\n"
elif total(player_hand) > 21:
print_results(dealer_hand, player_hand)
print "Sorry. You busted. You lose.\n"
elif total(dealer_hand) > 21:
print_results(dealer_hand, player_hand)
print "Dealer busts. You win!\n"
elif total(player_hand) < total(dealer_hand):
print_results(dealer_hand, player_hand)
print "Sorry. Your score isn't higher than the dealer. You lose.\n"
elif total(player_hand) > total(dealer_hand):
print_results(dealer_hand, player_hand)
print "Congratulations. Your score is higher than the dealer. You win\n"

def game():
choice = 0
clear()
print "WELCOME TO BLACKJACK!\n"
dealer_hand = deal(deck)
player_hand = deal(deck)
while choice != "q":
print "The dealer is showing a " + str(dealer_hand[0])
print "You have a " + str(player_hand) + " for a total of " + str(total(player_hand))
blackjack(dealer_hand, player_hand)
choice = raw_input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower()
clear()
if choice == "h":
hit(player_hand)
while total(dealer_hand) < 17:
hit(dealer_hand)
score(dealer_hand, player_hand)
play_again()
elif choice == "s":
while total(dealer_hand) < 17:
hit(dealer_hand)
score(dealer_hand, player_hand)
play_again()
elif choice == "q":
print "Bye!"
exit()
if __name__ == "__main__":
game()

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !