• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Making a simple blackjack game

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to make a very simple blackjack game to play against the computer.

I now made it, without using GUI in one class. It's quite big and now I want to try to make it possible to split. However, I would like to make methods like "drawcard". (Now I use a switch for this) and call the "drawcard" method in the main class.
But, if I place my switch (drawcard) in an method in an other class, I can only return 1 variable, and I want to return more (the name of the card, the points you get by it and if it's a ace or not.)

I just started with learning Java so sorry if I don't use the correct terms. I can copy the code I have now, but then I have to translate some names of variables and text . (it's now Dutch). Could I just copy the Javacode in the message?

Could anybody give me some advise if I should use more classes or not? Maybe I just have to learn more.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a proper OO design, you should only have to return one thing. You should be able to create a 'card' class that would hold the rank (A-K) and a suit. I would NOT have it return a value, since the value is not a property of the card, but of the rules of the game of blackjack.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Sanne!

I agree with Fred. It's probably easiest if you create a Card class.
 
Sanne Gloeric
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

I wrote the java code in English. Please don't laugh at my very-beginner simple code (or don't laugh to hard)
I'm trying to learn java on my own. So if anyone would tell me if this a logic structure/solution I would appreciate it very much. The code does work.

If I would make one method to generate a card, should I use an other method to determine the value and one to determine if it's a ace or not?
Or do I make it more difficult than I should by using multiple methods for a simple game?

This is my code:

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanne Gloeric wrote:Thank you.

I wrote the java code in English. Please don't laugh at my very-beginner simple code (or don't laugh to hard)
I'm trying to learn java on my own. So if anyone would tell me if this a logic structure/solution I would appreciate it very much. The code does work.

If I would make one method to generate a card, should I use an other method to determine the value and one to determine if it's a ace or not?
Or do I make it more difficult than I should by using multiple methods for a simple game?


Nobody here is going to laugh at you. If they do, let a moderator know, and we'll smack them down. I don't have time to look at your 300+ lines of code, but I'd probably approach it this way...

there is almost never anything wrong with creating more methods. The idea is that a method should do one simple thing, and do it well. And methods can of course call other methods. So you may have a method called 'getValueOfHand'. That may call (possibly many times) 'getValueOfCard' and 'getNumOfAces'. I would probably sum up all the values (and have all aces always worth 11). then, if my total was over 21, check to see if I have any aces. If no, the hand busts. If yes, I could loop and subtract 10 for each ace until my total is under 21.

this is just off the top of my head, without any real analysis, so there are probably some 'gotchas' I haven't thought of yet.
 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should probably look at using enum types to represent your deck of cards...

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic