• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using my Hand, Deck, and Card class together.  RSS feed

 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'm still rather new to programming, so here goes, I have created a Cards and Deck class which work great, but the Hand class is where I run into trouble. In the driver I have to ask the user, how many players there are going to be as well as how many cards in the hand. Here's what I have so far, I'll try to post only the necessary code to keep it short.

Here's my Deck Class:



For the dealAHand method, my instructor had us using returning a String before, but now that we've created a Hand class she made us change the String to Hand which has thrown me for a loop.

Here's my Hand class:



The problem I have is when asking the user how many players will be playing, do I create a new object for hand? like Hand player1 = new Hand (); ? How would I go about creating hands for multiple players based on user input?

Also, when I ask for how many cards are in a hand, and they input 15 cards a hand if there are to be 5 players? Since it can't that can't be done, where would I do the checking for that? I understood what I had to do when it was just the two classes (Card and Deck) but once the third class was introduced it just totally screwed my logic up.

I know this it's rather long, but a push in the right direction would be greatly appreciated.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first reaction is that you might want to consider a fourth class.

Just like a deck holds more cards, a round of play includes more players with holdings.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stijn Rensen wrote:My first reaction is that you might want to consider a fourth class.

Just like a deck holds more cards, a round of play includes more players with holdings.


The criteria had only asked for the 3 classes though along with the driver. What would the fourth class do exactly?
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oke my understanding of the english language might restrict me here. But i would consider the following:
Class 1 the deck. As described
Class 2 the card. As described
Class 3 a game (or a round of cards)
Class 4 a player.

Class game would determine if the round is possible (size of holding * players < 53)
Class game would direct the the giving of the cards

Class player would ensure the cards can be given to an object
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just take a step back. Think about your requirements, and which classes fulfill which responsibilities.

You need a class Card that represents playing cards.
You need a class Hand that holds some amount of cards.
You need a class Deck that deals Hands.
You need a driver class that gets some amount of Hands.

Nowhere is there any talk of strings, so just forget them.

What do you need to create a card? A rank and a suit. Pass them in the constructor.

What do you need to create a hand? A maximum number of cards it can hold. Pass it in the constructor. Here is the skeleton:
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:



These methods are where i'm running into problems. In the hand class how would I go about seeing if there's enough room and how would I make room?
It's also the same problem with the dealAHand method, specifically checking to see if there are enough cards, creating, and filling the hand.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put all the cards in an arraylist you can do this - choose a random number between 0 and its size-1 (51 to start with). Use that random number as an index into the arraylist. That will be the card you are dealing. Remove the element with that index from the arraylist from from the arraylist. That gives you a new upper bound for the next random number. The arraylist elements can be a Card object with suit and value fields.
 
john lampard
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:If you put all the cards in an arraylist you can do this - choose a random number between 0 and its size-1 (51 to start with). Use that random number as an index into the arraylist. That will be the card you are dealing. Remove the element with that index from the arraylist from from the arraylist. That gives you a new upper bound for the next random number. The arraylist elements can be a Card object with suit and value fields.


If I had it my way, I would definitely would have used an arraylist, but it wasn't up to me. Just got to play with the cards i've been dealt
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Har har!
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sort of figured it out it seems.

the dealAHand method in the Deck class


My driver:


The output:



How can i get it to print Player 1: cards....
Player 2: cards...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Shoe holds multiple Decks. Maybe if the conditions are right, you need a Shoe to hold however many Decks you need to play. But before you go and create a Shoe class, consider this: if you combine two or more Decks, all you really get is a bigger Deck. Everything you would do with a standard 52-card Deck, you can still do with the bigger, combined Deck. Take a hint from the List.addAll() method.

Edit: Sorry if that seems to have come out of left field. I was referring to the condition where you need more than 52 cards to play. That made me think of the card shoes that are used to hold multiple decks, like they do at a Blackjack table in a casino.

Notice how Stephan has named his class Card (singular) whereas your class is named Cards (plural). A class name is usually singular because each instance represents ONE object. There are exceptions to this but generally, class names are singular and in this case, they should be.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't get the story that this code is telling. Here's how I read it:


This is quite confusing to me and it doesn't tell a clear story.

Here's the story that I would expect the dealAHand() method to tell:


It's a pretty simple exercise to translate this to method parameters, loops, variables, calls to different methods of objects we have available, etc.

Now, which "story" makes more sense, yours or mine?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:Here's the story that I would expect...

Of course when I implemented that, I realized that there's no way for me to specify how many cards the Hand should have to be considered "full". So maybe I change the story up a little:

Here, instead of being given just a Deck, we start by getting a Deck and a Hand. We do the same thing as before. This story doesn't translate into very good code though for reasons I can explain later. Suffice it to say for now that I would again change the story up:

This translates to pretty decent code. Go ahead, try your hand at it
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john lampard wrote:

How can i get it to print Player 1: cards....
Player 2: cards...

Well, I would guess that your dealACard() has a System.out.println() statement in it that displays the Card suit and rank. Not a very good thing to do, especially since it's happening as part of the process of dealing a Hand. Again, I would start by laying out a story that I want to tell:

When translating this to code, you need to figure out the proper structure to hold however many Hand objects you have. Also, think about which object you want to attach the "display Cards in a Hand" sub-story and whether or not that story needs to be changed up a little bit as a result.

Note also that we separated the process of dealing N Hands from displaying the players and their cards. This is called "separation of concerns" and it's an important concept to learn if you're going to write good, maintainable code.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, parameters (variables in the signature of methods) and arguments (their values) are important! It's good to give them a name that makes clear what is supposed to happen.

Junilu already showed that the name n in dealAHand(int n) is very confusing. And what's supposed to happen when you pass a single number to a Card constructor? It doesn't make a lot of sense.

I have a challenge for you. Don't use a single println() statement, except in the main() method. It will become more clear why you need to think well about what goes in, and especially what gets returned from your methods. Here's a start:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!