# Homework Help: Simple cardgame project in Java

1. Sep 29, 2016

### Arnoldjavs3

1. The problem statement, all variables and given/known data
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. Relevant 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.

3. The attempt at a solution

Code (Java):

public class CardProblemss {

String hand = "Kh3h7h8h2h";
String play = "KhQc3s2h";

public static void main(String[] args) {

}
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;
}

}

int diffcounter = 0;

for (int i = 0; i < hand.length(); i++) {
if (i == 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: Sep 29, 2016
2. Sep 29, 2016

### Arnoldjavs3

Going to update the original post with the entirety of the project.

3. Sep 29, 2016

### Staff: Mentor

For your hasFlush() method, set s to the suit of the first card (i.e., index 1 of your string). Loop through the rest of the hand, incrementing flushCounter if there's a match, but exiting early if there is no match (for a flush, all five cards have to be the same suit). If at the end of the loop flushCounter == 5, the hand is a flush. Note that flushCounter won't ever by > 5 -- you're checking flushCounter >= 5.

The method for four of a kind is more complicated, because you have 13 different face values -- A, 2, 3, 4, ..., 9, 10, J, Q, K. For four of a kind, with a five-card hand, there are five possible ways that the hand could contain four cards of the same suitrank. So a hand with 5 aces could look like XAAAA, AXAAA, AAXAA, AAAXA, or AAAAX, with X representing a card that isn't an ace. Same thing for deuces, treys, and so on, up thrrough jacks, queens, and kings.

Last edited: Sep 29, 2016
4. Sep 29, 2016

### Arnoldjavs3

In regards to the four of the kind, I'm a bit confused. It says that the suit is irrelevant so wouldn't I just have it check at every index seeing if they share the same char?(In this case, start at 0 and work my way up to 8)
And then of which it would check if the counter is at 4 and proceed to returning true if so.

5. Sep 29, 2016

### Staff: Mentor

I meant rank, but inadvertently wrote suit. I've changed this in my earlier post.
But you might have four cards of the same rank, but they don't start at the first card. In other words, if you are comparing the 2nd, 3rd, 4th, and 5th cards against the 1st one, they won't match, but cards 2, 3, 4, and 5 might match. Are you taking this into account?

6. Sep 29, 2016

### Arnoldjavs3

Ah, I see. So what i can do is write another else if checking if the other cards match? SOmething like:

Code (Java):

for (int i = 0; i < hand.length(); i = i + 2) {
if (hand.charAt(0) == hand.charAt(2)) {
r = hand.charAt(0);
} else if (hand.charAt(2) == hand.char(4)) {
r = hand.charAt(2)
} else if (r == hand.charAt(i)) {
kindcounter++;
}
}

If index 0 and 2 don't match, then it'll proceed to check if index 2 and 4 match. If both conditions aren't fulfilled then it should return as false I'd like to think?

7. Sep 30, 2016

### Staff: Mentor

This code doesn't work. If the first if condition is false, control goes to the else condition. If it's false as well, control goes to the next else condition, . else if (r == hand.charAt(i)). At this point r hasn't been set yet, so you're comparing an uninitialized variable, r, to what's stored in hand.charAt(i). Not good.

Here's an example string where the first two conditions are false: "K_A_K_K_A_" The _ characters are the suits, which we don't care about here.

8. Sep 30, 2016

### Arnoldjavs3

Hmm... would writing more conditions alleviate this issue? Or would that be considered inefficient to do? I'm not too comfortable with java's syntax yet so I'm not sure.

9. Oct 2, 2016

### Staff: Mentor

One approach is to have a loop that runs 5 times (once for each card). On each iteration, determine whether the letter you are looking at represents an ace, deuce, trey, ..., ten, jack, queen, or king. When you get a match, increment a counter for that rank. You'll need 13 variables, one for each rank.

Do you know about switch statements yet? If you do, what I'm proposing is a natural for this control structure. If you don't know about switch yet, you can do it with an if ( ) else if () else if() ... else control structure.

Last edited: Oct 2, 2016
10. Oct 3, 2016

### Arnoldjavs3

The deadline to hand in this lab was extended to this Saturday as apparently I'm not the only one who was having difficulties(poorly designed lab? :D) With that being said, is there any way I can benefit off of comprehending the JUnit test that my professor uses to grade our labs? My current lab fails all his tests which leaves me wondering what the heck is going on.

11. Oct 3, 2016

### Staff: Mentor

I have no idea what a JUnit test is.

The best advice I can give is to write your program so that it meets the specifications you gave in post #1. If for some reason it fails the test you mentioned, talk to your instructor to see why your program is faililng the test.

12. Oct 3, 2016

### Arnoldjavs3

I apologize I didn't see your entire message from earlier, and no I have not used switch statements yet. But I do know the general idea behind them from working with C and I don't see any reason why he shouldn't permit me to use them. The JUnit is a test that just runs random values in to my code and sees if it has acceptable output frmo what I understand - so judging from that if the input -> satisfies his output then it shouldn't matter?

@ your algorithm, are you saying that every time a rank(having quantified all 13) matches another, the counter goes up? And once it reaches a certain threshold, the program should state that there is a four of a kind?(and vice versa).

Last edited: Oct 3, 2016
13. Oct 3, 2016

### Staff: Mentor

As long as you do some throrough testing of your code, it shouldn't matter.

No, that's not what I'm saying at all. There's no matching going on. All I'm doing in my suggestion is seeing how many cards of each rank there are. I'm suggesting 13 variables, one for each rank, with all of them initialized to zero at the start. If the hand looks like 5, 3, J, 2, J, after all five cards have been looked at, fiveCount should be 1, threeCount should be 1, jackCount should be 2, and twoCount should be 1. All of the other variables for counting ranks should still be zero.
After inspecting all five cards, you will need logic that checks each of the variables to see if it is four or larger.