how to get the sum of the two random value from an array of strings in java?

658 Views Asked by At

im working on a card pair game that generates two random value from an array of strings.. what i want to know is how to get the sum of two random values from the array of strings to determine the winner. here are the codes

import java.util.*;

public class Cards {
private String suit;
private String face;
private String[] cardSuits;
private String[] cardFaces;
private Random ran;

public Cards() {
    ran = new Random();
    cardSuits = new String[] { "of Spade", "of Hearts", "of Diamonds",
            "of Clubs" };
    cardFaces = new String[] { "Ace", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "Jack", "Queen", "King" };

}

public String setPlayerCardSuit() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setPlayerCardFace() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public String setPlayerCardSuit2() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setPlayerCardFace2() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public String setCompCardSuit() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setCompCardFace() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public String setCompCardSuit2() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setCompCardFace2() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public void getResults() {
    System.out.println("Here are your cards: " + setPlayerCardFace() + " "
            + setPlayerCardSuit() + " and " + setPlayerCardFace2() + " "
            + setPlayerCardSuit2());
}

public void getCompCard() {
    System.out.println("Here's the computer's cards: " + setCompCardFace()
            + " " + setCompCardSuit() + " and " + setCompCardFace2() + " "
            + setCompCardSuit2());
}

}

and here is the code to test the Cards Class:

import javax.swing.JOptionPane;

public class TestCards {
public static void main(String[] args) {
    Cards playerCards = new Cards();
    Cards computerCards = new Cards();

    int confirm, x = 1;
    while (x == 1) {
        JOptionPane.showMessageDialog(null,
                "Random Card game \nPlease press OK to Start Game",
                "Card Pair Game", JOptionPane.INFORMATION_MESSAGE);

        JOptionPane.showMessageDialog(
                null,
                "Here are your Cards: " + playerCards.setPlayerCardFace()
                        + " " + playerCards.setPlayerCardSuit() + " and "
                        + playerCards.setPlayerCardFace2() + " "
                        + playerCards.setPlayerCardSuit2()
                        + "\nThe Computer's Cards are: "
                        + computerCards.setCompCardFace() + " "
                        + computerCards.setCompCardSuit() + " and "
                        + computerCards.setCompCardFace2() + " "
                        + computerCards.setCompCardSuit2());


        confirm = JOptionPane.showConfirmDialog(null, "Game Ends. Again?",
                "Game Over", JOptionPane.YES_NO_OPTION);

        if (confirm != JOptionPane.YES_OPTION) {
            x = 2;
        }
    }
}
}

what lacks now is the code to determine the winner.

PS: im a beginner in java programming.. so please bear with me if u see an unusual use of codes :)


i have tried Dylan's suggestion but i can't seem to make it work.. instead used his idea and added this code to Cards class.

public int playerValues(){
    int temp = 0;
    if(face != cardFaces[0] && face != cardFaces[10] && face != cardFaces[11] && face != cardFaces[12]){
        temp = Integer.parseInt(face);
    }else if(face == cardFaces[0]){
        temp = 1;
    }else if(face == cardFaces[10]){
        temp = 11;
    }else if(face == cardFaces[11]){
        temp = 12;
    }else if(face == cardFaces[12]){
        temp = 13;
    }
    return temp;
}
public int computerValues(){
    int temp = 0;
    if(face != cardFaces[0] && face != cardFaces[10] && face != cardFaces[11] && face != cardFaces[12]){
        temp = Integer.parseInt(face);
    }else if(face == cardFaces[0]){
        temp = 1;
    }else if(face == cardFaces[10]){
        temp = 11;
    }else if(face == cardFaces[11]){
        temp = 12;
    }else if(face == cardFaces[12]){
        temp = 13;
    }
    return temp;
}
6

There are 6 best solutions below

0
On

I have managed to determine the winner by modifying the TestCard class instead of showing two random values from the array of strings. I changed it so that it shows only one value from the array. With that I managed to determine who the winner is.

1
On

You have a problem that not all the cards are unique. Two players can have the same cards. They could all be the same card even.

Instead you should generate a list of all the possible cards. I suggest using a Card class. Use Collections.shuffle to shuffle them. This way all the players will have different cards.

You need rules for comparing a set of cards. Define them in English first, and then translate them into code.

1
On

I would really suggest that your Card be a class or at least an enum. Then summing them will make sense.

Also, please explain these two

public String setCompCardFace()
public String setCompCardFace2()  //identical

Pretty much what you need is for each player to have an instance of Cards much like you started

public class Cards extends ArrayList<Card>

but somewhere along the way you got lost and mixed up these two into one class that does nothing really.

so in your Card you put these

public class Card 
{
    private String suit;
    private String face;

    public Card(String face, String color)
    {
        suit = color; this.face = face;
    }

    @Override
    public int compareTo(Card otherCard); //implement this yourself
 }
0
On

You shouldn't use an array of Strings to hold the card faces, but an array of CardFace, having a name (for display), and a value (to compute the sum of the faces). And since there are only 13 values, it should be an enum:

public enum CardFace {
    ACE("Ace, 1),
    TWO("2", 2),
    ...
    KINK("King", 13);

    private String face;
    private int value;

    private CardFace(String face, int value) {
        this.face = face;
        this.value = value;
    }

    public String getFace() {
        return this.face;
    }

    public int getValue() {
        return this.value;
    }
}
4
On

Using the String.parseInt() method will return the number within a string. But, youll have issues with the ace,kind,queen,jack since they arent a number. i would Recommend creating a statement like:

if(!cardFaces[x].equals("Ace")&&!cardFaces[x].equals("Queen")&&!cardFaces[x].equals("King")&&!cardFaces[x].equals("Jack"))
  {
    int temp = cardFaces[x].parseInt();
   }
else if(cardFaces[x].equals("Ace")
{
  int temp = 1;
}
else if(cardFaces[x].equals("King") || cardFaces[x].equals("Queen") || cardFaces[x].equals("Jack"))
{
 int temp = 10;
0
On

It seems that no one has talked about getting combinations, so I'll talk about it here. If there's anything you don't understand, feel free to comment here.

Straight flush - you need a for loop.

First, you should sort an array of the player's cards with ascending (1, 2, 3, 4, 5 etc.). Now, check from the first entry, the lowest.

Note that the array cards stores only the values of the hand in integer.

// Variables to store the current and previous cards.
int previous = 0;
int current = 0; 

cards = Arrays.sort(cards); // Sort cards into ascending order

boolean straightFlush = false;

for (i = 0; i < cards.length; i++) {
    current = cards[i];
    if (!current == cards[0]) { // If current is not the first card, execute the rest. You don't want random errors popping up, do you? :)
        // Check if it is the last card - a straight flush would then be present
        if (current == cards[cards.length - 1]) {
            straightFlush = true;
            break;
        }

        // Checks if current - 1 != previous or the current card is not consecutive to the previous card
        if (current - 1 != previous) {
            break;
        }
    }
...

How to compare the hands then? Find the highest value among the cards if it is a straight flush.

I'll post more about this soon.