• Post Reply Bookmark Topic Watch Topic
  • New Topic

Little game as my first program, I feel like there is way too much code  RSS feed

 
Pawel Gawel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone, below you can find code of my BlackJack game. As title says, I feel like there could be less lines of code but I cant really figure it out how to make it shorter. I was thinking about making an array of strings for card_name instead of if's or make single card as an object but I'm not really sure if its a good idea. Can you give me a feedback of what can I do better and what's actually good?
Cheers

 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! Cześć!

Pawel Gawel wrote:I was thinking about making an array of strings for card_name instead of if's or make single card as an object but I'm not really sure if its a good idea.

I think a map would do better. Or you could create an enum to represent card types.

One thing worth noting. Do not cram all your logic in main method. Remember, main is a pain.

Second, you should separate your game logic from your display logic.
This way, if you ever wanted to create a GUI for your game, you would not need to touch game logic code.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I shall go back to your post and see if I can get rid of some of the long lines. Don't use //comments for long comments. Use
/*
* comments for long comments
* instead
*/
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like the multiple if-elses. Can you put the names into an array? Or even an enum? Look up enums in the Java Language Specification and you may have a pleasant surprise.
 
Pawel Gawel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cześć! Thanks for your tips. At this moment I dont really know whats map or enum in Java is but I'll get into it as soon as I come back from work. About the main method.. is it a method which should only call other classes/methods without having any its own code?
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Gawel wrote:Cześć! Thanks for your tips. . . .
You're welcome
About the main method.. is it a method which should only call other classes/methods without having any its own code?
Yes. See our FAQ.
 
Pawel Gawel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again, I rewrite the code. It took me a couple of hours. At first I could not figure it out how to change it, to be honest I am really suprised the code works :P I tried to separate display and logic of the game but I really don't know if I did it right. I throw away everything from main method (as you said) but now it looks silly, not sure if it should look like this. For the first time I used an array in my program, before I didnt know when is a good opportunity to make a good use off of it - now (I think) I know. Thanks guys!

Here's re written code - any suggestions?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Gawel wrote:I tried to separate display and logic of the game but I really don't know if I did it right. I throw away everything from main method (as you said) but now it looks silly, not sure if it should look like this. For the first time I used an array in my program, before I didnt know when is a good opportunity to make a good use off of it - now (I think) I know.

I think you're worrying a bit too much about how to do this (arrays, enums, etc.), when what you should be doing is concentrating on WHAT the program has to do.

Don't worry; it's a very common mistake when you're starting out, because you're just dying to write code.

The problem is that a Blackjack program is NOT simple (at least not compared to "Hello World") - it has lots and lots of rules, and you need to make sure you understand them ALL before you write one line of Java.

Just off the top of my head, the very least you need to know is how to:

1. Shuffle a deck (or decks).
2. Deal it out correctly - Note: one important visual aspect to Blackjack is that dealer's LAST card, and all players cards, are dealt face-DOWN. Dealer's first card is dealt face-UP.
3. "Value" a card (especially aces).
4. Replicate the correct order of play..
5. Recognise a "BlackJack" - ie, an ace and a "ten card" as the first two cards dealt.
6. Recognise "splits" - ie, two or more cards of the same value dealt as a player's first "playable" cards. The most I've ever seen at the table for a single player is 5 (4-deck shoe).
7. Let a player "double down" - ie, "buy" exactly ONE face-down card.
8. Work out who won a particular deal (dealer normally wins all "pushes").

and possibly also:

9. Enforce playing rules for the dealer (eg, in Vegas, dealer MUST hit anything up to 16, and stand on any hand of 17 or over - in the UK, dealer is (unfortunately) allowed to hit a "soft 17").
10. Use chips, and keep track of player "money status", house profits (again, in Vegas, a "BlackJack" pays 3:2), and possibly table limits.
11. "Insurance" bets.
11. "Surrenders" (optional).

So, as you can see, there's a LOT to plan for - and you ain't gonna want to do it while you're coding.

My advice: Write down all rules of the game (or perhaps, all the rules YOU want to play to) in detail, before you write any more code.

HIH

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Gawel wrote:Hello again, I rewrite the code. It took me a couple of hours. At first I could not figure it out how to change it, to be honest I am really suprised the code works :P

Congratulations on getting it to work.

Pawel Gawel wrote:I tried to separate display and logic of the game but I really don't know if I did it right. I throw away everything from main method (as you said) but now it looks silly, not sure if it should look like this.

A single line in main is quite common. The thing that looks odd to me are all the methods are static.

Pawel Gawel wrote:For the first time I used an array in my program, before I didnt know when is a good opportunity to make a good use off of it - now (I think) I know. Thanks guys!

Whenever you find yourself repeating something that is naturally indexed you should think array. In your latest code, you have cardplayer1 and cardplayer2 so if you wanted to change your code to support 4 players you would have to create another load of variables and change the Display method (which should be called 'display' BTW) to support the new variables. This is a lot of work but there is an alternative, you could have an array called players and if you wanted to change from 2 players to 4 players all you would need to do is initialize the array to a different size. Provided all your uses of the array elements were in for loops iterating over the length of the array there would be no other code changes.
 
Pawel Gawel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
The problem is that a Blackjack program is NOT simple (at least not compared to "Hello World") - it has lots and lots of rules, and you need to make sure you understand them ALL before you write one line of Java.


Yeah I know it, but what I wanted to do is just write the basics of the Blackjack. At the moment I dont know how I could write something like double down, splits or bets. I dont see a reason to make additional decks (shuffle decks) because it would not change anything here (thats my opinion because its all random anyways).

My plan is to make Blackjack with GUI, but first things firsts - :P
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Gawel wrote:Yeah I know it, but what I wanted to do is just write the basics of the Blackjack.

Then you need to write out explicitly what "the basics" are. As far as I'm concerned, splitting and doubling down are part of the game, but if you don't want to deal with them then you have to say so - or say what rules you are playing to.

I dont see a reason to make additional decks (shuffle decks) because it would not change anything here (thats my opinion because its all random anyways).

Then I'm afraid you're at odds with Las Vegas; perhaps because they didn't fancy shuffling after every other deal.

And the fact is: if you can shuffle 1 deck, it shouldn't be too difficult to shuffle 4...or 6 (which I think is the norm).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!