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.
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???
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!!
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
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.
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.
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"
> //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 will always
be deck, but the card at deck could be anything (deck is the card on the top).
So, player3 deals:
player4 gets deck
player1 gets deck
player2 gets deck
player3 gets deck,
player4 gets deck
player1 gets deck
player2 gets deck
player3 gets deck, 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
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..
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)