Comparing array values for card game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blazinhieu
    New Member
    • Mar 2010
    • 1

    #1

    Comparing array values for card game

    I want to double the score if the cards have the same suit or value and if two cards have the same both suit and value, the score get tripled. And the 2 of clubs is a 20 points. i already wrote the basics of the program which it adds two random cards together and whoever get the higher score win.:

    Code:
    class Card{
     GetCards gc = new GetCards();
    public static void main(String []args){
      new Card(); 
    }  
    public Card(){
    int c1, c2, c3, c4=0;
    System.out.println("COMPUTER CARDS: ");
    c1 = getCardValue();
    c2 = getCardValue();
    System.out.println("Computer score: "+(c1+c2));
    System.out.println("YOUR CARDS: ");
    c3 = getCardValue();
    c4 = getCardValue();
    System.out.println("Your Score: "+(c3+c4));
      if ( (c1+c2)>(c3+c4))
     System.out.println("You Lose");
    else if ( (c1+c2)<(c3+c4))
    System.out.println("YOu win");
    else System.out.println(" Draw");
    }
    public int getCardValue(){
    String card = gc.finalCard();
    System.out.println(card+": ");
    int value = gc.i;
    if(value>9)value=10;
    else if(value==0)value=11;
    else value++;
    return value;   
    }
    }
    class GetCards{
    int i,j;
    String card;
    GetCards(){}
    public String finalCard(){
           String []suit = {"Hearts", "Clubs", "Diamonds", "Spades"};
          String []value = {"Ace","1","2","3","4","5","6","7","8","9","Ten","Jack","Queen","King"};
     i= (int) (Math.random()*value.length);
    card=value[i];
    j= (int) (Math.random()*suit.length);
    card=card+"of "+suit[j];
    return card;
    
    }  
    }


    i have trouble define an array of a variable, i mean i don't know how to compare two arrays of two cards. please help me thanks
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    1. Note, you have 1 and Ace; you probably want to remove "1".
    2. Your function names don't match up at all.
    3. Ideally, you would seperate your card functions into the following:
    A. Create a card. This includes setting suit and card, as well as the string.
    B. Get the string of the card.

    4. You could have a card comparison function, eg.
    Code:
    static boolean SameSuit(Card c1, Card c2){
      return c1.j=c2.j
    }
    5. Your class should be named Card. GetCard could be a function in the class.
    6. Rename the members to something meaningful instead of i and j. eg value, suit.
    7. If you want to have a multiplier, have a function which computes the total score of two cards, eg:
    static int HandValue(Card c1, Card c2)

    Comment

    Working...