- #1
Arnoldjavs3
- 191
- 3
Homework Statement
Hi, so I'm working on a project(part 1) where I am supposed to create a simple card game, under a few requirements.
Here are the requirements:
1. Write a method boolean hasFlush(String hand) that checks whether the five-card poker hand given as 10-character string is a flush, that is, all five cards are of the same suit. (Hint: the ranks don’t matter for this problem since we don’t care about straight flushes. The suits are given in the positions 1, 3, 5, 7 and 9 of the parameter string.)
2. determine whether the given five-card poker hand contains four of a kind, that is, four cards of the same rank. Write a method boolean hasFourOfAKind(String hand) to determine this. (Now the suits don’t matter, and the ranks are given in positions 0, 2, 4, 6 and 8 of the parameter string.
3. Write a method boolean hasFourCardBadugi(String hand) that checks whether the given four-card hand is a badugi, that is, does not contain two cards of the same rank, nor two cards of the same suit. For example, given the hand “2d7cKhJs” the method would return true, but given the hands “2d2cKhJs” (that has two deuces) or “2d7cKhJd” (that has two diamonds), the method would return false
4. In a trick-taking card game, four players each play one card in turn. The trick is taken by the highest card played in the suit of the first card. (Ace counts as the highest card, and for simplicity, there are no jokers, trump suits or bowers that some card games have.) Write a method String winningCard(String play) that returns the card that won the trick, when the play to the trick is given as an 8-character string such as “5hKc2dJh”. For this example hand, this method would return “Jh”Questions at hand here:
1. Just to confirm here, with the 2nd requirement, i'd like to believe that this is in essence the same as requirement 1 in logic? Just with different indexes as it's looking for rank.
2. The last requirement is the hardest for me. How can I change my code to actually print a string(the winner) that shows the rank and the suit? On top of this, the hierarchy I've established for the cards seems faulty to me as well. Is there a different hierachy between suits? My professor instructed me not to do error-handling but I'm still unsure here.
3. If you can spot any obvious mistakes please let me know.
Thanks
2. Homework Equations
An individual playing card is represented as a string of two characters so that the first character is the rank (from “23456789TJQKA”) and the second character is the suit (from “cdhs”). For example, “Jd” would be the jack of diamonds, and “4s” would be the four of spades. A hand made up of multiple cards would be given as string containing those cards, such as “Kh3h7s8h2h” for a five-card hand. Note that the cards in the string can be given in any order, not necessarily sorted by suit or rank. The suits and ranks are also case-sensitive, with the rank always given as a digit or an uppercase letter, and the suit always given as a lowercase letter.
The Attempt at a Solution
Java:
public class CardProblemss {
String hand = "Kh3h7h8h2h";
String play = "KhQc3s2h";
public static void main(String[] args) {
System.out.println("NsadfRRS");
}
public boolean hasFlush(String hand) {
int flushcounter = 0;
char s = 0;
for (int i = 1; i < hand.length(); i = i + 2) {
if (i == 1) {
s = hand.charAt(1);
} else if (s == hand.charAt(i)) {
flushcounter++;
}
}
if (flushcounter >= 5) {
return true;
} else {
return false;
}
}
public boolean hasFourOfaKind(String hand) {
int kindcounter = 0;
char r = 0;
for (int i = 0; i < hand.length(); i = i + 2) {
if (i == 0) {
r = hand.charAt(0);
} else if (r == hand.charAt(i)) {
kindcounter++;
}
}
if (kindcounter >= 4) {
return true;
} else {
return false;
}
}
public boolean hasFourCardBadugi(String hand) {
int diffcounter = 0;
char badugi = 0;
for (int i = 0; i < hand.length(); i++) {
if (i == 0) {
badugi = hand.charAt(0);
} else if (badugi != hand.charAt(i)) {
diffcounter++;
}
}
if (diffcounter >= 10) {
return true;
} else {
return false;
}
}
public String winningCard(String play) {
char highestcard = play.charAt(0);
char T = 10;
char J = 11;
char Q = 12;
char K = 13;
char A = 14;
for (int i = 0; i < play.length(); i = i + 2) {
if (highestcard < play.charAt(i)) {
highestcard = play.charAt(i);
} else if (highestcard > play.charAt(i)) {
highestcard = play.charAt(0);
}
}
String winner = String.valueOf(highestcard);
return winner;
}
}
Last edited: