# Need ideas and help for a simple BlackJack Game

• 04-26-2009, 09:34 PM
Dannii
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!
• 04-26-2009, 10:01 PM
Dannii
Ok Have managed to get random numbers by doing
Code:

` cards = (int)(Math.random() * 12)+1;`
Think now I have to loop and ask for a new card? And calculate the total sum?
• 04-26-2009, 10:20 PM
Fubarable
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.
• 04-27-2009, 02:33 AM
Singing Boyo
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.
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.
• 04-27-2009, 02:39 AM
Fubarable
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.
• 04-27-2009, 10:53 AM
Dannii
Quote:

Originally Posted by Singing Boyo
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.
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!