Results 1 to 6 of 6
  1. #1
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Deck output with Card class help

    Hi everyone,
    I'm having a really hard time with the whole concept of references, class parameters, static variables, and static methods(very broad I know, but I just need a little help.) What I'm trying to do is create the output of a game of bridge that references a class called Card.
    The output is supposed to look similar to this:
    East
    S: A 9 6 5
    H: 8 3 2
    D: 10 8 2
    C: A K 5

    South
    S: 8 7 2
    H: Q J 9 5
    D: A Q 5
    C: Q 8 6

    West
    S: K Q 10
    H: 10 7 4
    D: J 7 6
    C: J 10 7 2

    North
    S: J 4 3
    H: A K 6
    D: K 9 4 3
    C: 9 4 3


    The Card class I was given to use is as follows
    Java Code:
    public class Card {
    
        private int number;
    
        public Card() {
            number = 0;
        }
    
        public Card(int cardNumber) {
            setNumber(cardNumber);
        }
    
        public int getNumber() {
            return number;
        }
    
        public void setNumber(int cardNumber) {
            if (cardNumber < 0 || cardNumber > 51) {
                System.out.println("Error - setNumber(int)");
                System.exit(0);
            } else {
                number = cardNumber;
            }
        }
    
        public String toString() {
            // (2C), or (3C), or ... or (AS) is returned
            // based on the value of the instance variable
            return "(" + shortName() + ")";
        }
    
        public boolean equals(Card otherCard) {
            return number == otherCard.number;
        }
    
        public String shortName() {
            // returns the card's short name
            // 2C, or 3C, or ..., or AS
            int suit, rank;
            String rankChar = "23456789TJQKA";
            String suitChar = "CDHS";
            suit = number / 13;
            rank = number % 13;
            return rankChar.substring(rank, rank + 1)
                    + suitChar.substring(suit, suit + 1);
        }
    
        public String longName() {
            // returns the card's long name
            // Two of Clubs, or Three of Clubs, or ..., or Ace of Spades
            String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
            String[] rank = { "Two", "Three", "Four", "Five", "Six", "Seven",
                    "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
            int s = number / 13;
            int r = number % 13;
            String name = rank[r] + " of " + suit[s];
            return name;
        }
    
        public void writeCard() {
            System.out.println(this.shortName());
        }
    
        private static boolean[] used = new boolean[52];
        private static int numberUsed = 0;
    
        public static final int CLUBS = 0;
        public static final int DIAMONDS = 1;
        public static final int HEARTS = 2;
        public static final int SPADES = 3;
    
        public static void shuffle() {
            // initializes deck by setting all used[i] to false
            int i;
            for (i = 0; i < 52; i++) {
                used[i] = false;
            }
            numberUsed = 0;
        }
    
        public static Card next() {
            Card next = new Card();
            if (numberUsed < 52) {
                int r;
                do {
                    r = (int) (52 * Math.random());
                } while (used[r]);
                used[r] = true;
                next.setNumber(r);
                numberUsed++;
                return next;
            } else {
                System.out.println("Error - nextCard()");
                System.exit(0);
                return next;
            }
        }
    
        public static void sort(Card[] hand) {
            int n = hand.length;
            int i;
            int j;
            Card temp;
            for (i = 0; i < n - 1; i++) {
                for (j = i + 1; j < n; j++) {
                    if (hand[i].number > hand[j].number) {
                        temp = hand[i];
                        hand[i] = hand[j];
                        hand[j] = temp;
                    }
                }
            }
        }
    
        public int getSuit() {
            // returns 0, 1, 2, 3
            // corresponding to
            // Clubs, Diamonds, Hearts, Spades
            return number / 13;
        }
    
        public static String suitName(int c) {
            switch (c) {
            case CLUBS:
                return "Clubs";
            case DIAMONDS:
                return "Diamonds";
            case HEARTS:
                return "Hearts";
            case SPADES:
                return "Spades";
            default:
                System.out.println("Error - suitName(int)");
                System.exit(0);
                return "Error";
            }
        }
    
        public int getValue() {
            // returns 2, 3, 4, 5, 6, 7 ,8, 9, 10, 11, 12, 13, 14
            // corresponding to
            // 2, 3, 4, 5, 6, 7, 8, 9, Ten, Jack, Queen, King, Ace
            return number % 13 + 2;
        }
    
        public static void main(String[] args) {
    
    
        }
    }
    I don't know how to start this new class, so any help would be appreciated. I'm not asking for direct answers, just some guidance.
    Thanks in advance,
    Stephen

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Deck output with Card class help

    In order to play game, You need to have 52 cards to deal to four sides.

    1. You need 4 sides. How to create 4 sides?
    2. You have to create deck of 52 cards. How to create deck of cards?
    3. You have to deal cards from that deck. How to deal card?

  3. #3
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Re: Deck output with Card class help

    So this is what I have so far:
    Java Code:
    public class Assignment7 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    
    
    		int i;
    		String Scard = " ";
    		String Hcard = " ";
    		String Dcard = " ";
    		String Ccard = " ";
    Card card1 = new Card();
    
    
    
    
    	
    
    
    card1.shortName();
    card1.longName();
    card1.writeCard();
    card1.getSuit();
    if(card1.getSuit() == 3){
    Scard += card1.shortName().substring(0,1) + " ";
    }
    else if(card1.getSuit() == 2){
    	Hcard += card1.shortName().substring(0,1) + " ";
    }
    else if(card1.getSuit() == 1){
    Dcard += card1.shortName().substring(0,1) + " ";
    }
    else if (card1.getSuit() == 0){
    Ccard += card1.shortName().substring(0,1) + " ";
    }
    
    
    
    
    
    		System.out.println("S:" + Scard);
    		
    		System.out.println("H:" + Hcard);
    		
    		System.out.println("D:" + Dcard);
    		
    		System.out.println("C:" + Ccard);
    
    
    
    		
    	}
    
    }
    I think I've got the output done right and how to make sure cards of certain suits go to their respected output line. But the thing that's giving me trouble is how to reference the deck and shuffle the cards and then make it so the program can choose 13 cards from it.
    Any tips with that using the Card class I attached in the OP?

  4. #4
    Lowest0ne is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Deck output with Card class help

    Static member functions are accessed via the name of the class. Your third question is easiest to answer:
    Java Code:
    int deckSize = 52;
    Card[] cards = new Card[deckSize];
    for (int i = 0; i < deckSize; i++)
    {
      cards[i] = Card.next();
      System.out.println(cards[i]);
    }
    Change deckSize to 53, and your last line of output will be the error message "Error - next card".

    Shuffling the deck resets the used array to be all false:
    Java Code:
    Card.shuffle();
    I guess this doesn't display the shuffling, but here is how I would do 4 hands:
    Java Code:
        public static void main(String[] args) {
    		Card[][] hands = new Card[4][13];
    		for (int i = 0; i < 4; i++)
    		{
    			for (int j = 0; j < 13; j++)
    			{
    				hands[i][j] = Card.next();
    			}
    		}
    		
    		for (int i = 0; i < 4; i++)
    			Card.sort(hands[i]);
    			
    		for (int i = 0; i < 4; i++)
    		{
    			System.out.println("Hand Number " + i + ":");
    			for (int j = 0; j < 13; j++)
    			{
    				System.out.print(hands[i][j] + ", ");
    			}
    			System.out.print("\n\n");
    		}
      
        }

  5. #5
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Re: Deck output with Card class help

    Alright, well I tried what you suggested and modified it a bit to try and get my output.
    Java Code:
    public class Assignment {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String handname = null;
    		String Scard = " ";
    		String Hcard = " ";
    		String Dcard = " ";
    		String Ccard = " ";
    
    		
    		
    		Card[][] hands = new Card[4][13];
    	    for (int i = 0; i < 4; i++)
    	    {
    	        for (int j = 0; j < 13; j++)
    	        {
    	            hands[i][j] = Card.next();
    	        }
    	    }
    	     
    	    for (int i = 0; i < 4; i++)
    	        Card.sort(hands[i]);
    	         
    	    for (int i = 1; i < 5; i++)
    	    {
    	    	if (i == 1)
    	    	{handname = "East";}
    	    	else if(i==2)
    	    	{handname = "South";}
    	    	else if(i==3)
    	    	{handname = "West";}
    	    	else if(i==4)
    	    	{handname = "North";}
    	    	
    	        System.out.println(handname);
    	        for (int j = 0; j < 13; j++)
    	        {
    	        	
    	        	Card output1 = hands[i][j];	
    	        	if (output1.getSuit() == 0){
    	        		String rank1 = output1.shortName().substring(0,1);
    	        		Ccard += rank1 + " ";}
    	        		else if(output1.getSuit() == 1){
    		        		String rank1 = output1.shortName().substring(0,1);
    		        		Dcard += rank1 + " ";} 
    	        		else if(output1.getSuit() == 2){
    		        		String rank1 = output1.shortName().substring(0,1);
    		        		Hcard += rank1 + " ";}
    	        		else if(output1.getSuit() == 3){
    		        		String rank1 = output1.shortName().substring(0,1);
    		        		Scard += rank1 + " ";}
    	        	
    	        	}
    	        System.out.println("S: " + Scard);
    			
    			System.out.println("H: " + Hcard);
    			
    			System.out.println("D: " + Dcard);
    			 
    			System.out.println("C: " + Ccard);	
    	        }
    	        System.out.print("\n\n");
    	    }
    	}
    here was the output I got:
    East
    S: 2 6 Q A
    H: 5 6 Q
    D: 6 8 K A
    C: 5 T
    South
    S: 2 6 Q A 3 4 8 9
    H: 5 6 Q 3 7 T
    D: 6 8 K A 5 7 T
    C: 5 T 3 6 9
    West
    S: 2 6 Q A 3 4 8 9 J K
    H: 5 6 Q 3 7 T 2 4 8 9 J A
    D: 6 8 K A 5 7 T 2 3 4
    C: 5 T 3 6 9 7 K
    North
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at Assignment.main(Assignment.java:44)


    So as you can see, I'm throwing an error, and I'm not even getting 13 cards per hand. Any advice?
    Last edited by steve0827; 12-10-2012 at 12:07 AM.

  6. #6
    steve0827 is offline Member
    Join Date
    Oct 2012
    Location
    CT
    Posts
    9
    Rep Power
    0

    Default Re: Deck output with Card class help

    Alright, well I figured out my problems and redid my code based off of your suggestion. Here is what I have now....it seems to be giving me exactly what I want.
    Java Code:
    public class Assignment7 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    
    
    	
    		String Scard = " ";
    		String Hcard = " ";
    		String Dcard = " ";
    		String Ccard = " ";
    		String handname = null;
    for (int j = 0; j<4; j++){
    if (j == 0)
    {handname = "East";}
    else if(j==1)
    {handname = "South";}
    else if(j==2)
    {handname = "West";}
    else if(j==3)
    {handname = "North";}
    
    System.out.println(handname);
    	
    	
    
    int handsize = 13;
    Card[] cards = new Card[handsize];
    for (int i = 0; i < handsize; i++)
    {
      cards[i] = Card.next();
      
     
    int suitoutput = cards[i].getSuit();
    
    if(suitoutput == 3){
    Scard += cards[i].shortName().substring(0,1) + " ";
    }
    else if(suitoutput == 2){
    	Hcard += cards[i].shortName().substring(0,1) + " ";
    }
    else if(suitoutput == 1){
    Dcard += cards[i].shortName().substring(0,1) + " ";
    }
    else if (suitoutput == 0){
    Ccard += cards[i].shortName().substring(0,1) + " ";
    }
    
    }
    
    
    
    
    if (Scard.length() < 2){
    	Scard = " -";
    }
    if (Hcard.length() < 2){
    	Hcard = " -";
    }
    if (Dcard.length() < 2){
    	Dcard = " -";
    }
    if (Ccard.length() < 2){
    	Ccard = " -";
    }
    
    		System.out.printf("S:%s%n", Scard);
    		
    		System.out.printf("H:%s%n", Hcard);
    		
    		System.out.printf("D:%s%n", Dcard);
    		if(j == 3){
    			System.out.printf("C:%s", Ccard);
    		}
    		else {	
    		System.out.printf("C:%s%n%n", Ccard);
    		}
    		Scard = " ";
    		Hcard = " ";
    		Dcard = " ";
    		Ccard = " ";
    		
    }
    
    		
    	}
    
    }
    If any of you see any potential errors(IE. reusing specific numbers more than 4 times, etc.), please let me know.
    Thank you for all of the help so far!

Similar Threads

  1. Want two deck card in my applet
    By ganesh.gothi@gmail.com in forum Java Applets
    Replies: 4
    Last Post: 04-16-2012, 01:21 PM
  2. Replies: 11
    Last Post: 11-25-2011, 04:56 AM
  3. Creating a card class for a Deck of cards
    By adjit in forum New To Java
    Replies: 13
    Last Post: 04-06-2011, 10:20 PM
  4. Card deck arrays?
    By linc186 in forum New To Java
    Replies: 2
    Last Post: 03-16-2011, 09:56 PM
  5. Replies: 2
    Last Post: 12-13-2010, 11:12 AM

Posting Permissions

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