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;
}
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.