Consider a StringListener: public interface StringListener{ void OnEvent(string
ID: 3810481 • Letter: C
Question
Consider a StringListener:
public interface StringListener{
void OnEvent(string data);
}
Cards are dealt indefinitely by a dealer from a typical deck of playing cards without jokers. When a card is dealt, a StringListener is used as a callback, passing in data as “X of Y” where X represents the numerical value of the card and Y represents the suite of the card. X will be a number unless it is an 1, 11, 12, 13. In those cases, X will be “Ace”, “Jack”, “Queen”, or “King” respectively. Y can be “Hearts”, “Diamonds”, “Clubs”, or “Spades”.
Implement a StringListener that will print out when the 5 most recent cards contain a straight or a flush. A straight is a set of cards with unique numerical values that can be reordered into a consecutive sequence with the possibility of King wrapping around to Ace. A flush is a set of cards that that have the same suite.
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
public class StringListener {
private String numberToString[] = {"", "Ace", "2", "3", "4", "5", "6", "7", "8", "9","Jack", "Queen", "King"};
private List<Card> slidingList = new ArrayList<Card>();
public void onEvent(String data) {
/*
* Parse the string to get the value and suite,
* Using integer values for [KING, QUEEN, JACK, ACE]
*/
String[] splittedString = data.split(" ");
int value = getNumericValue(splittedString[0].trim());
String suite = splittedString[2].trim();
if (value <= 0) {
System.out.println(" Invalid Input Data ");
return;
}
/*
* Create a new card with th egiven details, and add it to the front of
* the list If the size of list is more than 5, remove elements from end.
*/
Card card = new Card(value, suite);
slidingList.add(0, card);
if (slidingList.size() > 5) {
slidingList.remove(slidingList.size()-1);
}
/*
* print out when the 5 most recent cards contain a straight or a flush
*/
if (checkStraightOrFlush()) {
for (int i=0; i<5; i++) {
System.out.println(slidingList.get(i).toString());
}
}
System.out.println(System.lineSeparator());
}
public boolean checkStraightOrFlush() {
if (slidingList.size() < 5) {
return false;
}
boolean isStraight = true;
int previousVal = slidingList.get(4).value;
for (int i=3; i>=0; i--) {
if (previousVal+1 != slidingList.get(i).value) {
isStraight = false;
break;
}
}
boolean isFlush = true;
String previoussuite = slidingList.get(0).suite;
for (int i=1; i<5; i++) {
if (!previoussuite.equalsIgnoreCase(slidingList.get(i).suite)) {
isFlush = false;
break;
}
}
return isFlush || isStraight;
}
public int getNumericValue(String stringValue) {
for (int i=1; i<numberToString.length; i++) {
if (numberToString[i].equalsIgnoreCase(stringValue)) {
return i;
}
}
return -1;
}
private class Card {
Integer value;
String suite;
public Card(int value, String suite) {
this.value = value;
this.suite = suite;
}
@Override
public String toString() {
return "Card - [value = "+value+", suite "+suite+"]";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.