Simple cardgame project in Java

  • Comp Sci
  • Thread starter Arnoldjavs3
  • Start date
  • Tags
    Java Project
In summary: NsdafRRS"); } }In summary, the program checks to see if the given five-card poker hand contains a flush, and also checks to see if the given four-card hand is a badugi. If either condition is met, then the program returns the correct string.
  • #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:
Physics news on Phys.org
  • #2
Going to update the original post with the entirety of the project.
 
  • #3
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:
  • #4
Mark44 said:
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 suit. 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.
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
Arnoldjavs3 said:
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?
I meant rank, but inadvertently wrote suit. I've changed this in my earlier post.
Arnoldjavs3 said:
(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.
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?
 
  • Like
Likes Arnoldjavs3
  • #6
Ah, I see. So what i can do is write another else if checking if the other cards match? SOmething like:

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
Arnoldjavs3 said:
Ah, I see. So what i can do is write another else if checking if the other cards match? SOmething like:

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?
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
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
Arnoldjavs3 said:
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.
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:
  • Like
Likes Arnoldjavs3
  • #10
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
Arnoldjavs3 said:
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.
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
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 into 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:
  • #13
Arnoldjavs3 said:
The JUnit is a test that just runs random values into 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?
As long as you do some throrough testing of your code, it shouldn't matter.

Arnoldjavs3 said:
your algorithm, are you saying that every time a rank(having quantified all 13) matches another, the counter goes up?
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.
Arnoldjavs3 said:
And once it reaches a certain threshold, the program should state that there is a four of a kind?(and vice versa).
 

1. What is a simple card game project in Java?

A simple card game project in Java is a computer program that allows players to play a card game on their device. It involves creating a deck of cards, shuffling them, and implementing game rules using Java programming language.

2. What are the basic components of a simple card game project in Java?

The basic components of a simple card game project in Java include a deck of cards, players, game logic, and a user interface. The deck of cards should be able to generate and shuffle cards, while the game logic should handle the rules and actions of the game. The user interface is responsible for displaying the game and receiving input from players.

3. How do I create a deck of cards in Java?

To create a deck of cards in Java, you can use arrays, collections, or custom classes. One way is to create an array or ArrayList of card objects, where each card has a suit and a rank. You can then use a loop to generate all the cards and add them to the deck.

4. How can I implement game logic in a simple card game project in Java?

The best way to implement game logic in a simple card game project in Java is to use conditional statements and loops. You can set up rules for playing the game, such as how cards are dealt, how points are scored, and how the game is won. You can also use methods to handle specific actions, such as checking for a valid move or determining the winner.

5. Can I add more features to my simple card game project in Java?

Yes, you can add more features to your simple card game project in Java, such as multiplayer options, different game modes, or a better user interface. You can also make your game more challenging by adding more complex rules or by incorporating different types of cards. The possibilities are endless, and you can continuously improve your project with more advanced programming techniques.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
939
  • Engineering and Comp Sci Homework Help
Replies
1
Views
851
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top