1

I am writing a code that vaguely simulates a poker game, which at this point should output a "Players Hand", an "Opponents Hand" and a "Flop".

Each card should only appear once (that means no identical cards should be given out).

I tried to accomplish this by storing the individual cards in Arrays which I then compare with each other, and if one of them is identical it should asign a new value until it is nor identical.

Somehow it doesn't work how it supposed to do. Namely my if(stringArr[x]==stringArr[y]) statements dont compare the String values like I want.

What am I missing?

<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>

// meine = mine, gegner = opponent (German = English)

import java.util.Random;

public class PokerGame 
{
    public static void main(String[] args)
    {
        int cards = 2; // Texas Hold em is played with 2 cards
        int flopCards = 3;  // Texas Hold em has 2 cards on the flop

        String meine = dealer(cards);
        String gegner = dealer(cards);
        String flop = dealer(flopCards);

        String[] meineArr = meine.split(" ");
        String[] gegnerArr = gegner.split(" ");
        String[] flopArr = flop.split(" ");

        
        if( 
            meineArr[0]!=meineArr[1] &&
             meineArr[0]!=gegnerArr[0] && 
             meineArr[0]!=gegnerArr[1] && 
             meineArr[1]!=gegnerArr[1] && 
             meineArr[1]!=gegnerArr[0] &&
                flopArr[0]!=flopArr[1] && 
                 meineArr[0]!=flopArr[0] && 
                 meineArr[0]!=flopArr[1] && 
                 meineArr[1]!=flopArr[1] && 
                 meineArr[1]!=flopArr[0] &&
                    gegnerArr[0]!=gegnerArr[1] &&
                     gegnerArr[0]!=flopArr[0] &&
                     gegnerArr[0]!=flopArr[1] &&
                     gegnerArr[1]!=flopArr[1] &&
                     gegnerArr[1]!=flopArr[0] &&
                     gegnerArr[0]!=flopArr[2] &&
                     gegnerArr[1]!=flopArr[2] &&
                            flopArr[0]!=flopArr[1] &&
                            flopArr[0]!=flopArr[2] &&
                            flopArr[1]!=flopArr[2] &&
                            flopArr[2]!=flopArr[1] &&
                            flopArr[2]!=flopArr[0]
                        )
        {
                  System.out.println("Your Hand:        "
                                    + meine + "\n\n"+ "Opponent's Hand: " 
                                    +gegner+ "\n\n"+ "FLOP:                 "+flop);
        }else {
            while(meineArr[0]==meineArr[1] ||
                   meineArr[0]==gegnerArr[0] || 
                   meineArr[0]==gegnerArr[1] || 
                   meineArr[1]==gegnerArr[1] || 
                   meineArr[1]==gegnerArr[0] ||
                        flopArr[0]==flopArr[1] || 
                         meineArr[0]==flopArr[0] || 
                         meineArr[0]==flopArr[1] || 
                         meineArr[1]==flopArr[1] || 
                         meineArr[1]==flopArr[0] ||
                            gegnerArr[0]==gegnerArr[0] || 
                             gegnerArr[0]==flopArr[0] || 
                             gegnerArr[0]==flopArr[1] || 
                             gegnerArr[1]==flopArr[1] || 
                             gegnerArr[1]==flopArr[0] ||
                             gegnerArr[0]==flopArr[2] ||
                             gegnerArr[1]==flopArr[2] ||
                                    flopArr[0]==flopArr[1] ||
                                    flopArr[0]==flopArr[2] ||
                                    flopArr[1]==flopArr[2] ||
                                    flopArr[2]==flopArr[1] ||
                                    flopArr[2]==flopArr[0]){

                        gegner = dealer(cards);
                        gegnerArr = gegner.split(" ");

                        System.out.println("Your Hand:      "
                                    + meine + "\n\n"+ "Opponent's Hand: " 
                                    +gegner+ "\n\n"+ "FLOP:                 "+flop);
                        
            }

        }
        System.out.println("\n\n\n"+legend());
    }

    static String legend(){
        String legend = "A = Ace        h = hearts \n"
                       +"K = King       c = clubs\n"
                       +"Q = Queen      d = diomonds\n"
                       +"J = Jack       s = spades\n"
                       +"T = 10";
        return legend;
    }

    static String dealer(int len)
    {
        String value = "23456789TQJKA";
        String face = "hcds";
        
        Random rand = new Random();
        
        char[] handvalue = new char[len];
        char[] handface = new char[len];

        int index = 0;

        String generatedHand = "";

        for (int i = 0; i < len; i++) 
        {
            handvalue[i] = value.charAt(rand.nextInt(value.length()));
            generatedHand += handvalue[i];
            for (int j = 0; j < 1;j++ ){     
                handface[i] = face.charAt(rand.nextInt(face.length()));
                generatedHand += handface[i];
            }
            generatedHand += " ";
        }
   
        return generatedHand;
    }

    
}

I tried playing around with the condtitional statements with no success. I suspect the problem lies somewhere in the comparison between the stored String values (the cards) within the StringArrays containing them.

2
  • You are comparing the card with the entire deck because you want to generate a deck of unique cards, am I right? If that is the case, there is no need to do any comparison at all. Generate all cards sequentially. If needed to shuffle the deck of cards, shuffle the deck afterwards. Commented Dec 21, 2022 at 6:41
  • The indentation of your code is pretty wonky. Could you clean it up? Commented Dec 21, 2022 at 8:24

2 Answers 2

0

As your commentator suggested, get a representation of a deck of cards, then shuffle them by some means of your choosing (which loosely models the real world); the following should allow you to obtain a shuffled deck of cards:

import java.util.Random;

static String[] VALUES = "Ace 2 3 4 5 6 7 8 9 10 Jack Queen King".split(" ");

static String[] SUITS = "Hearts Clubs Diamonds Spades".split(" ");

static int[] newDeck() {
  int[] cards = new int[52];
  for (int i = 0; i < cards.length; i++) {
    cards[i] = i+1;
  }
  return cards;
}

static void shuffle(long seed, int[] cards) {
  System.err.printf("Shuffling deck with seed: %d", seed);
  Random rand = new Random(seed);
  for (int i = 0; i < cards.length; i++) {
    int j = rand.nextInt(cards.length);
    int k = rand.nextInt(cards.length);
    int card = cards[k];
    cards[k] = cards[j];
    cards[j] = card;
  }
}

static String repr(int card) {
  return String.format("%s of %s", VALUES[(card - 1) % VALUES.length], SUITS[(card - 1) % SUITS.length]);
}

Trying it in jshell:

long seed = new Random().nextLong();
int[] cards = newDeck();
shuffle(seed, cards);
shuffle(seed, cards);
cards;

cards = newDeck();
shuffle(seed, cards);
shuffle(seed, cards);
cards;

seed = new Random().nextLong();
cards = newDeck();
shuffle(seed, cards);
shuffle(seed, cards);
cards;

seed = -2556576281960593300L;
cards = newDeck();
shuffle(seed, cards);
shuffle(seed, cards);
cards;

So you have a deck of cards now, deal them out by sequentially stepping through the cards array, say with index i:

repr(cards[i]) // ==> "Ace of Diamonds" if cards[i] == 27

The key insight here is to use java.util.Random to shuffle; using seed appropriately will facilitate easier unit-testing.

Sign up to request clarification or add additional context in comments.

Comments

0

Your attempt to find a card in a deck is a bit clumsy.

    String[] meineArr = meine.split(" ");
    String[] gegnerArr = gegner.split(" ");
    String[] flopArr = flop.split(" ");

    
    if( 
        meineArr[0]!=meineArr[1] &&
         meineArr[0]!=gegnerArr[0] && 
         meineArr[0]!=gegnerArr[1] && 
         meineArr[1]!=gegnerArr[1] && 
         meineArr[1]!=gegnerArr[0] &&
  • Be aware that string comparison will not work as expected using ==/!=. You would have to run .equals().

  • Rather than doing so many single comparisons in lines of code, use loops:

    String card = meineArr[0]; for (String other: gegnerArr) { if (card.equals(other)) { // we found the same card again? } }

  • But since you store a deck as a string and your cards are listed in there, the search for a card could be as simple as

    String card = meineArr[0]; if (gegner.contains(card)) { // we found the same card again? }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.