Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

am i going about this the right way?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

trying to organise my code a bit better in a full on black jack game



im planning to get the Wager function return the placed wage to the main program (is that the way to go?)
then i will write the functions for the actual player hands and dealers hand etc

any help advice appreciated
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66187
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all static, all the time. 

Also, you're not following standard naming conventions. Methods start with a lowercase letter.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some comments would help others understand the code. 
For example: Why is there a hardcoded 11 on lines 60 to 63?

Note: the logic assumes that there are a large number of decks in the shoe to deal from. For example: There is no limit on the number of Aces that can be dealt.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

static all the time? what does that mean?
should i rather be using a class/s and methods in the class...

sorry,just trying to learn the best way to build a program...its what i find a lot of tutorials/videos lack...proper construction of such a program

yes i could do comments...i realise this...will add some

hard coded 11? those lines call a random number between 1 and 11...dont they?

thanks for the input...any tutorials / advice / books etc you could recommend..

i been doing programmingbydoing...but am stuck on this black jack...been doing well up till now

am i better off looking at the completed project to see how things are done...or struggle along?
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
found this

http://math.hws.edu/eck/cs124/javanotes4/c5/ex-5-5-answer.html

will follow the instruction without looking at the solution
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hard coded 11? those lines call a random number between 1 and 11...dont they?
Why 1 to 11?  The decks I'm used to have 13 cards in a suit: A, 2-10, J,Q,K
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I'm doubted this program compiles, because of the line 49. Does it?

2. On line 33 you could change:
to:
that way is safer as you're not risking to reassign betPlaced to an unwanted value in case you manage to write:
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
hard coded 11? those lines call a random number between 1 and 11...dont they?
Why 1 to 11?  The decks I'm used to have 13 cards in a suit: A, 2-10, J,Q,K

This is a kludgey way to address the fact that aces can be scored as 1 or 11.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.  My point is that the programmer should document those "tricks".
For example by saying there are 11 different values for cards: 2-9 face value, Ace=1 or 11 and 10,J,Q,K = 10

And instead of hardcoding 11, define a constant:
final int NumberOfValues = 11;
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Yes.  My point is that the programmer should document those "tricks".
For example by saying there are 11 different values for cards: 2-9 face value, Ace=1 or 11 and 10,J,Q,K = 10

Agreed.

At some point the whole deck/shuffle/deal approach will need to be re-written, but we're currently focusing on Java 101 issues.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about enums for the cards and values with some code for the Ace?

A List of said enums to make a deck, a shoe object of these decks, randomised, perhaps?  Then you would actually draw cards by removing them from the ArrayList object...

Players as objects, that way you can create/delete them.

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
focusing on Java 101 issues.

My question is: should those issues include having comments in the program?  Using comments is a small step from designing a program before writing it.
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
focusing on Java 101 issues.

My question is: should those issues include having comments in the program?  Using comments is a small step from designing a program before writing it.

  • Comments
  • Static
  • Magic numbers
  • Naming conventions

  • all seem like appropriate topics.

    Creating a Deck class that can shuffle and deal out cards - not yet though this is currently needed in the program.
     
    Campbell Ritchie
    Marshal
    Posts: 55681
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Julian West wrote:What about enums for the cards and values with some code for the Ace? . . .
    What a good idea Read the Java™ Tutorials and the Java® Language Specification (=JLS) about enumerated types. That is one of the easier parts of the JLS to understand. You can be really object‑oriented by using 11 as a field of the ACE instance. If you can get 1 or 11 from the ACE, you might have to give it a getValue() method taking the outstanding maximum as a parameter.You are I presume familiar with the terms deuce and trey for 2 and 3?
     
    Knute Snortum
    Sheriff
    Posts: 4073
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    jon ninpoja wrote:
    static all the time? what does that mean?
    should i rather be using a class/s and methods in the class...


    You should get used to using an instance of your class (object) rather than marking everything static:




    sorry,just trying to learn the best way to build a program...its what i find a lot of tutorials/videos lack...proper construction of such a program

    This style guide will help you.


    thanks for the input...any tutorials / advice / books etc you could recommend..

    Have you looked at the Oracle tutorials yet?

    i been doing programmingbydoing...but am stuck on this black jack...been doing well up till now

    am i better off looking at the completed project to see how things are done...or struggle along?

    Programming is hard.  I'd say do some struggling, then if you get too frustrated, take a break and maybe read a Java book
     
    Fred Kleinschmidt
    Bartender
    Posts: 560
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your wager() method is declared as "void" yet you include the statement "return bet;".

    Never say
    It is too easy to mistype and use a single equal sign instead of two.  Just use

    What happens if the user types "quit" or "four" instead of an integer value?  You need to handle inputs that cannot be interpreted as an int.
     
    Junilu Lacar
    Sheriff
    Posts: 11138
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred Kleinschmidt wrote:Never say
    It is too easy to mistype and use a single equal sign instead of two.  Just use

    This is good advice but I think while the justification around possible typos is valid, it's less important than making the code more conversational and more natural to read. Read the original code out loud and you'll sound like a pedantic robot. People don't really talk like that. "All bets down is true!" is something that you'll hear in a casino never. "I am hungry is false!" is something you'll never hear at the end of a Thanksgiving dinner either. Why would you write code that's basically saying things like that?

    I read a great blog article recently about naming: http://arlobelshee.com/good-naming-is-a-process-not-a-single-step/ -- it's got a lot of great points.

    Consider this progression:

    I would lean toward either of the last two options since they are more readable, more conversational, and will probably fit into the larger narrative (story) of the code more naturally.
     
    Julian West
    Ranch Hand
    Posts: 91
    3
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:Why would you write code that's basically saying things like that?

    ...Because it's called, "CODE", not "narrative"! 
     
    Junilu Lacar
    Sheriff
    Posts: 11138
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Julian West wrote:
    Junilu Lacar wrote:Why would you write code that's basically saying things like that?

    ...Because it's called, "CODE", not "narrative"! 

    I hope that was meant to be a tongue-in-cheek emoji
     
    Junilu Lacar
    Sheriff
    Posts: 11138
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    jon ninpoja wrote:found this

    http://math.hws.edu/eck/cs124/javanotes4/c5/ex-5-5-answer.html

    will follow the instruction without looking at the solution


    Well, I read through it and the solution is quite appalling. Too much code in each of the two methods given. So much functionality jammed together like that makes the program difficult to read, understand, and modify. I'm sorry but that solution would have been given low marks by my instructors back in the 80s. Even when we were doing procedural programming, which is what that solution is, modularity and coherence were already emphasized and expected of us for programs of this size.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!