This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

[Code Review] Simple Blackjack game

 
Greenhorn
Posts: 5
VI Editor PHP Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am new to CodeRanch.

Anyway, I am still a newbie at writing Java. And I wrote a simple Blackjack game,  please give honest comments on what you think about my code. So that I can improve on.

Here is the link of the github repo: https://github.com/carlomigueldy/simple-blackjack
 
Marshal
Posts: 66236
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Unless you are over 500 lines, please post the code here using the code button.
 
Carlo Miguel Dy
Greenhorn
Posts: 5
VI Editor PHP Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that XD. Here is the code

 
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Well, your code is nicely indented and formatted, so at least that's a good start.

There's way too much code in the main() method though. The sections you have commented could be easily extracted to their own methods to start making the code in main() more high-level, making the flow of the game easier to follow. The upside is that you have made good use of comments and whitespace to group sections of code together. That makes it easier to see which chunks of code can be extracted to their own method.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, let's talk about names. Try to use names from the problem domain. Avoid names that reveal or imply implementation details.

The name "hit()" is good, because it is a standard Blackjack term. The name "stick" not so much. The more common term is "stand."

The name "initCards()" is too techie. You're never going hear the Blackjack dealer at a real casino say "Ok, I'm going to init the cards." Again, try to use terms from the domain. A name that comes from Blackjack is "deal" -- you deal the first two cards. Then the player can choose to hit or stand.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try refactoring your code so that you're not referencing game so much. The code gets very verbose and noisy when you have game.playerStick and game.playerHit and game.whatever all over the place.
 
Carlo Miguel Dy
Greenhorn
Posts: 5
VI Editor PHP Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Try refactoring your code so that you're not referencing game so much. The code gets very verbose and noisy when you have game.playerStick and game.playerHit and game.whatever all over the place.



Hello sir, good evening! Haha. I was at school from class. And I tried to refactored my code, and here's it..



Is this better?
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prefer java.util.Random over Math.Random -- java.util.Random has many convenient methods that are more straightforward to use. In this case, java.util.Random.nextInt() would be easier to read than what you have written.

Also, assigning a value to a variable and then returning that variable in the next statement makes your code unnecessarily verbose. You can eliminate the temporary variable and return the value directly.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a few instances of duplication in your code. See if you can identify and eliminate them.

This is confusing:
Think about what that code says for a minute: While the dealer chooses to stand (that is, not take any more cards) then you're going to display a message saying that the dealer is taking another card? And then actually call hit() to give the dealer another card? Isn't that kind of like saying "I'm going to tell my dog to stay" and then saying "Come here, boy!"

Your code should tell a consistent and coherent story.
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kent Beck wrote:If you don't know how you're going to test your code, then you have no business writing it.


What's your plan for testing this code? Are you going to write automated tests for it? Or are you going to just manually test it? Which approach were you taught? Which approach do you think is better, automated tests or manual tests?
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way you can gauge the maintainability of your code is to introduce new requirements.

Here's a couple:

How easy/hard would it be to change the code so that you're also displaying what particular cards are being dealt?

For example, if I wanted the game to go something like this:


Welcome to BlackJack!

Your starting hand: 8♥ 7♦

Would you like to Hit or Stand (H, S)? H

Hit: K♠

Your hand: 8♥ 7♦ K♠

Bust. Sorry, you lose.


2. Have logic that automatically uses 1 or 11 for an Ace card, depending which value makes the hand better and avoids a bust.

3. What if you wanted to make a GUI instead of character interface? How easy would it be to display images of cards.

4. What if instead of just using a PRNG (Pseudo Random Number Generator), you'd actually simulate combining N number of standard 52-card decks, shuffling them all, and dealing from the combined deck? How hard/easy would it be to make that change?

These are all good exercises that will help you experience for yourself the kind of impediments early design decisions can create in terms of maintainability and enhancement.
 
Carlo Miguel Dy
Greenhorn
Posts: 5
VI Editor PHP Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Another way you can gauge the maintainability of your code is to introduce new requirements.

Here's a couple:

How easy/hard would it be to change the code so that you're also displaying what particular cards are being dealt?

For example, if I wanted the game to go something like this:


Welcome to BlackJack!

Your starting hand: 8♥ 7♦

Would you like to Hit or Stand (H, S)? H

Hit: K♠

Your hand: 8♥ 7♦ K♠

Bust. Sorry, you lose.


2. Have logic that automatically uses 1 or 11 for an Ace card, depending which value makes the hand better and avoids a bust.

3. What if you wanted to make a GUI instead of character interface? How easy would it be to display images of cards.

4. What if instead of just using a PRNG (Pseudo Random Number Generator), you'd actually simulate combining N number of standard 52-card decks, shuffling them all, and dealing from the combined deck? How hard/easy would it be to make that change?

These are all good exercises that will help you experience for yourself the kind of impediments early design decisions can create in terms of maintainability and enhancement.



Thanks for the suggestion @Junilu it sounds fun, I will do this and post my code here again to see if I'm doing it right
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good, but try it by starting with what you already have instead of starting over from scratch. Also, try to write automated tests as you go. How would you test randomness?
 
Carlo Miguel Dy
Greenhorn
Posts: 5
VI Editor PHP Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Good, but try it by starting with what you already have instead of starting over from scratch. Also, try to write automated tests as you go. How would you test randomness?



Hello sir good evening, sorry for the late reply. I got busy at writing code for our thesis/capstone project.

I don't know yet how I would test randomness though..
And for the GUI, do you have any recommendations on what libraries that I should use? is Java Swing good?
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!