• Post Reply Bookmark Topic Watch Topic
  • New Topic

Blackjack has me stumped  RSS feed

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

just need some advice on the best way to go about someting...the theory

i am writing a black jack game ....

i can place a wager
no more than the money i have
and no less than 1

the first hand plays
then i want the player to select if he wants to draw or stand
this is where im having the problem
the loops i am trying just dont seem to give me the effect i require

should i be using a
for
while
or do while loop to accomplish this the easiest and most efficient way

any advice appreciated

then i will code and pos back here if im having a problem

thanks
 
Carey Brown
Bartender
Posts: 2998
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:the loops i am trying just dont seem to give me the effect i require

What effect do you require?
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to have a loop that takes a new card total and adds it to the first two cards total
then ask the player if he wants another card
but then also says bust if the total is over 21

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
takes a new card total and adds it to the first two cards total
then ask the player if he wants another card
but then also says bust if the total is over 21

The second and third lines should be swapped -> check for over 21 before asking the player if he wants another card.
 
Carey Brown
Bartender
Posts: 2998
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:

Which can be refactored to

Or if you're afraid that someone might misread the method name:

By extracting the calculations to separate methods, you reduce the noise in the code, clarify the intent by using well-chosen names, and make it easier to change the rules/implementation later on, if necessary.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so should i be using methods now?
thought i could get through this with loops and if statements
should i have separate classes for different functions?
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:so should i be using methods now?
thought i could get through this with loops and if statements
should i have separate classes for different functions?

Not necessarily. What you do depends on where you are in your learning progression.

Here's how it typically goes:

1.  Procedural Java
a. All code jammed into main()
b. code in main() + static methods

2.  One object Java, still largely procedural
a. main creates object, then runs its "main" method
b. previous static methods turned into instance methods

3.  Object-oriented Java
a. main() as driver of primary flow
b. coordinates actions of different objects
c. objects work together to achieve a goal
d. each object has its own set of responsibilities and well-defined interactions with other objects

To progress from 1 to 3, you have to understand concepts like assignment of responsibility, encapsulation, data hiding, polymorphism. You also need to learn to organize your code so that it's highly cohesive and loosely coupled.  There's a lot to take in but since you haven't shown us any actual code so that we can see more or less where you are, it's kind of hard to tell you what to do that would be appropriate for your current learning level.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i have so far:



it is sort of working but i can see two problems

1) the bust loop (over 21) asks once more for a card and only busts the next time round

2) where is my bust going to go to? if i put bust outside the loop (in my main while playing loop it will print even if i dont draw a card

thanks for the hep...its been very challenging ...but fun

good night ll
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this situation it's best to post a full, working example.  I can see lots of problems but I don't know if that's because some code wasn't included.
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can see in the snippet of code you posted, I would guess that you still have all your code in main(), is that correct? The problem with doing that of course is that with more than 20 or 30 lines of code, the logic becomes very convoluted and the "story" that you're trying to tell gets drowned out in the noise of all the implementation details.


The above code is kind of like the answer given in the following conversation:

"What are you trying to do?"

"Well, I'm trying to initiate combustion of this small splinter of wood that has been impregnated with ammonium phosphate and bathed with paraffin wax, with one end coated with a dried out dollop of a compound consisting of red phosphorus, sulfur and potassium chlorate."

Compare that to this version of the same conversation:

"What are you trying to do?"

"I'm trying to light this match."

That's kind of like what this version of the code is doing:

Do you see how hiding all the noisy details actually clarifies the intent of the code and makes it easier to verify the correctness of the logic?

Caveat: This is just an example of how I might start to try to simplify the code and clarify the logic. I'm just using this example to point out that simplicity and clarity can go a long way in making code easier to understand and debug. Don't drown yourself in details. Try to figure out the essence of what you're trying to do and express it as simply and succinctly as you can. (I know, it's kind of ironic that I say that and yet have the longest replies in this thread)
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so

in essence what i should be doing is looking more towards oop programming
use functions and classes more...

i should restructure my code

thanks
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should definitely restructure your code but even the example of cleaner code that I gave isn't necessarily OOP yet. In fact, it's code that could still be in all static methods that access global static variables. It's still a step forward and a vast improvement from a program that has all the code jammed into main(). I'm glad you recognize the need to reorganize your code though.
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

While above code is an improvement, it still isn't necessarily OOP. It is, however, the result of refactoring for clarity and simplicity. Those methods might look something like this:

Notice that the code has greater cohesion: each static method contains code that achieves only one specific goal and every line of code in the method is related to that goal. Cohesive code tends to be simpler as well and easier to follow. Well chosen names that reflect the intent of the code helps to increase clarity. The resulting high-level code that calls all these lower-level private methods is more "composed", that is, it has a single, high level of abstraction. This demonstrates the power of three simple refactorings:

1. Rename for clarity of intent
2. Extract to eliminate duplication and/or hide details and/or move responsibility
3. Compose method to achieve a single level of abstraction

If you try to apply any or all of these techniques for every few lines of code (say 10 to 20) that you write, you will be able to keep code complexity under control and most bugs that can hide in convoluted logic at bay.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brilliant...i will do a complete rewrite this weekend

thanks for the help

are you good with graphics too? would be nice to take it to a graphical representation...once i have the base program done

thanks again,invaluable help!!!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to add graphics to your game you will need to choose between Swing (older, but still used a lot) and JavaFX.  Likewise, there are two different forums for each.  If you want a tutorial, you could start with the Oracle Help Center.  Look a few paragraphs down at Creating Graphical User Interfaces.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!