As the topic may explain, I need to figure out if out of the five cards dealt to the player are any of them considered a pair, If there is a pair it would be printed as part of the final JOptionPane to state "You have a pair of [pair]" Is there a way to do this with the way i have the strings set up currently or will they not be recognized as pairs because they would never be the same suit...
thank you for any help.
here is my code right now:
int cardsPerPlayer = 5;
int players = 1;
String[] suit = {" of Spades", " of Diamonds", " of Clubs", " of Hearts"};
String[] face = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
String [] deck = new String[52];
for (int i = 0; i < deck.length; i++){
deck [i] = face[i%13] + suit[i/13];
//System.out.println(deck[i]);
}
for (int i = 0; i <deck.length; i++){
int index = (int) (Math.random()*deck.length);
String temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for (String u: deck) {
//System.out.println(deck);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < players * cardsPerPlayer; i++) {
sb.append(deck[i])
.append("\n");
}
JOptionPane.showMessageDialog(null, sb.toString());
}
}
It might not be the most elegant solution, but this'll work:
//Create an array with an integer for all the different faces
int[] amount = new int[face.length];
//set all those values to 0
for(int i = 0; i < amount.length;i++) amount[i] = 0;
//loop through the five cards
for(int i = 0; i < cardsPerPlayer;i++) {
//setting the faceIndex to 0, which will later be used to see what face it is (so if its a 2 it will have face 0, just like the array
int faceIndex = 0;
//looping through all the faces
for(int j = 0; j < face.length;j++) {
//detecting if the jth item in the face array matches the first character of the current card
if(face[j].equals(Character.toString(deck[i].charAt(0)))) {
//make the faceIndex that number
faceIndex = j;
}
}
//so now faceIndex is the index of the face in the array
//so if the face is 2, faceIndex is 0
//if the face is K, the faceIndex is 11 etc.
//we add one to that item in the amount array
amount[faceIndex]++;
}
//now we know how much of each face is in the dealt cards
//looping through all the amounts of cards
for(int i = 0; i < amount.length; i++) {
//if the amount of cards with the current face is 2, we have a pair! (which we print)
if(amount[i] == 2) {
System.out.println("You have a pair of " + face[i] + "s!");
}
}
If you want to make a pair also include more than 2 of the same face, you shoukd change if(i == 2) {
to if(i >= 2) {
.