• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deck of cards  RSS feed

 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks hope you can help me.

I'm trying to write a program that creates a deck of 52 playing cards ,shuffles, and then deals them.


I'm required to use a boolean array to indicate whether the card has been dealt or not and store each suit and rank implicitly.

" 0 – 12 correspond to Clubs ... 39 – 51 correspond to spades"

I currently wrote the method for making the deck itself using 2 enum type from another class that each contains constants 1 for suit and the other for rank.

public DeckOfCards()
{
initialize array

for ( PlayingCard.CardSuit suit : PlayingCard.CardSuit.values())
{
for (PlayingCard.CardRank rank : PlayingCard.CardRank.values())
{

add values to array ( make every value true)

}

This is where I really am thinking hard on what do do next.. do I have to create another array? I'm not allowed to create another instance variable so the one array i have initialized is the only one I can use I think.. I can't seem to get my head around how I'm going to shuffle from an array that only has true and false values.. I guess I have to create an alternate way to do that..

Can someone please steer me in the right direction???
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I designed my company's java programming interview around the game of Texas Hold-Em.

This tests our candidates ability to understand arrays, collections, Math.Random etc.

In your case, assuming the 52 cards are shuffled, I think you would pick from 1-52 (or 0-51),
tag it as boolean to say it was dealt.

If you want to be really clever, add a boolean attribute to a Card object.

Think of your deck as an array of Card objects.

WP
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created the constructor which created and set all the 51 indexes to true to denote that they are still in the deck.

I can't create an extra object cause I have to work with the methods provided for me to fill in...

Right now I'm trying to figure out exactly how to associate the array to the type of card.. can't really figure out how to do this and shuffle the deck without creating another array with the names in it... especially when i can't use any values outside of this shuffle method .thanks for the help!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you really need to create a Card object,
so it can retain all the info.

your array will be of Cards()
Card[] cards = new Card[52];

in a loop you
create the card
set the true/false
assign rank and suit

the simple way for rank and suit is to
use the loop counter:
counter % 13 = rank
counter / 4 = suit
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys. My instructor tells me it's not necessary to add another array object and suggested that it's borderline trivial() :P
So far I think I finished my "shuffle" and constructor methods, but I"m still working on my deal method..

This is what I have so far... the first if throws an exception in case the deck is empty and they all check if the deck index is false from my boolean array of 52.. if it's false that means it has been dealt. CardsInDeck is originally 52 so each time one is dealt there is a subtraction from the counter.

I'm trying to assign the values of a random card with the indexes 0-13... ect ect.. like suggested using the / and % .. however I think I get how this works.. but the deal method requires a PlayingCard to be returned (Or i think so because my instructor set up the method using "PlayingCard" and a return null...

Right now I'm not sure how I can assign my playing card suit variable a constant like 'C'... or how to derive it from the enum.. I'm not so familiar with enums by the way.. right now I had something like PlayingCard.CardSuit.valueOf(suit); = ... but I think I might be way off... any help is appreciated.. thanks again folks





==== this is the enum i'm trying to use..





 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == false or == true; they are poor style and prone to error, particularly if you write = by mistake, as you have done It’s if (!deck[number])...
There is something wrong with using parallel arrays. It is error-prone.
There is something wrong with using an Exception to signify an empty collection. You are better using a look to count the contents of the collection.
There is also something wrong with returning null.
What is wrong with i / 13 == 0? You get that 0 result from any integer in the range -12 to 12 inclusive. I can’t understand that bit.
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the i/13 == 0 bit is used to tell whether the random number refers ... 0 – 12 correspond to CLUBS, elements 13 – 25 correspond to DIAMONDS, elements 26 – 38 correspond to HEARTS, and elements 39 – 51 correspond to SPADES. and I'd be using the % operator to refer to ranks.

thanks
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I should tell you guys this is for my second programming class.. anyways the description of my deal method indicates that it should: "
//Simulate dealing a randomly selected card from the deck
//Dealing from an empty deck results in a RuntimeException"

thus the Exception in the method...
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find a far better way to create cards using enums if you search this forum.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> //Dealing from an empty deck results in a RuntimeException"

to me it looks like you're required to handle the exception, but it should never occur.

in your opening post:
"I'm required to use a boolean array to indicate whether the card has been dealt or not..."

'dealt' probably means 'alive on the table', so when the hand is over, dealt returns to false.
this gives you the opportunity of then shuffling all dealt-false cards when end-of-deck is
reached (or some earlier position in the deck, as in blackjack), so the game continues.

for dealing, you need to think of a real card game, say 5-card-draw poker, 4 players,
player3 is the dealer. you start with a deck of 52 cards (0-51), they are shuffled, but the
deck positions don't change (what card that position holds changes) i.e. deck[0] will always
be deck[0], but the card at deck[0] could be anything (deck[0] is the card on the top).

So, player3 deals:
player4 gets deck[0]
player1 gets deck[1]
player2 gets deck[2]
player3 gets deck[3],
player4 gets deck[4]
player1 gets deck[5]
player2 gets deck[6]
player3 gets deck[7], repeat 3 more times so each player has a 'hand'

if the above are outside of the requirements, it could be an interesting exercise for you
to write your own card game that simulates a game you know - just think object-oriented
Game, Player, Hand, Deck, Card etc
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys I finished my methods, but can someone please tell me what's wrong with my add card method??




 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) what is the point of having this line in addCard()
DeckOfCards dealer = new DeckOfCards();

2)

think about the first time you add a card
if(hand[0] == null) hand[0] = card;

now, what happens in the next loop when i == 1?
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help again..
Now that i run my program.. i seem to be getting the right output, but at the end I get an exception and the Royal Flush seems to take a bit to come up.. is it because of some bad code delaying it? I seem to get the error at around the time where my code tries to sort the cards in ascending rank order.. is my rank ordering code correct?? in my output the first few types seems to be out of order, but after three types or so it starts to sort them correctly.. I tried to mess around with code and think it over but I can't seem to crack it... help?? thanks again..





here is my output..

run:
5H
8C
QS
6C
KS - HIGH_CARD
2H
9C
4D
JH
JS - ONE_PAIR
2H
2S
5C
5S
TH - TWO_PAIR
3S
7C
TH
TD
TS - THREE_OF_A_KIND
7H
8C
9S
TC
JH - STRAIGHT
7H
3H
5H
JH
AH - FLUSH
2C
2H
2D
5S
5C - FULL_HOUSE
2C
JH
JS
JD
JC - FOUR_OF_A_KIND
3D
4D
5D
6D
7D - STRAIGHT_FLUSH
TD
JD
QD
KD
AD - ROYAL_FLUSH
Exception in thread "main" java.lang.NullPointerException
at PokerHand.addCard(PokerHand.java:107)
at PokerHandClient.aceLowStraight(PokerHandClient.java:48)
at PokerHandClient.main(PokerHandClient.java:23)
Java Result: 1

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
too much code to read, and it doesn't compile, so you'll just have to track it down by the error message.
i.e. whatever is on line 107 of PokerHand.java at some point is null (the line is probably not the same line 107 appearing in your post)
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]
duplicate post removed

reposted after "ahh you caught us with our pants down..." (or whatever the error message is)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!