String[] Array variable comparison

64 views Asked by At

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

There are 2 answers

0
Darren Bishop On

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.

0
queeg On

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