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.
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:
Trying it in
jshell
:So you have a deck of
cards
now, deal them out by sequentially stepping through the cards array, say with indexi
:The key insight here is to use
java.util.Random
to shuffle; usingseed
appropriately will facilitate easier unit-testing.