programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Completed BlackJack Code.

Robin Al-salehy
Greenhorn
Posts: 19
Hey guys here is a completed BlackJack Code.

I have gotten this code from my teacher. He said I should learn to understand this code before beginning to start programming the graphical of this game.

The only thing that bugs me with this code is that the cards has the value from 2 - 14 which is incorrect in blackjack. it is supposed to be 2-11, 2,3,4,5,6,7,8,9,10, J=10,Q=10,K=10, A =1 or 11.

I guess it has something to do with symbol but I don't really get It Actually. :S Anyone here who can explain how to change the value of these symbols? Thanks! =D

Code:

Campbell Ritchie
Marshal
Posts: 56581
172
Where does the value of each card (deuce=2=2, J=knekt=10, A=ess=1, A=ess=11) come from in that code?
Why do you have while (true) in line 8? Why don't you have a gameFinished variable, or even a GameState enum?Then you can say while (state == GameState.SPELA) ...
Can you work out code for if total is 21 or total is 11 when the last card draw is an A=ess?

Have you seen the Java® Language Specification about enums? It uses a pack of cards as its example.

Robin Al-salehy
Greenhorn
Posts: 19
I don't know why my teacher wrote this code. I think it might be that the code can be written in different ways or that he has taken out an old code.

Anyway what I think is that this code gives the value with "symbol". Or am i wrong?

Campbell Ritchie
Marshal
Posts: 56581
172
Using ordinal in that fashion will give ess=A=14, which we already know is incorrect. If you search this forum and “beginning Java” for ordinal in the last 30 days, you will find somebody else had a similar problem and found that using ordinal() like that is potentially incorrect.

Robin Al-salehy
Greenhorn
Posts: 19
Could you post me the link if possible? I couldn't find that topic you where referring to.

Campbell Ritchie
Marshal
Posts: 56581
172
Try here, and the remainder of the thread. See if you can find a copy of Effective Java™ 2/e by Joshua Bloch. The relevant pages appear to be 158 and 162.

Piet Souris
Master Rancher
Posts: 2044
75
hi Robin,

I do not have the book that Campbell refers to, so I do not know what

But in general: the value of a card, or of a hand, very much depends on
the specific card game. For Black Jack you value the cards in a different
way to, say, bridge or poker or (in Durtch) klaverjassen. So why not write
a dedicated class that has methods to value a card or a hand, specific to
some game?

Greetz,
Piet

Campbell Ritchie
Marshal
Posts: 56581
172
Excellent book.
There might be a copy in your library. If your employer doesn't have a copy, you really need one, even though it is 7 years old. Tell them to buy it. You might get a second‑hand copy for a good price.

Robin Al-salehy
Greenhorn
Posts: 19
• 1
I would love to continue this project and change the game so it would be perfect. Like giving the game several players and so on and so on. It's just that I have not written the code, and my assignment was not about the core code this time it was about the graphical use.

So when i have done the graphical I can go back and change the code so I can teach my self better.

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
Good luck Robin, keep us updated on your progress.