Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2007, 11:51 AM   #1
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
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...
TCStyle is offline   Reply With Quote
Old Mar 22nd, 2007, 8:20 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Mar 22nd, 2007, 9:02 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 22nd, 2007, 10:00 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 932
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Mar 23rd, 2007, 7:42 AM   #5
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
        at Game.cardName(Game.java:16)
        at Game.main(Game.java:37)
first letter of class name has to be Capialized.

  while (lCond == true)
  while (lCond)
lCond is a boolean you dont need to test if its value is equivolant to true time by means of ==. by its self in a condition statment its value is tested.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Mar 23rd, 2007, 12:46 PM   #6
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3 pegasus001 is on a distinguished road
What have classes done to you?

Quote:
Originally Posted by DaWei
In other words, you might want to consider playing with a full deck.
lol.
__________________
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.
pegasus001 is offline   Reply With Quote
Old Mar 23rd, 2007, 1:01 PM   #7
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
How do I declare my array as static so that it doesn't recreate itself?
__________________
meh...
TCStyle is offline   Reply With Quote
Old Mar 23rd, 2007, 1:03 PM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 932
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Declare it outside the method.
titaniumdecoy is offline   Reply With Quote
Old Mar 23rd, 2007, 2:24 PM   #9
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
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...
TCStyle is offline   Reply With Quote
Old Mar 23rd, 2007, 6:21 PM   #10
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 932
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC