Adblock breaks this site

A Class that represents a Deck of cards

Discussion in 'Programming General' started by SuF, Dec 22, 2008.

  1. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    Now Swan, call of your damned armies. I am using object orientation!

    This is not done, nor are all the parts working... shuffle is though... that took awhile since i kept finding very small errors in coding... i am making a blackjack game... i decided to make a nice deck class first... o_O

    Now, somebody go make a 1v1 poker game. :D

    Code:
    import java.util.*;
    
    public class Deck
    {
    	String[] a = new String[52];
    	String[] suits = new String[]
    	{ "Hearts", "Clubs", "Spades", "Diamonds" };
    	String[] faces = new String[]
    	{ "Ace", "Jack", "Queen", "King" };
    	int topCard = -1;
    
    	public Deck()
    	{
    		int count = 0;
    
    		for (int c = 1; c < 5; c++)
    		{
    			for (int b = 0; b < 13; b++)
    			{
    				a[count] = b + 1 + "" + c;
    				count++;
    			}
    		}
    	}
    
    	public String shuffle()
    	{
    		int[] used = new int[52];
    		int random;
    		int count = 0;
    		boolean cont = true;
    		boolean first = true;
    		topCard = -1;
    
    		for (int c = 1; c < 5; c++)
    		{
    			for (int b = 1; b < 14; b++)
    			{
    				if (count > 0)
    				{
    					first = true;
    					cont = true;
    					random = random(0, 51);
    					while (cont)
    					{
    						cont = false;
    						if (!first)
    						{
    							random = random(0, 51);
    						}
    						else
    						{
    							first = false;
    						}
    						for (int a = 0; a <= count - 1; a++)
    						{
    							if (used[a] == random)
    							{
    								cont = true;
    								break;
    							}
    							else
    							{
    							}
    						}
    					}
    					a[random] = b + "" + c;
    					used[count] = random;
    					count++;
    				}
    				else if (count == 0)
    				{
    					random = random(0, 51);
    					a[random] = b + "" + c;
    					used[count] = random;
    					count++;
    				}
    			}
    		}
    		return toString();
    	}
    
    	public String toString()
    	{
    		return Arrays.toString(a);
    	}
    
    	public String getCard(int location)
    	{
    		return a[location - 1];
    	}
    
    	public String getCard(int location, boolean suit)
    	{
    		if (!suit)
    		{
    			return getCard(location);
    		}
    		else
    		{
    			String y = getCard(location);
    			if(y.length() == 3)
    				return y.charAt(0) + y.charAt(1) + " of " + suits[1];
    			else
    				return y.charAt(0) + " of " + suits[1];
    		}
    	}
    
    	public String getTopCard()
    	{
    		topCard++;
    		return a[topCard];
    	}
    
    	private static int random(int min, int max)
    	{
    		return (int) (Math.random() * (max - min + 1)) + min;
    	}
    }
    
     
  2. Zyloch

    Zyloch Member

    Joined:
    Apr 21, 2005
    Posts:
    63
    Referrals:
    0
    Sythe Gold:
    0
    A Class that represents a Deck of cards

  3. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    That is nice, I thought of using an List, but I likezor arrays better. My shuffle method is greatly fast... Maybe I should make a card class to use in the desk class. o_O
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    A Class that represents a Deck of cards

    You'd better believe my legions are assembled.

    Anyway, if I'm bored enough I may try to write my own version of this (I haven't used Java in a while) and we can compare them.
     
  5. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    Ok, if you wish. I need to keep working on mine, but I am really lazy...
     
  6. Robin tha hood

    Robin tha hood Forum Addict
    Banned

    Joined:
    Dec 17, 2008
    Posts:
    516
    Referrals:
    1
    Sythe Gold:
    0
    A Class that represents a Deck of cards

    You guys are making poker games? neto i would like to play when finished
     
  7. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    A Class that represents a Deck of cards

    Code:
    		for (int b = 0; b < 13; b++)
    			{
    				a[count] = b + 1 + "" + c;
    				count++;
    			}
    
    I think this could be simplified/shortened to..
    Code:
    		for (int b = 1; b < 14; b++)
    			{
    				a[count++] = b + "" + c;
    			}
    
    Looks pretty good overall. I wrote one in C++ a while ago but it was more of a pain since it isn't as flexible and easy to use strings as it is in Java.
     
  8. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    Yea, I know I kept changing it from b + 1 back to to b and changing stuff because the damned methods were not working... I still am going to go back through and streamline the code, making it nice when I am closer to being done... maybe after i write a card class...
     
  9. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    A Class that represents a Deck of cards

    You know, I've just made a wonderful class that uses queues quite effectively. I'll create a small blackjack game later and post it.
     
  10. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    I just made a class that represents the cards. Now time to change the deck class... and maybe finish it... lol..

    Code:
    public class Card
    {
    	public final int HEARTS = 1;
    	public final int CLUBS = 2;
    	public final int SPADES = 3;
    	public final int DIAMONDS = 4;
    	final String[] SUITS = new String[]
    	{ "Hearts", "Clubs", "Spades", "Diamonds" };
    	final String[] FACES = new String[]
    	{ "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    	String[] card = new String[3];
    
    	public Card(int value, int suit)
    	{
    		setSuit(suit);
    		setValue(value);
    	}
    
    	public void setSuit(int suit)
    	{
    		switch (suit)
    		{
    		case 1:
    			card[2] = "Hearts";
    			break;
    		case 2:
    			card[2] = "Clubs";
    			break;
    		case 3:
    			card[2] = "Spades";
    			break;
    		case 4:
    			card[2] = "Diamonds";
    			break;
    		default:
    			System.out.println("That is not a valid number!");
    		}
    	}
    
    	public void setValue(int value)
    	{
    		if (value < 1 || value > 13)
    		{
    			System.out.println("The number must be less than 14 and greater than 0!");
    		}
    		else
    		{
    			card[0] = "" + value;
    			card[1] = FACES[value - 1];
    		}
    	}
    
    	public String getValue()
    	{
    		return card[0];
    	}
    
    	public String getFace()
    	{
    		return card[1];
    	}
    
    	public String getSuit()
    	{
    		return card[2];
    	}
    }
    
    fucking testing reveled a few mistakes
     
  11. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    A Class that represents a Deck of cards

    nice, are you planning to create a complete game?

    and Swan, I'm waiting excited for yours :laugh:
     
  12. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    I hope to create a game...


    ok swan i do not need you help no more. D:
     
  13. Zyloch

    Zyloch Member

    Joined:
    Apr 21, 2005
    Posts:
    63
    Referrals:
    0
    Sythe Gold:
    0
    A Class that represents a Deck of cards

    SuF, have you thought about using enumerations for your suit and ranks?
     
  14. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    Won't I not be able to use a for loop to create the deck then? I have never really used enums, sooooo....
     
  15. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    A Class that represents a Deck of cards

    SuF, Here's a nice little tip for you.

    Why don't you just use number values to set the face and whatnot? You already have constants defining HEARTS, DIAMONDS, CLUBS and SPADES, so why not just do it like that?

    Well, I suppose it is your choice for your own class, ;)
     
  16. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    I do not even know how I have it now, but it works... lol.... i am getting close to finishing my blackjack, but aces r pissing me off... i am trying to think of an easy way to deal with them... lol
     
  17. Zyloch

    Zyloch Member

    Joined:
    Apr 21, 2005
    Posts:
    63
    Referrals:
    0
    Sythe Gold:
    0
    A Class that represents a Deck of cards

    Treat them as 11 unless the cards sum to over 21. When they do, go through the hand and change the value of the aces in the hand to 1 one by one until the sum is again under 21, if possible.
     
  18. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    A Class that represents a Deck of cards

    Yea, I thought of something yesterday, pick which ever one is closest to 21, but not over. I want to tell them they have 17 or 7... I still have to write my check win method... o_O...
     
  19. broady

    broady Newcomer

    Joined:
    Nov 15, 2008
    Posts:
    22
    Referrals:
    0
    Sythe Gold:
    0
    A Class that represents a Deck of cards

    Here's my implementation (excuse lack of package)

    Code:
    import java.util.*;
    
    public class Deck implements Iterable<Card> {
        private final Stack<Card> cards;
    
        public Deck() {
            cards = new Stack<Card>();
            for (Suit suit : Suit.SUITS) {
                cards.addAll(Suit.getCardsInSuit(suit));
            }
        }
    
        synchronized public void shuffle() {
            Collections.shuffle(cards);
        }
    
        synchronized public Card pop() {
            return cards.pop();
        }
    
        synchronized public Card peek() {
            return cards.peek();
        }
    
        public int size() {
            return cards.size();
        }
    
        public Iterator<Card> iterator() {
            return cards.iterator();
        }
    }
    
    Code:
    import java.util.List;
    import java.util.ArrayList;
    import static java.util.Arrays.asList;
    
    public enum Suit {
        DIAMOND("Diamond"),
        CLUB("Club"),
        HEART("Heart"),
        SPADE("Spade");
        
        private String description;
    
        Suit(String description) {
            this.description = description;
        }
    
        final public static List<Suit> SUITS = asList(Suit.DIAMOND, Suit.CLUB, Suit.HEART, Suit.SPADE);
    
        public static List<Card> getCardsInSuit(Suit suit) {
            List<Card> result = new ArrayList<Card>(13);
            for (int i = 2; i <= 14; i++) {
                result.add(new Card(suit, i));
            }
            return result;
        }
    
        @Override
        public String toString() {
            return this.description;
        }
    }
    
    Code:
    import java.util.Map;
    import java.util.HashMap;
    
    public class Card {
        private final Suit suit;
        private final Integer value; // 14 = Ace, 11 = J, 12 = Q, 13 = K
    
        private static final Map<Integer, String> DESCRIPTIONS = new HashMap<Integer, String>() {{
            put(11, "Jack");
            put(12, "Queen");
            put(13, "King");
            put(14, "Ace");
        }};
    
        public Card(final Suit suit, final Integer value) {
            if (value < 2 || value > 14)
                throw new IllegalArgumentException("Value must be between 2 and 14 inclusive, got " + value);
            
            this.suit = suit;
            this.value = value;
        }
    
        public Suit getSuit() {
            return suit;
        }
    
        public Integer getValue() {
            return value;
        }
    
        public String getValueAsString() {
            String result = DESCRIPTIONS.get(value);
            return result == null ? value.toString() : result;
        }
    
        @Override
        public String toString() {
            return getValueAsString() + " of " + suit + "s";
        }
    }
    
     
< I'm goign frickin' insane. This has always worked before... | need help with phpbb >


 
 
Adblock breaks this site