d2jsp
Gaming and Trading Community
Gaming and Trading Community
Hourly Raffle
Ladder Slasher
Trade Finder
Photo Gallery
Forum Gold FAQ
Instant Messenger
Help and Rules
Live Streams
Account RecoveryResend Validation Email
Hello, GuestLog InRegister
d2jsp Forums > Programmer's Haven > Java, Visual Basic, VBScript > Creating A Deck Of Cards Using Arrays

Add ReplyNew TopicNew Poll
Page 1 of 1
Cadet133
#1 Mar 8 2011 09:04am
Group: Members
Posts: 4,890
Joined: Jul 27 2008
Gold: 27.00
I need to create a new class called Deck that should be able to make an Array containing 52 cards, 13 each of red (r), green (g), blue (b) and yellow (y). How do I go about doing this?
SilverMice
#2 Mar 8 2011 09:13am
Group: Members
Posts: 4,944
Joined: Apr 13 2006
Gold: 115.60
Sounds like a assignment...

got a language?

first attempt?

layout a plan?
Cadet133
#3 Mar 8 2011 09:21am
Group: Members
Posts: 4,890
Joined: Jul 27 2008
Gold: 27.00
Quote (SilverMice @ Mar 8 2011 09:13am)
Sounds like a assignment...

got  a language?

first attempt?

layout a plan?


Its in java and Well I got

Code
public class CardDeck
{
private int[] card = new int[52];

}


But I dont know whether I should make an array for each suit (G,B,Y,R)

Or can I somehow make the suits in that one array.

This post was edited by Cadet133 on Mar 8 2011 09:21am
SilverMice
#4 Mar 8 2011 09:26am
Group: Members
Posts: 4,944
Joined: Apr 13 2006
Gold: 115.60
Quote (Cadet133 @ Mar 8 2011 09:21am)
Its in java and Well I got

Codepublic class CardDeck
{
private int[] card = new int[52];

}

But I dont know whether I should make an array for each suit (G,B,Y,R)

Or can I somehow  make the suits in that one array.


How about a double dimension array?

Code
private int[][] = new int[4][13];


[0][0] to [0][12] would be say red.
[1][0] to [1][12] green.

so on so forth.

This post was edited by SilverMice on Mar 8 2011 09:34am
ASBands
#5 Mar 8 2011 02:17pm
Group: Members
Posts: 6,923
Joined: Sep 27 2003
Gold: 518.50
Code
public enum CardColor
{
   Red, Green, Blue, Yellow
}


Code
public class Card
{
   private final CardColor _color;
   private final int _number;

   public Card(CardColor color, int number)
   {
       _color = color;
       _number = number;
   }

   public CardColor getColor() { return _color; }
   public int getNumber() { return _number; }

   @override
   public String toString()
   {
       return "" + _color + " " + _number;
   }
}

public class Program
{
   public static void main()
   {
       final int max_number = 13;
       final Card[] cards = new Card[CardColor.values().length() * max_number];
       int i = 0;

       for (CardColor color : CardColor.values())
           for (int num = 1; num <= max_number; ++num)
               cards[i++] = new Card(color, num);

       for (Card c : cards)
           System.out.println(c);
   }
}
Ekenstenen
#6 Mar 9 2011 03:02am
Group: Members
Posts: 2,201
Joined: Nov 29 2007
Gold: 0.00
Enum is the key to success here.

public enum Rank { ONE, TWO, THREE; FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ACE, JACK, QUEEN, KING };
public enum Suit { HEART, SPADES, CLUB, DIAMOND }

Then just create a class Card that keeps a rank and a suit.
A deck is obviously a list of cards, and each card got a rank and suit. The rest should be pretty simple to construct.
llamaoo7
#7 Mar 9 2011 03:14am
Group: Members
Posts: 9,445
Joined: Mar 14 2005
Gold: 13.37
Quote (Ekenstenen @ 9 Mar 2011 04:02)
Enum is the key to success here.

public enum Rank { ONE, TWO, THREE; FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ACE, JACK, QUEEN, KING };
public enum Suit { HEART, SPADES, CLUB, DIAMOND }

Then just create a class Card that keeps a rank and a suit.
A deck is obviously a list of cards, and each card got a rank and suit. The rest should be pretty simple to construct.


I like how misread the original post and didn't bother reading the rest of the thread either.
Ekenstenen
#8 Mar 9 2011 08:39am
Group: Members
Posts: 2,201
Joined: Nov 29 2007
Gold: 0.00
Quote (llamaoo7 @ 9 Mar 2011 10:14)
I like how misread the original post and didn't bother reading the rest of the thread either.


I like how you cant understand how to use enum for creating a deck of whatever.
ASBands
#9 Mar 9 2011 09:45am
Group: Members
Posts: 6,923
Joined: Sep 27 2003
Gold: 518.50
Quote (Ekenstenen @ Mar 9 2011 04:02am)
Enum is the key to success here.

public enum Rank { ONE, TWO, THREE; FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ACE, JACK, QUEEN, KING };
public enum Suit { HEART, SPADES, CLUB, DIAMOND }

Then just create a class Card that keeps a rank and a suit.
A deck is obviously a list of cards, and each card got a rank and suit. The rest should be pretty simple to construct.

I like how that is the example for enums from the Java website, blindly copy and pasted here without fully reading the original forum post.
llamaoo7
#10 Mar 9 2011 02:01pm
Group: Members
Posts: 9,445
Joined: Mar 14 2005
Gold: 13.37
Quote (Ekenstenen @ 9 Mar 2011 09:39)
I like how you cant understand how to use enum for creating a deck of whatever.


You aren't good at things, are you?
Go Back To Java, Visual Basic, VBScript Topic List
Page 1 of 1