Hi @kotp, sure! Thank you for taking the time.
I’ve seen the TwoFer exercise only over on the Groovy track, but the same is true there.
Here’s my solution of TwoFer that passed the Exercism test:
class TwoFer {
static String twoFer(String name) {
if (name) {
return "One for " + name + ", one for me."
} else {
return "One for you, one for me."
}
}
}
Now, since I love to see things in action (especially to see if it still works when I tweak it), I always make a version that spits out something in the command line. Here’s the “actionable” version:
class TwoFer {
def name;
public TwoFer(String name) {
this.name = name;
}
public String decider() {
if (name) {
return "One for " + name + ", one for me."
} else {
return "One for you, one for me."
}
}
static main(args) {
def walter = new TwoFer("Walter");
def rando = new TwoFer(null);
println(walter.decider());
println(rando.decider());
}
}
To be honest, the groovy code just gets a time-out error.
In Java, I get actual failed tests. Here, I don’t have two-fer, but I can give you Blackjack. Here’s the solution that passes the Exercism tests:
public class Blackjack {
public int parseCard(String card) {
switch (card) {
case "ace":
return 11;
case "two":
return 2;
case "three":
return 3;
case "four":
return 4;
case "five":
return 5;
case "six":
return 6;
case "seven":
return 7;
case "eight":
return 8;
case "nine":
return 9;
case "ten": case "jack": case "queen": case "king":
return 10;
default:
return 0;
}
}
public boolean isBlackjack(String card1, String card2) {
if (parseCard(card1) + parseCard(card2) == 21) {
return true;} else {
return false;
}
}
public String largeHand(boolean isBlackjack, int dealerScore) {
if (isBlackjack == true) {
if (dealerScore < 10) {
return "W";
} else {
return "S";
}
} else {
return "P";
}
}
public String smallHand(int handScore, int dealerScore) {
if (handScore > 16) {
return "S";
} else if (handScore < 12) {
return "H";
} else if (dealerScore > 6) {
return "H";
} else {
return "S";
}
}
// FirstTurn returns the semi-optimal decision for the first turn, given the cards of the player and the dealer.
// This function is already implemented and does not need to be edited. It pulls the other functions together in a
// complete decision tree for the first turn.
public String firstTurn(String card1, String card2, String dealerCard) {
int handScore = parseCard(card1) + parseCard(card2);
int dealerScore = parseCard(dealerCard);
if (20 < handScore) {
return largeHand(isBlackjack(card1, card2), dealerScore);
} else {
return smallHand(handScore, dealerScore);
}
}
}
And here’s the version that I can compile and run in the command line, but it fails the Exercism tests:
public class Blackjack {
String card1;
String card2;
String dealerCard;
public Blackjack(String a, String b, String c) {
card1 = a;
card2 = b;
dealerCard = c;
}
public int parseCard(String card) {
switch (card) {
case "ace":
return 11;
case "two":
return 2;
case "three":
return 3;
case "four":
return 4;
case "five":
return 5;
case "six":
return 6;
case "seven":
return 7;
case "eight":
return 8;
case "nine":
return 9;
case "ten": case "jack": case "queen": case "king":
return 10;
default:
return 0;
}
}
public boolean isBlackjack(String card1, String card2) {
if (parseCard(card1) + parseCard(card2) == 21) {
return true;} else {
return false;
}
}
public String largeHand(boolean isBlackjack, int dealerScore) {
if (isBlackjack == true) {
if (dealerScore < 10) {
return "W";
} else {
return "S";
}
} else {
return "P";
}
}
public String smallHand(int handScore, int dealerScore) {
if (handScore > 16) {
return "S";
} else if (handScore < 12) {
return "H";
} else if (dealerScore > 6) {
return "H";
} else {
return "S";
}
}
// FirstTurn returns the semi-optimal decision for the first turn, given the cards of the player and the dealer.
// This function is already implemented and does not need to be edited. It pulls the other functions together in a
// complete decision tree for the first turn.
public String firstTurn() {
int handScore = parseCard(card1) + parseCard(card2);
int dealerScore = parseCard(dealerCard);
if (20 < handScore) {
return largeHand(isBlackjack(card1, card2), dealerScore);
} else {
return smallHand(handScore, dealerScore);
}
}
public static void main(String[] args){
Blackjack hand = new Blackjack("two", "queen", "seven");
System.out.println(hand.firstTurn());
}
}