![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
Textbased blackjack.
I'm writing a black jack game and I want it so that whenever a card is picked it is eliminated from the deck. I wrote a method to do such a thing, but it doesn't work. Any help?
import javax.swing.JOptionPane;
public class main {
public static String cardName(int num) {
String cName[] = {"Two of hearts", "Two of spades", "Two of diamonds", "Two of clubs",
"Three of hearts", "Three of spades", "Three of diamonds", "Three of clubs",
"Four of hearts", "Four of spades", "Four of diamonds", "Four of clubs",
"Five of hearts", "Five of spades", "Five of diamonds", "Five of clubs",
"Six of hearts", "Six of spades", "Six of diamonds", "Six of clubs",
"Seven of hearts", "Seven of spades", "Seven of diamonds", "Seven of clubs",
"Eight of hearts", "Eight of spades", "Eight of diamonds", "Eight of clubs",
"Nine of hearts", "Nine of spades", "Nine of diamonds", "Nine of clubs",
"Ten of hearts", "Ten of spades", "Ten of diamonds", "Ten of clubs",
"King of hearts", "King of spades", "King of diamonds", "King of clubs",
"Queen of hearts", "Queen of spades", "Queen of diamonds", "Queen of clubs",
"Jack of hearts", "Jack of spades", "Jack of diamonds", "Jack of clubs"};
return cName[num];
}
public static int randCard() {
int rand = (int)(Math.random()*53), temp = 0;
int cDeck[] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11};
boolean lCond = true;
while (lCond == true) {
if (cDeck[rand] != 0) {
temp = cDeck[rand];
cDeck[rand] = 0;
lCond = false;
} else {
rand = (int)(Math.random()*53);
lCond = true;
}
}
return rand;
}
public static void main(String args[]) {
System.out.println();
for (int x = 1; x < 30; x++) {
System.out.println(cardName(randCard()));
}
}
}
__________________
meh... |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
The cDeck variable is recreated every time you call randCard(). You could make it static, so that it keeps its value from one call to the next, but that does mean that you would only be able to run through the deck once in your program. You might have to change the code so you can initialise a deck and pass it in to randCard instead.
Also, your random calculation gives a number from 0 to 52, but your cDeck array only goes from 0 to 51 and your cName array only goes from 0 to 47. Similarly your cardname array is recreated every time you call cardName(0, which is not very efficient. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In other words, you might want to consider playing with a full deck. You also might want to consider the various ways of reusing certain values, instead of replicating them over and over. Inefficiencies are easily concealed by the availability of copious resources, but what if you had a deck with twelve jillion cards?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Expert Programmer
|
You could use an ArrayList (make sure it is a member variable of the class) to store objects representing cards (perhaps Integer objects). You could then use the remove(int index) method with a random index to both remove and return a random object.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Game.cardName(Game.java:16)
at Game.main(Game.java:37)while (lCond == true) while (lCond)
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
What have classes done to you?
![]() Quote:
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
How do I declare my array as static so that it doesn't recreate itself?
__________________
meh... |
|
|
|
|
|
#8 |
|
Expert Programmer
|
Declare it outside the method.
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
Alright so I initialized and assigned my array in my main method and I pass it into my randCard() method every time I call it. Now, how can I manipulate my array from inside my randCard() method?
__________________
meh... |
|
|
|
|
|
#10 |
|
Expert Programmer
|
You can alter the values stored in an array, but you cannot alter the number of elements in the array once it has been created. If you need to do this, use an ArrayList.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Updating my old blackjack program | King | C++ | 3 | May 15th, 2006 2:48 AM |
| [Python] BlackJack | UnKnown X | Show Off Your Open Source Projects | 9 | Feb 20th, 2006 7:01 AM |
| help with blackjack game | sunny22 | Java | 4 | Feb 12th, 2006 1:15 AM |
| Simple blackjack script problem. Need help. | jokr004 | C++ | 6 | Feb 10th, 2006 12:44 PM |