Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
October 29th, 2013, 02:07 PM
 tonynsx
Contributing User

Join Date: Aug 2013
Posts: 78
Time spent in forums: 10 h 53 m 18 sec
Reputation Power: 1
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:

Code:
``` 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:

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;

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();

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: ");

{
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;
}

if(shuffleCards == 0 && (dealerTotal + 11 <= 33))
{
}

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++;
}

{
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();

}
}
}
}```

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Help with card game (ace could be 1 or 11)

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |