Results 1 to 9 of 9
  1. #1
    trinity is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Poker Class , removing cards already dealt

    Hi there , I am trying to to create a poker program to return a full deck , deal a hand and deal the 5 cards in the middle. The only problem I have is removing cards that have already been dealt from the deck..:confused:
    PS, using an IO(terminal class )
    THX


    public class Card {
    String face;
    String suit;

    public Card(String face,String suit) {
    this.face = face;
    this.suit = suit;
    }

    public String getFace() {
    return face;
    }

    public String getSuit() {
    return suit;
    }


    }





    import tcdIO.*;

    public class PokerProgram {
    public static void main(String[] arg) {
    Card[] card = new Card[52];
    String[] face = new String[13];
    String[] suit = new String[4];
    DeckOfCard[] deck = new DeckOfCard[52];
    // DeckOfCard deck;
    // Card seb = new Card("Seb", "seb");
    Terminal t = new Terminal("");
    int counter = 0;
    int choice = 0;

    face[0] = "ace";
    face[1] = "deuce";
    face[2] = "three";
    face[3] = "four";
    face[4] = "five";
    face[5] = "six";
    face[6] = "seven";
    face[7] = "eight";
    face[8] = "nine";
    face[9] = "ten";
    face[10] = "jack";
    face[11] = "queen";
    face[12] = "king";
    suit[0] = "heart";
    suit[1] = "spades";
    suit[2] = "clubs";
    suit[3] = "diamonds";

    while (counter < 52) {

    for (int i = 0; i < suit.length; i++) {

    for (int j = 0; j < face.length; j++) {
    card[counter] = new Card(face[j], suit[i]);
    counter++;

    }
    }

    }

    while (choice != 4) {
    t.println(" MENU ");
    t.println("---------------------------------------");
    t.println("(1)Show card deack");
    t.println("(2)Deal Hand");
    t.println("(3)Deal Floop");
    t.println("(4)Exit");
    t.println("---------------------------------------");
    choice = t.readInt("Please select:");

    switch (choice) {

    case 1:
    for (int i = 0; i < card.length; i++) {
    t.println(card[i].getFace() + " of " + card[i].getSuit());
    }
    break;

    case 2:
    int card1,
    card2;
    card1 = (int) (Math.random() * 52 + 1);
    card2 = (int) (Math.random() * 52 + 1);
    if (card1 != card2) {
    t.println("your cards are:");
    t.println(card[card1].getFace() + " of "
    + card[card1].getSuit());
    t.println(card[card2].getFace() + " of "
    + card[card2].getSuit());
    }
    break;

    case 3:

    for (int i = 1; i < 6; i++) {
    t.println(card[(int) (Math.random() * 52 + 1)].getFace()
    + " of "
    + card[(int) (Math.random() * 52 + 1)].getSuit());
    }

    break;
    case 4:
    t.println("Goodbye");
    break;

    }
    }

    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Arrays never change their length. So if you are modelling something like a stack of cards that are to be dealt (which can change its size) then it might be better to use something like a list. Implementations of List provide a method that removes elements, while the Collections class provides a method to shuffle a list.

    If you are stuck with using an array (because it's an assignment condition or whatever) then things become less straightforward. One approach would be to "mark" the array to show that a particular card had been dealt: for instance by making the element at that index null. Your use of random() will have to change since you would have to check that the card you have selected is not null and, if it is, call keep calling random() until you get a card that is still in the array.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    ps - Your main() method is already too big. Consider using some other methods: A method to initialise the deck, and methods to implement each of the switch cases would be a good start.

  4. #4
    trinity is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Hi pbrockway2 , Thanks for your answer .
    You were saying to implement a method for each switch case , how would one do that and in what Class should i consider implementing them?
    Thanks :)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The methods would be in PokerProgram.

    Java Code:
    private static Card[] card;
    
    public static void main(String[] arg) {
        card = new Card[52];
        // etc
        switch(choice) {
            case 1:
                displayCards();
                break;
            case 2:
                // etc
    }
    
    private static void displayCards() {
        for(int i = 0; i < card.length; i++) {
            t.println(card[i].getFace() + " of " + card[i].getSuit());
        }
    }

    You will probably need to consult a textbook to become familiar with the use of methods. The aim is for no single method to be more than a handful of lines long.

    (A better design would not involve use of the "static" keyword at all except for the main() method.)

    Other techniques to avoid a logical "tangle" include making sure you have (and document) a clear purpose for each method you write. And compile often so you can address the problems which the compiler alerts.

    ------------------

    Reading your code more closely I see you have another type, DeckOfCard, that you haven't described anywhere. It isn't clear what role it plays (you currently declare an array of such decks but don't use the array anywhere).

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    I have noticed a bug where you may get a ArrayIndexOutOfBoundsException.
    See "case 3" in the switch statement, it says "(int)Math.round(Math.random()*52+1)". Your "cards" array is only 52 indices long, 0-51. This will generate numbers from 1-53. Delete the "+1" and change 52 to 51.

  7. #7
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default

    I haven't read much of the thread, so maybe this has been said already. But what I did to create a new deck was create an array unshuffledDeck[] of just 0-51. Then an array shuffledDeck[] where I put each one from the unshuffledDeck[] into a random spot. Then I just had a method, getNextCard() that would get next card from the shuffledDeck[] and add 1 to the nextCard counter

    Looking back you could probably get rid of the unshuffledDeck[] array

  8. #8
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Why don't you keep your cards in an arrayList??
    Then you could do something like:
    arrayList.remove(playedCard);

  9. #9
    trinity is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Smile

    Thanks for all the answers guys!
    It seems I need to familiarize myself more with methods and how to implement them , cheers pbrockway2 for the method you posted it makes more sense to do it that way and its "cleaner".
    ra4king - Thanks for pointing that out , for some reason i thought you needed to add the +1 so that the Math.random would give me a number between 1-52 but i now see that 52 would make it Out of bound.
    Maximus-EVG- Hi Maximus , I am pretty new to programing and will dig into Array Lists , they seem very usefull!

Similar Threads

  1. Creating a card class for a Deck of cards
    By adjit in forum New To Java
    Replies: 13
    Last Post: 04-06-2011, 11:20 PM
  2. Poker Simulation (shuffling a deck of cards?)
    By StateofMind in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 09:59 PM
  3. Please help Me out with this poker Problem
    By ckrisasa in forum Networking
    Replies: 8
    Last Post: 12-03-2009, 02:59 PM
  4. Replies: 14
    Last Post: 10-19-2009, 11:57 AM
  5. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 06:47 PM

Posting Permissions

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