-
Notifications
You must be signed in to change notification settings - Fork 1
/
Blackjack Highest
105 lines (91 loc) · 2.89 KB
/
Blackjack Highest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
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.
*/
import java.util.*;
import java.io.*;
class Main {
public static String BlackjackHighest(String[] strArr) {
// code goes here
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;
}
static class Card{
int lowVal = 0, rating = 0, topVal=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;rating=4; topVal=4;
}else if(card.equals("five")){
lowVal=5; rating=5;topVal=5;
}else if(card.equals("six")){
lowVal=6;rating=6;topVal=6;
}else if(card.equals("seven")){
lowVal=7;rating=7;topVal=7;
}else if(card.equals("eight")){
lowVal=8;rating=8;topVal=8;
}else if(card.equals("nine")){
lowVal=9;rating=9;topVal=9;
}else if(card.equals("ten")){
lowVal=10;rating=10;topVal=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) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(BlackjackHighest(s.nextLine()));
}
}