Results 1 to 14 of 14
  1. #1
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default please help me with this 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. But i don't know how to compare arrays of two variable. Here is the original onw that i already had written:
    Java 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;
    
    }  
    }

  2. #2
    tfitz666 is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    Im a beginner myself, so this advice might not be the best, but if I was you I wouldn't represent the cards as strings because it makes comparing their suits and values difficult. Instead I would use enumerators or else make a playingCard class(card was already used) containing suit and value attributes.
    That's what I would do, but like I said that might not be the best way of doing it.

  3. #3
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    well if i use int then i wouldn't be able to write words. please help me

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by noobinoo View Post
    well if i use int then i wouldn't be able to write words. please help me
    If you'd use enums you would be able to 'write words'.

    kind regards,

    Jos

  5. #5
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    uh... what is enums i didn't study that yet sorry :D.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by noobinoo View Post
    uh... what is enums i didn't study that yet sorry :D.
    Read this.

    kind regards,

    Jos

  7. #7
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    well can you tell me how to compare those ? i mean if i change it into enums then i still have to compare the arrays right?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by noobinoo View Post
    well can you tell me how to compare those ? i mean if i change it into enums then i still have to compare the arrays right?
    If you have implemented a hand or deck of cards as an array then, yes you in fact have to compare enums.

    kind regards,

    Jos

  9. #9
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    so how do i compare them with my original code? i really need it right now so i can't just change it to enums and change the others too. so can you show me how to compare the suit or the value? you don't have to write the code for me, just give me an example. :D thanks

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by noobinoo View Post
    so how do i compare them with my original code? i really need it right now so i can't just change it to enums and change the others too. so can you show me how to compare the suit or the value? you don't have to write the code for me, just give me an example. :D thanks
    Also read the link I gave you; this is a minimal example:

    Java Code:
    public class Cards {
    
    	enum ENUM { CLUBS, DIAMONDS, HEARTS, SPADES };
    	
    	public static void main(String args[]){
    		ENUM suit1 = ENUM.DIAMONDS;
    		ENUM suit2 = ENUM.SPADES;
    
    		System.out.println(suit1.compareTo(suit2));
    	}
    }
    kind regards,

    Jos

  11. #11
    noobinoo is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    thank you very much but can you give me an example that use if and else because my level is not that high. and not with enums
    Last edited by noobinoo; 03-28-2010 at 01:41 PM.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by noobinoo View Post
    thank you very much but can you give me an example that use if and else because my level is not that high. and not with enums
    Did you read the link I gave you?

    kind regards,

    Jos

  13. #13
    tfitz666 is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    At the moment you cant really compare using if and else(or anything else for that matter) without doing a lot of work breaking up the string. If you're dead set against using enums try implementing a card class like this:
    Java Code:
    public class Card{
         private String value;
         private String suit;
         
         public Card( String value, String suit){
               this.value = value;
               this.suit = suit;
         }
         
         /* Getters and Setters here*/
    }
    Then compare like this:
    Java Code:
           Card carda = new Card( "8", "Diamonds" );
           Card cardb = new Card( "Jack", "Hearts" );
    
           if( carda.getValue().equals( cardb.getValue() ) && carda.getSuit().equals( cardb.getSuit() ) ){
                 Do Stuff!
    }
    Last edited by tfitz666; 03-28-2010 at 02:03 PM. Reason: formatting

  14. #14
    tfitz666 is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. 4x4 2D array card game,need solving
    By highschool in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 03:36 PM
  2. Replies: 44
    Last Post: 10-19-2009, 08:07 PM
  3. Creating a Card Game in Java
    By Natrix in forum New To Java
    Replies: 1
    Last Post: 05-05-2009, 05:55 PM
  4. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:53 PM
  5. A Online Card Game
    By GonzaloP in forum Networking
    Replies: 0
    Last Post: 12-28-2008, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •