• Post Reply Bookmark Topic Watch Topic
  • New Topic

Review and Upgrade of BlackJack  RSS feed

 
Grzegorz Skawinski
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

We've just finished our project of coding a blackjack game (we're using programmingbydoing.com website).
Can someone review our code and tell us if there's any other ways of writing that up (easier).
We also want to implement the following three into the game :

1) Use realistic card values, with suits and faces from ace to king.
2) Incorporate wagering.
3) Display some sort of graphical cards.

Can someone give us a hint on how to start implementing those, because we're wondering if we might need to use different classes, functions etc.

Our code is as follows :

 
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 was just saying this in another thread today: Good names are essential to writing good programs. Choose names that reveal your intent.

The name gene for the random generator is not a very good one. First, it's an abbreviation that just looks funny the first time you read it. I have a friend named Eugene who goes by the name Gene and I thought maybe you had one, too, or maybe one of you is named that and likes to be the dealer when you play cards. Names that are commonly used to refer to an instance of Random include rnd, rand, and random.  Any of these three are better than gene.

If you think that this comment about names is nitpicky, read what some of the best programmers in the world say are some of the hardest things in programming-it may sound like a joke and in a way, it's meant to be humorous but it's also deadly serious. Choosing good names is one of the hardest things to do in programming.

Look at other variable names in your program and ask yourselves, do these names clearly tell the reader what the value is for or what the method does?

One thing that is a nitpick: "LOOSE" is the opposite of TIGHT. The opposite of WIN is "LOSE"
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good start. The first thing I see is that the main method is getting long. Think about what you could extract into a method. You might need to make the variables static variables rather than local variables to facilitate this.

Basically before you add more functionality, you'll need to have the main method be shorter. For example:




 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:you'll need to have the main method be shorter. For example:
...

What Jeanne showed was a step in the right direction. It doesn't go all the way though. We have a Wiki page that explains why Main is a Pain and gives you more examples of what you can do to make it as short as possible. The shortest main() method you could write for this program would be something like this:

The play() method would be where you'd write the outline of the flow of the BlackJack game that Jeanne suggested.
 
Grzegorz Skawinski
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

We are learning programming from the website I provided above and we havent learned about classes and objects yet. Would you know any good tutorials about learning the objects. Junilu I dont know if you're a mind reader but our supervisor is called Gene and he's a gambler too
 
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
You can start with the Java Tutorial on Classes and Objects: https://docs.oracle.com/javase/tutorial/java/javaOO/

What you'll learn there are the mechanics of objects and classes: how to define fields, getters, setters, methods. That's all about form.  Once you understand the form that classes and objects take, you'll need to understand more about their function. That's a little harder. 

(Shameless self-promotion follows) -- some of the things you should know about the function of classes and objects are explained in these posts:

https://coderanch.com/t/676278/java/Test-Answer-Practice#3168712
https://coderanch.com/forums/posts/preList/676192/3168677#3168677

I just wrote those today but if you read them after you go through the Java Tutorial on Classes and Objects, the concepts I explain in those posts might give you some deeper insight into how to use object-orientation in Java programs.
 
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
Here are some more things to think about after you get a basic understanding of classes and objects.

I find it's more useful to focus on outcomes, behaviors, and relationships between objects first. Many inexperienced programmers, however, tend to focus on object fields and attributes first, probably because those are the most obvious to them and they're often in a rush to write code and get something done. I think that's a big mistake.

Behaviors and relationships of objects may not be as obvious and they're more difficult to wrap your head around but that's what object-orientation is really about: behaviors and interactions between different objects. So, resist the urge to write classes and define fields/attributes up front. Wait until the behaviors and responsibilities you assign to the objects hint or even demand the presence of some kind of information that will support those behaviors and responsibilities.  The examples in those two posts I cited show some of this in action.

In the case of BlackJack, I can see that your minds have already been pulled into the trap of thinking about data more than behavior. All these values and calculations you're doing are things that should be hidden away in a object. When you do that, all you have to do is become a client of the objects, telling them to do things and report back the results. 

Here's what I mean by that.

This is not object-oriented code. It's procedural code because it pulls information and behavior (the calculation) away from the object and puts it out in the open for everyone to see.  In fact, there isn't even any proper BlackJack-related object involved in this at all!

Compare that code with this:

Which version tells a clearer story and actually looks like a game of BlackJack?  This is the power of object-oriented programming. If you do it right, it's as if your program comes alive and tells a story of what's happening.
 
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
By the way, I'd probably come back to that code and change this:

to this:

The reason is that I'd be duplicating knowledge in two places: on line 10 with the condition and inside the hitToMinimumOrBust() method, where I was thinking of a while loop with almost the same kind of condition. By changing the call on line 11 to just playerHand.hit(deck), I eliminate the duplicate condition check inside that method and keep the logic one place, there on line 10.  Duplication of knowledge is a violation of the DRY principle or Don't Repeat Yourself.
 
Grzegorz Skawinski
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all your advises.
We're definitely start learning objects and re-write the code using them.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!