• Log in or sign up for an account here: https://www.sythe.org/login

A Class that represents a Deck of cards

SuF

Legend

Pirate
Retired Global Moderator
Joined
Jan 21, 2007
Sythe Gold
1,234
Messages
14,211
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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;
	}
}
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Ok, if you wish. I need to keep working on mine, but I am really lazy...
 
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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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.

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...
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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
 
nice, are you planning to create a complete game?

and Swan, I'm waiting excited for yours :laugh:
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
nice, are you planning to create a complete game?

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

I hope to create a game...


ok swan i do not need you help no more. D:
 
SuF, have you thought about using enumerations for your suit and ranks?
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
SuF, have you thought about using enumerations for your suit and ranks?

Won't I not be able to use a for loop to create the deck then? I have never really used enums, sooooo....
 
Member of the Month Winner
Sythe's 10th Anniversary
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, ;)
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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
 
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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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.

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...
 
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";
    }
}
 
Runescape Gamblingrunescape gambling
Back
Top