Results 1 to 6 of 6
  1. #1
    Dannii is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default Need ideas and help for a simple BlackJack Game

    Greetings to all,

    I am about to write a very simple black jack game with swing and JOption. The problem I am having is how to deal cards to the player and computer.

    My first idea was to use some sort of array for the cards...
    However since I have NO clue how to use arrays my second idea was to generate random numbers.

    First problem:
    How do I randomly generate 2 cards for the player?


    Cheers!

  2. #2
    Dannii is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Ok Have managed to get random numbers by doing
    Java Code:
     cards = (int)(Math.random() * 12)+1;
    Think now I have to loop and ask for a new card? And calculate the total sum?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First I would search this and the Sun fora as this assignment has been worked on here innumerable times. This should give you some ideas on how to start. If you don't understand what's been discussed before, then come on back with the link (you can't post direct links til you've posted 20 times here, but you still tell us where to look by using -dot- instead of . ) and your question.

    Good luck.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Do research this program, it is very common. However, if you are using random numbers, try using java.util.Random, which is the random primitive generator for Java. Just create an instance of it and use the code below to get an Integer.
    Java Code:
    Random rand = new Random();
    int cardnum = rand.nextInt(13);
    int suitnum = rand.nextInt(4);
    Note that the method nextInt(int) returns a number between 0(inclusive) and the specified int(exclusive) so an Ace would be 0, a 2 would be 1, etc.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The problem with using random numbers this way, is that it doesn't match what happens in real life. With this random approach, you can and will have duplicates of many cards, even if you're only supposed to be using one deck.

    Better would be to create an ArrayList of Card objects and shuffle them. Again, there are many examples of this in this and other related fora.

  6. #6
    Dannii is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Singing Boyo View Post
    Do research this program, it is very common. However, if you are using random numbers, try using java.util.Random, which is the random primitive generator for Java. Just create an instance of it and use the code below to get an Integer.
    Java Code:
    Random rand = new Random();
    int cardnum = rand.nextInt(13);
    int suitnum = rand.nextInt(4);
    Note that the method nextInt(int) returns a number between 0(inclusive) and the specified int(exclusive) so an Ace would be 0, a 2 would be 1, etc.
    A very big thank YOU!
    As I mentioned in my post the game is a very simple one. Will not use decks and "real life" scenario. The posts made on this forum have "advanced" black jack games which I am not interested in.
    Just random numbers and then compare numbers with each other and pick a winner...simple as that!
    Once again a big thank YOU!

Similar Threads

  1. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 09:46 AM
  2. out of ideas
    By v1nsai in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 01:35 AM
  3. Simple Text race game
    By Inf1nity in forum New To Java
    Replies: 2
    Last Post: 03-16-2009, 03:46 PM
  4. Research Ideas
    By hawaiifiver in forum Forum Lobby
    Replies: 2
    Last Post: 02-03-2009, 04:43 AM
  5. BlackJack help please,, adding hand values
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •