# Thread: Need ideas and help for a simple BlackJack Game

1. Member
Join Date
Apr 2009
Posts
4
Rep Power
0

## 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. Member
Join Date
Apr 2009
Posts
4
Rep Power
0
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. 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. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
6
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.

5. 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. Member
Join Date
Apr 2009
Posts
4
Rep Power
0
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.
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!

#### Posting Permissions

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