# Arrays-Poker Simulation Methods help/hints

• 04-10-2013, 08:00 AM
NoobieCode
Arrays-Poker Simulation Methods help/hints
Hi I am writing a poker code that mainly implements the use of Arrays.
I am kinda confused on these certain methods I have to make.
Please help me correct something If I am doing something wrong.
Thank you.

---------------------------------------------
Constructor Summary

Card(int value, int suit)
Standard constructor.

Method Summary

int getSuit()
"Getter" for suit of Card.
int getValue()
"Getter" for value of Card.
java.lang.String toString()
Returns the name of the card as a String.

Constructor Detail
Card

public Card(int value, int suit)
Standard constructor.
Parameters:
value - 1 through 13; 1 represents Ace, 2 through 10 for numerical cards, 11 is Jack, 12 is Queen, 13 is King
suit - 0 through 3; represents Spades, Hearts, Clubs, or Diamonds

Method Detail

getValue

public int getValue()
"Getter" for value of Card.

Returns:
value of card (1-13; 1 for Ace, 2-10 for numerical cards, 11 for Jack, 12 for Queen, 13 for King)
getSuit

public int getSuit()
"Getter" for suit of Card.

Returns:
suit of card (0-3; 0 for Spades, 1 for Hearts, 2 for Clubs, 3 for Diamonds)

----------------------------------------------------

Code:

``` /**[B]public Deck()[/B] -- This constructor initializes the Deck with 52 card objects, representing the 52 cards that are in a standard deck.  The cards must be ordered as in the diagram below: **/ public class Deck {         private Card[] cards;         public Deck() {                 this.cards = new Card[52]; //index [0,51]                 int index = 0;                 for (int suit = 0; suit <= 3; suit++){                         for (int value = 1; value <= 13; value++){                                 this.cards[index] = new Card(value, suit);                                 index++;                   }         } } /** Important:  The FIRST card in your array should be the Ace of Spades, and the LAST card should be the King of Diamonds.  Note that in the picture above, the Ace of Spades is at the TOP of the deck, which is to the left. **/   public Deck(Deck other) /** -- Standard copy constructor.  Note that it is okay to make a shallow copy of the array of cards.  (The Card class is immutable, so aliasing Cards is not a problem.) **/ this.cards = other.cards;  } public Card getCardAt(int position) /**-- Returns the card that is at the specified position in the array.  (Uses the usual 0-based indexing.)**/ int position = this.cards.getValue;  //Don't understand how this getter works. return; }   public int getNumCards() /** -- Returns the size of the array of Cards.  (It starts off equal to 52, but becomes smaller as cards are dealt from the deck.)**/ this.cards = new Cards [this.cards.length-i]  //Confused about how to do this return;  }```
• 04-10-2013, 10:48 AM
Ronin
Re: Arrays-Poker Simulation Methods help/hints
Hi NoobieCode, what is the confusion?

The code above just shows the Deck class. Have you started the Card class? If so could you post your attempt so we can assist.

Regards.
• 04-10-2013, 11:10 AM
NoobieCode
Re: Arrays-Poker Simulation Methods help/hints
Hi.

Im confused, in terms of syntax, on how to write the copy method, getNumCards, and getCartAt methods.
• 04-10-2013, 11:14 AM
NoobieCode
Re: Arrays-Poker Simulation Methods help/hints
I only need to implement the Deck Class...It consists of these methods which I am struggling on:

public Deck() -- This constructor initializes the Deck with 52 card objects, representing the 52 cards that are in a standard deck. The cards must be ordered as in the diagram below:

public Deck(Deck other) -- Standard copy constructor.

public Card getCardAt(int position) -- Returns the card that is at the specified position in the array. (Uses the usual 0-based indexing.)

public int getNumCards() -- Returns the size of the array of Cards. (It starts off equal to 52, but becomes smaller as cards are dealt from the deck.)
• 04-10-2013, 11:53 AM
Ronin
Re: Arrays-Poker Simulation Methods help/hints
• The diagram did not attach so I can't help you with this just yet.
• Is this a deep or shallow copy? I'm assuming deep in which case I would expect a copy construct within the card class as well.
• getCardAt() just returns the instance of Card at the required position within the array.
• The implementation depends on the type of array you are using, whether this is fixed or a type of list. As you are using a fixed size array then there needs to be a way of either removing a card from the pack as it is dealt or to record the number of cards which have been dealt.

Regards.
• 04-10-2013, 12:04 PM
NoobieCode
Re: Arrays-Poker Simulation Methods help/hints
shallow copy

i think i got it. thanks...

any ideas on how I would go about shuffling, dealing, and cutting the deck?

Im bad with algorithms and Im just beginning.
• 04-10-2013, 12:26 PM
Ronin
Re: Arrays-Poker Simulation Methods help/hints
A lot of this is down to personal interpretation, how would you do this with a real set of cards? For example each person would have their own way of shuffling, dealing and cutting the deck.
• One solution to shuffle the pack would be to randomly switch each card in the pack. You could also switch one random card with another a random number of times.
• To deal a card you would remove a card from start or end of the card array and assign it to a player. How you want to remove the card from the array can be done by nulling the position or reducing the size of the array.
• As for cutting the deck, would you like to split the pack or use this function to obtain a random card?

Regards.