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 2 of 2

Thread: Help with card game (ace could be 1 or 11)

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

    Default Help with card game (ace could be 1 or 11)

    Hello,

    In this project, the ace card could be either 1 or 11 depending on the situation. The objective of the game is to not go over 33.

    example,

    Ace calculates the values as either 1 or 11 according to the best interest of the player.

    Ace, 5,7,9 is 32 , here ace is 11 because, (11 + 5 + 7 + ) is not over 33.

    Ace, 6, 8, 10 is 25, here ace is 11 because (1 + 6 + 8 + 10 ) is not over 33. If ace was counted as 11 it will be 35.

    All the other values of the card works, except for the ace card because it could be either 1 or 11. Other card has fixed values.

    so if all 3 cards are aces, it should be 33, 11 + 11 + 11 (best interest of the player)

    That's what I need help with. I'm sure there's something wrong with my if condition, but I can't figure it.

    Here's the part of the code where the problem is:

          playerTotal = playerPoints + playerTotal;
     
                    if(shuffleCards == 0 && playerTotal + 11 <= 33)
                    {
                            playerTotal = playerTotal + 11;
                    }
     
                    showCard = showCard.concat(cards[shuffleCards]).concat(suits[shuffleSuits].concat(", "));
                }
     
                if(shuffleCards == 0 && playerTotal + 1 <= 33)
                {
                    playerTotal = playerTotal + 1;
                }


    Full code:

    package cardgame;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class CardGame {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            Random rand = new Random();
     
            String[] cards = {"Ace", "Two", "Three", "four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
            String[] suits = {" of Hearts", " of Diamonds", " of Spades", " of Clubs"};
     
            int shuffleCards = 0, shuffleSuits, playerPoints, dealerPoints, playerTotal, dealerTotal, wins, loses, pushes;
     
            String play, anotherCard, showCard, playAgain, score;
     
            StringBuilder PSB = 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;
     
                    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;
                    }
     
                    playerTotal = playerPoints + playerTotal;
     
                    if(shuffleCards == 0 && playerTotal + 11 <= 33)
                    {
                            playerTotal = playerTotal + 11;
                    }
     
                    showCard = showCard.concat(cards[shuffleCards]).concat(suits[shuffleSuits].concat(", "));
                }
     
                if(shuffleCards == 0 && playerTotal + 1 <= 33)
                {
                    playerTotal = playerTotal + 1;
                }
     
                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;
                    }
     
                    playerTotal = playerPoints + playerTotal;
     
                    if(shuffleCards == 0)
                    {
                        if(playerTotal + 11 <= 33)
                        {
                            playerTotal = playerTotal + 11;
                        }
     
                        else
                        {
                            playerTotal = playerTotal + 1;
                        }
                    }
     
                    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 == 10 && shuffleSuits == 2 || shuffleCards == 10 && shuffleSuits == 0 || shuffleCards == 12 && shuffleSuits == 1)
                    {
                        dealerPoints = 13;
                    }
     
                    if(shuffleCards == 2 && shuffleSuits == 3)
                    {
                        dealerPoints = 0;
                    }
     
                    dealerTotal = dealerPoints + dealerTotal; 
     
                    if(shuffleCards == 0 && (dealerTotal + 11 <= 33))
                    {
                        dealerTotal = dealerTotal + 11;               
                    }
     
                    System.out.print(cards[shuffleCards] + suits[shuffleSuits].concat(", "));                  
                }
     
                System.out.println(" (Total " + dealerTotal + ")");
     
                System.out.println();
     
                // change the conditions for win, lose 
                // if the player is 32, and dealer is 31, player wins, who ever is closest to 33
     
                if(playerTotal <= 33 )
                {
                    score = "win!";                
                    wins++;
                }
     
                if(playerTotal > 33 && (dealerTotal > 26 && dealerTotal <= 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();
     
    }
            }        
        }    
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Help with card game (ace could be 1 or 11)

    Also posted at: Help with card game (ace could be 1 or 11) - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

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. Card Class (Setting string rank value. 1 = "Ace" etc)
    By ashboi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2012, 04:42 PM
  3. Help with Card Game
    By JSeol14 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2012, 11:46 PM
  4. A Card Game
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2012, 07:30 AM
  5. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 08:55 AM