Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with Arrays (Java card game)

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Help with Arrays (Java card game)

    In my game below, I have to make sure I don't select the same card again. Just like in a Deck. As you can see below, I chose 'A of Hearts' 2 times. How can I fix this? I'm using two arrays, one for the Card and one for the Suits. The first time, 3 cards are randomly chosen, then, if the player want, one by one.

    Welcome to Thirty3
     
    Would you like to play (Y/Q)? y
     
    Your cards: 2 of Diamonds, A of Hearts, A of Hearts, (Total 24)
    Would you like another card (Y/N)? n
     
    The dealer drew: Queen of Diamonds, Jack of Diamonds, Queen of Diamonds, (Total 30)
     
    You lose! - Current Total - Wins - 0 Ties - 0 Loses - 1
     
    Would you like to play again (Y/Q)? n

    package test;
     
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.Random;
     
    public class Test {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            Random rand = new Random();
     
            String[] cards = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
            String[] suits = {" of Hearts", " of Diamonds", " of Spades", " of Clubs"};
     
            ArrayList checkCards = new ArrayList();
            String currentCard, getCard;
     
            int shuffleCards, shuffleSuits, playerPoints, dealerPoints, playerTotal, dealerTotal, wins, loses, pushes;
     
            String play, anotherCard, showCard, playAgain, score;
     
            StringBuilder PSB = new StringBuilder();
            StringBuilder selectedCard = new StringBuilder();
     
            showCard = "";
            playAgain = "y";
            score = "";
     
            playerTotal = 0;
            playerPoints = 0;
            dealerPoints = 0;
            dealerTotal = 0;
     
            wins = loses = pushes = 0;
     
            System.out.println("Welcome to Thirty3\n");
     
            System.out.print("Would you like to play (Y/Q)? ");
            play = scan.nextLine();
     
            if(play.equalsIgnoreCase("y"))
            {
                while(playAgain.equalsIgnoreCase("y"))
    {
                System.out.println();
     
                System.out.print("Your cards: ");
                for(int a=1; a<=3; a++)
                {
    //                shuffleCards = rand.nextInt(cards.length-1) + 0;
    //                shuffleSuits = rand.nextInt(suits.length-1) + 0;
     
                    shuffleCards = 0;
                    shuffleSuits = 0;
     
                    selectedCard.append(cards[shuffleCards].concat(suits[shuffleSuits]));
    //                selectedCard = cards[shuffleCards].concat(suits[shuffleSuits]);
                        currentCard = cards[shuffleCards].concat(suits[shuffleSuits]);
    //                System.out.println("current card" + currentCard);
                    checkCards.add(currentCard);
    //                System.out.println("currentCard " + checkCards);
                    for(int x=0; x<checkCards.toArray().length; x++)
                    {
    //                    getCard = (String) checkCards.get(x);
     
                        if(currentCard.matches(selectedCard.toString()))
                        {
                            shuffleCards = rand.nextInt(cards.length-1) + 0;
                            shuffleSuits = rand.nextInt(suits.length-1) + 0;
     
    //                        System.out.println("That card already selected!");
     
                        }
     
    //                    System.out.println("getCard["+x+"] = " + (String) checkCards.get(x));
                    }
     
                    System.out.print(cards[shuffleCards] + suits[shuffleSuits] + ", ");                         
     
                    if(shuffleCards == 1)
                    {
                        playerPoints = 2;
                    }
     
                    if(shuffleCards == 2)
                    {
                        playerPoints = 3;
                    }
     
                    if(shuffleCards == 3)
                    {
                        playerPoints = 4;
                    }
     
                    if(shuffleCards == 4)
                    {
                        playerPoints = 5;
                    }
     
                    if(shuffleCards == 5)
                    {
                        playerPoints = 6;
                    }
     
                    if(shuffleCards == 6)
                    {
                        playerPoints = 7;
                    }
     
                    if(shuffleCards == 7)
                    {
                        playerPoints = 8;
                    }
     
                    if(shuffleCards == 8)
                    {
                        playerPoints = 9;
                    }
     
                    if(shuffleCards == 9 || shuffleCards == 10 || shuffleCards == 11 || shuffleCards == 12)
                    {
                        playerPoints = 10;
                    }
     
                    if((shuffleCards == 10 && shuffleSuits == 2 )|| (shuffleCards == 10 && shuffleSuits == 0) || (shuffleCards == 12 && shuffleSuits == 1))
                    {
                        playerPoints = 13;
                    }
     
                    if(shuffleCards == 2 && shuffleSuits == 3)
                    {
                        playerPoints = 0;
                    }
     
                    if(shuffleCards == 0 && (playerTotal <= 22))
                    {
                        playerPoints = 11;
                    }
     
                    if(shuffleCards == 0 && (playerTotal > 22))
                    {
                        playerPoints = 1;
                    }   
     
                    playerTotal = playerPoints + playerTotal;
     
                    showCard = showCard.concat(cards[shuffleCards]).concat(suits[shuffleSuits].concat(", "));
                }
     
                System.out.print("(Total " + playerTotal + ")");
     
                System.out.println();
     
                System.out.print("Would you like another card (Y/N)? ");
                anotherCard = scan.nextLine();
     
                while(anotherCard.equalsIgnoreCase("y"))
                {
                    System.out.print("Your cards: " + showCard);
     
                    shuffleCards = rand.nextInt(cards.length-1) + 0;
                    shuffleSuits = rand.nextInt(suits.length-1) + 0;
     
                    PSB.append(cards[shuffleCards].concat(suits[shuffleSuits]).concat(", ")); // dashes done here
                    System.out.print(PSB.toString());
     
                    if(shuffleCards == 1)
                    {
                        playerPoints = 2;
                    }
     
                    if(shuffleCards == 2)
                    {
                        playerPoints = 3;
                    }
     
                    if(shuffleCards == 3)
                    {
                        playerPoints = 4;
                    }
     
                    if(shuffleCards == 4)
                    {
                        playerPoints = 5;
                    }
     
                    if(shuffleCards == 5)
                    {
                        playerPoints = 6;
                    }
     
                    if(shuffleCards == 6)
                    {
                        playerPoints = 7;
                    }
     
                    if(shuffleCards == 7)
                    {
                        playerPoints = 8;
                    }
     
                    if(shuffleCards == 8)
                    {
                        playerPoints = 9;
                    }
     
                    if(shuffleCards == 9 || shuffleCards == 10 || shuffleCards == 11 || shuffleCards == 12)
                    {
                        playerPoints = 10;
                    }
     
                    if(shuffleCards == 10 && shuffleSuits == 2 || shuffleCards == 10 && shuffleSuits == 0 || shuffleCards == 12 && shuffleSuits == 1)
                    {
                        playerPoints = 13;
                    }
     
                    if(shuffleCards == 2 && shuffleSuits == 3)
                    {
                        playerPoints = 0;
                    }
     
                    if(shuffleCards == 0 && (playerTotal <= 22))
                    {
                        playerPoints = 11;
                    }
     
                    if(shuffleCards == 0 && (playerTotal > 22))
                    {
                        playerPoints = 1;
                    }   
     
                    playerTotal = playerPoints + playerTotal;
     
                    System.out.print("(Total " + playerTotal + ")");
     
                    System.out.println();
     
                    System.out.print("Would you like another card (Y/N)? ");
                    anotherCard = scan.nextLine();
                }            
     
                System.out.println();
     
                System.out.print("The dealer drew: ");
     
                while(dealerTotal <= 26)
                {
                    shuffleCards = rand.nextInt(cards.length-1) + 0;
                    shuffleSuits = rand.nextInt(suits.length-1) + 0;
     
                    if(shuffleCards == 1)
                    {
                        dealerPoints = 2;
                    }
     
                    if(shuffleCards == 2)
                    {
                        dealerPoints = 3;
                    }
     
                    if(shuffleCards == 3)
                    {
                        dealerPoints = 4;
                    }
     
                    if(shuffleCards == 4)
                    {
                        dealerPoints = 5;
                    }
     
                    if(shuffleCards == 5)
                    {
                        dealerPoints = 6;
                    }
     
                    if(shuffleCards == 6)
                    {
                        dealerPoints = 7;
                    }
     
                    if(shuffleCards == 7)
                    {
                        dealerPoints = 8;
                    }
     
                    if(shuffleCards == 8)
                    {
                        dealerPoints = 9;
                    }
     
                    if(shuffleCards == 9 || shuffleCards == 10 || shuffleCards == 11 || shuffleCards == 12)
                    {
                        dealerPoints = 10;
                    }
     
                    if((shuffleCards == 10 && shuffleSuits == 2 )|| (shuffleCards == 10 && shuffleSuits == 0) || (shuffleCards == 12 && shuffleSuits == 1))
                    {
                        dealerPoints = 13;
                    }
     
                    if(shuffleCards == 2 && shuffleSuits == 3)
                    {
                        dealerPoints = 0;
                    }
     
                    if(shuffleCards == 0 && (dealerTotal <= 22))
                    {
                        dealerPoints = 11;
                    }
     
                    if(shuffleCards == 0 && (dealerTotal > 22))
                    {
                        dealerPoints = 1;
                    }
     
                    dealerTotal = dealerPoints + dealerTotal; 
     
                    System.out.print(cards[shuffleCards] + suits[shuffleSuits].concat(", "));     
     
                }
     
                System.out.println("(Total " + dealerTotal + ")");
     
                System.out.println();
     
                if((33 - playerTotal < 33 - dealerTotal) && playerTotal <= 33 || dealerTotal > 33)
                {
                    score = "win!";
                    wins++;
                }
     
                if((33 - playerTotal > 33 - dealerTotal) && dealerTotal <= 33 || playerTotal > 33)
                {
                    score = "lose!";
                    loses++;
                }
     
                if(playerTotal == dealerTotal)
                {
                    score = "tied!";                
                    pushes++;
                }
     
                System.out.println("You " + score + " - Current Total - Wins - " + wins + " Ties - " + pushes + " Loses - " + loses);
     
                System.out.println();
     
                System.out.print("Would you like to play again (Y/Q)? ");
                playAgain = scan.nextLine();
     
                if(playAgain.equalsIgnoreCase("y"))
                {
                    playerTotal = 0;
                    dealerTotal = 0;
                    playerPoints = 0;
                    dealerPoints = 0;
     
                    PSB.delete(0, PSB.length());
                    showCard = "";
                }
     }
            }        
        }    
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Arrays (Java card game)

    If you changed how the cards are stored to use an ArrayList, then you could shuffle the list and remove the cards one at a time from the list.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with Arrays (Java card game)

    I didn't even check your code for your dealing design, but a clever way to do it (I think) is to:

    Assuming each card is an object (adjust if it's not):
    Load the deck of cards into an array, order doesn't matter.
    Set the number of available cards = to the size of the array of cards.

    Dealing Loop:
    Pick an index randomly, 0 - number of available cards.
    Deal the card at the random index.
    Swap the card dealt with the card at index ( numberOfAvailableCards - 1 ) (the current last available card).
    Reduce the number of available cards by 1.
    Repeat Dealing Loop as necessary.

    When the numberOfAvailableCards has reached 0, there are no more cards in the deck. The deck is ready for reuse just as it is because it has been shuffled as part of the dealing process, or a new deck can be created, if desired. It doesn't matter.

    Let me know if you have questions.

  4. #4
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays (Java card game)

    Quote Originally Posted by Norm View Post
    If you changed how the cards are stored to use an ArrayList, then you could shuffle the list and remove the cards one at a time from the list.
    I thought about using ArrayList since I can remove something if I want, but I HAVE to use Array for this.

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    I didn't even check your code for your dealing design, but a clever way to do it (I think) is to:

    Assuming each card is an object (adjust if it's not):
    Load the deck of cards into an array, order doesn't matter.
    Set the number of available cards = to the size of the array of cards.

    Dealing Loop:
    Pick an index randomly, 0 - number of available cards.
    Deal the card at the random index.
    Swap the card dealt with the card at index ( numberOfAvailableCards - 1 ) (the current last available card).
    Reduce the number of available cards by 1.
    Repeat Dealing Loop as necessary.

    When the numberOfAvailableCards has reached 0, there are no more cards in the deck. The deck is ready for reuse just as it is because it has been shuffled as part of the dealing process, or a new deck can be created, if desired. It doesn't matter.

    Let me know if you have questions.
    Is there a way to compare arrays? My plan is to get the cards that are already chosen and put it an array. I know the maximum is 52 cards, so I can declare that array size to be 52. Then, my plan is to compare next card to the cards that are already selected. If they match, pick another card till it doesn't match.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Arrays (Java card game)

    You can get the same effect if you shuffle the array and use an index that points to the next card to deal.
    Increment the index as the cards are dealt.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays (Java card game)

    Quote Originally Posted by Norm View Post
    You can get the same effect if you shuffle the array and use an index that points to the next card to deal.
    Increment the index as the cards are dealt.
    Thanks, I'll try that.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with Arrays (Java card game)

    Shuffling and comparing to cards already dealt are all unnecessary steps with my approach, but you do as you decide is best.

  8. #8
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with Arrays (Java card game)

    You could make a 2D string array to hold the unique cards. Something like this:
    String[][] deck = new String[suits.length()][cards.length()];
     
    for (int i = 0; i < cards.length(); i++)
         (for int j = 0; j < suits.length(); j++)
              deck[i][j] = cards[i] + suits[j];

    The only problem that remains after that is mapping the indices correctly given 1-52. It shouldn't be too difficult for you to figure out though

Similar Threads

  1. Hi Lo card game
    By ryanc33 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 07:01 PM
  2. arrays of card objects
    By Dekpo in forum Member Introductions
    Replies: 1
    Last Post: August 28th, 2012, 08:48 PM
  3. Help with Card Game
    By JSeol14 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2012, 10:46 PM
  4. Random suit and card with arrays
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 1st, 2012, 10:28 AM
  5. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM