This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Blackjack project / app  RSS feed

 
Paul D Pearson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to use what I've learned so far in terms of OOP in Java to build a fairly simple Blackjack game.  Please keep in mind this is v1 and I will expand into more classes after I have stood this one up. 

Right now I'm focused on my Card class and the setValue() and getValue() methods and how the interact with my Rank enum.  The Client class is merely for testing my current setup. 

I can instantiate a deck, shuffle, and deal a card.  My hand constructor works but right now I can't get the value of the hand once dealt.  Once I'm good with the Card, Rank and value methods I'd like to do the same with my Suit enum so when I deal it shows the symbol rather than HEARTS, SPADES, DIAMONDS, CLUBS.

https://github.com/pauldpearson/Blackjack











 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't get the value of the hand once dealt. 

Can you post the print out that shows what you are talking  about?  Add some comments that describe what is wrong and show what you want it to be.
 
Fred Kleinschmidt
Bartender
Posts: 567
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are a couple of thoughts about your Card class.

Why do you have variables rank and value? They are not independent. A single variable (rank) should be sufficient.

I would not have included a setValue() method. The rank (and therefore value) is fixed at construction time, and should be invariant after that.

you should priobably have a getRank() method.

The getValue() method should return rank.getCardValue(), since the value parameter is extraneous.
And you don't really need a getValue() method, since you could use something like
int value = card.getRank().getValue();


 
Junilu Lacar
Sheriff
Posts: 11428
173
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,  an Ace can take on two values in BlackJack: 1 or 11.  How are you going to represent that logic in your program?
 
Junilu Lacar
Sheriff
Posts: 11428
173
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A card's value is really more dependent on / dictated by the rules of the particular card game you are playing.  In BlackJack, 10, J, Q, K have the same value but in Poker, they don't. The "value" of a card can also be relative to other cards in a hand. As I already pointed out, an Ace in BlackJack can be either a 1 or 11, depending on what other cards you have.  In Poker, you have a whole bunch of rules for giving a value of a card based on the hand that you have.  An Ace can be a throwaway card, for example, or it could be what gives you a royal flush or a full house.

Remember, an object doesn't have to be a physical thing from the real world, it can also be an abstract idea / collection of related responsibilities such as assigning values to cards in a particular game, like BlackJack or Poker.
 
Paul D Pearson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my print out

Hand shows: [JACK of CLUBS, NINE of SPADES]
Hand value: 0
Hand shows: [KING of SPADES, THREE of HEARTS]
Hand value: 0
 
Paul D Pearson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I can't get the value of the hand once dealt. 

Can you post the print out that shows what you are talking  about?  Add some comments that describe what is wrong and show what you want it to be.


I just posted it right about this.  Still learning how to use the Saloon.
 
Paul D Pearson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Also,  an Ace can take on two values in BlackJack: 1 or 11.  How are you going to represent that logic in your program?


I meant to put that in my original post.  For v1 I'm designating an Ace to only represent 11.  I will eventually have AceClass extend Card.
 
Paul D Pearson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Here are a couple of thoughts about your Card class.

Why do you have variables rank and value? They are not independent. A single variable (rank) should be sufficient.

I would not have included a setValue() method. The rank (and therefore value) is fixed at construction time, and should be invariant after that.

You should priobably have a getRank() method.

The getValue() method should return rank.getCardValue(), since the value parameter is extraneous.
And you don't really need a getValue() method, since you could use something like
int value = card.getRank().getValue();




re: single variable (rank)  noted and I have removed
re: setValue()  I removed and that seems to make sense
re: getRank()  I have added that

re: getValue()  you give me the return and say don't really need it?  That causes some confusion, can you elaborate please?
re: int value = ...   Where would that go?

It seems I don't need that variable as...IT IS NOW WORKING!!!  



Hand shows: [THREE of HEARTS, KING of DIAMONDS]
Hand value: 13
Hand shows: [SEVEN of HEARTS, EIGHT of SPADES]
Hand value: 15
 
Junilu Lacar
Sheriff
Posts: 11428
173
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul D Pearson wrote:For v1 I'm designating an Ace to only represent 11.  I will eventually have AceClass extend Card.

That's not a good design. Object-orientation is about behaviors and responsibility. IMO, inheritance is not the proper mechanism to use to represent the logic/responsibility of determining the variable value of an Ace.
 
Junilu Lacar
Sheriff
Posts: 11428
173
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The General Responsibility Assignment Software Principles/Patterns (GRASP) will come into play here, in particular, Information Expert. Which class would have all the context/information needed to determine when an Ace should be valued as 1 or 11? That's where you should assign the responsibility.  In your proposed design where a AceCard extends Card, an AceCard object still would not (and should not) have the context it needs to determine whether it should have a 1 or 11 value. If you had a "Hand" class, however, specifically a "BlackJackHand", then it would have all the information it needs to determine what the value of an Ace should be, given that it knows what the other cards are in the hand.
 
Paul D Pearson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The General Responsibility Assignment Software Principles/Patterns (GRASP) will come into play here, in particular, Information Expert. Which class would have all the context/information needed to determine when an Ace should be valued as 1 or 11? That's where you should assign the responsibility.  In your proposed design where a AceCard extends Card, an AceCard object still would not (and should not) have the context it needs to determine whether it should have a 1 or 11 value. If you had a "Hand" class, however, specifically a "BlackJackHand", then it would have all the information it needs to determine what the value of an Ace should be, given that it knows what the other cards are in the hand.


That makes sense and I like that better than the AceCard class extending Card.  I do have a Hand class currently and will eventually modify it to hold the hard and soft value of an Ace. 
Thanks for the help and suggestions!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!