Everything else works fine except I run into an issue of printing out the card the player (if they 'hit' ) as I don't know what card the player draws and which player draws what card, it starts at 8 as the first 7 cards are drawn but I don't know what player gets what and how many. So I tried to have a method to print out the hand of each player but I'm having a LOT of trouble with that could use some help. I want to print the cards the players get in my output. I'll post my Card, Player, and BlackJackGame class as my Dealer class is very similar to player.
Card.java
import java.util.Random;
public class Card
{
private String suit, rank;
private int value;
public Card(String suit, String rank)
{
this.suit = suit;
this.rank = rank;
}
public String getRank()
{
return rank;
}
public int Value()
{
if(rank.equals("2"))
{
value=2;
}
else if(rank.equals("3"))
{
value=3;
}
else if(rank.equals("4"))
{
value=4;
}
else if(rank.equals("5"))
{
value=5;
}
else if(rank.equals("6"))
{
value=6;
}
else if(rank.equals("7"))
{
value=7;
}
else if(rank.equals("8"))
{
value=8;
}
else if(rank.equals("9"))
{
value=9;
}
else if(rank.equals("10"))
{
value=10;
}
else if(rank.equals("A"))
{
Random rand = new Random();
int count = rand.nextInt(1) +1;
if(count == 1)
{
value=11;
}
else
value= 1;
}
else if(rank.equals("Q"))
{
value=10;
}
else if(rank.equals("J"))
{
value=10;
}
else if(rank.equals("K"))
{
value=10;
}
return value;
}
public String toString()
{
return(rank + " of " + suit);
}
}
Player.java
public class Player
{
private int cValue;
private int cCount; //Card count used to count how many 'cards' added
Card[] deck= new Card[52];
private int sum;
public Player()
{
cCount=0;
}
public Card addCard(Card a)
{
deck[cCount] = a;
cCount++;
return a;
}
public int getcCount()
{
return cCount;
}
public int getValue()
{
int total=0;
for(int i=0; i < cCount; i++)
{
total += deck[i].Value();
}
return total;
}
BlackJackGame.java
public class BlackJackGame
{
public static void main(String [] args)
{
Card[] deck = new Card[52];
Player[] player = new Player[3];
int loopcount=0;
String p1result = " ", p2result = " ", p3result = " ", p4result = " ", dresult = " ";
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
String[] rank = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for(int i=0; i<13; i++)
{
for(int x=0; x<4;x++)
{
deck[loopcount] = new Card(suit[x], rank[i]);
loopcount++;
}
}
System.out.println("Shuffling...");
for(int i=0; i< deck.length; i++) //Shuffle
{
Card tmp = deck[i];
int count= (int)(Math.random()* deck.length);
deck[i] = deck[count];
deck[count] = tmp;
}
Player player1 = new Player();
Player player2 = new Player();
Player player3 = new Player();
System.out.println("Welcome to our BlackJackGame!");
System.out.println("Welcome Dealer!");
Dealer dealer = new Dealer();
System.out.println("Let's deal the cards!");
player1.addCard(deck[0]);
player2.addCard(deck[1]);
player3.addCard(deck[2]);
System.out.println("And now the Dealer gets his card...");
dealer.addCard(deck[3]);
System.out.println("Now we get our second cards!");
System.out.println("Okay Dealer, deal out the cards!");
player1.addCard(deck[4]);
player2.addCard(deck[5]);
player3.addCard(deck[6]);
dealer.addCard(deck[7]);
int count =8;
int i=0;
do
{
p1result = "";
p2result = "";
p3result = "";
dresult = "";
int dvalue = dealer.getValue();
int p1value = player1.getValue();
int p2value = player2.getValue();
int p3value = player3.getValue();
while(p1value < 17) //hit
{
player1.addCard(deck[count]);
count++;
p1value = player1.getValue();
}
if(p1value > 21)
{
p1result = "Bust!";
}
if(p1value <21 && p1value >17)//stand
{
}
while(p2value < 17)//hit
{
player2.addCard(deck[count]);
count++;
p2value = player2.getValue();
}
if(p2value > 21) //bust
{
p2result = "Bust!";
}
if(p2value <21 && p2value >17) //stand
{
}
while(p3value < 17) //hit
{
player3.addCard(deck[count]);
count++;
p3value = player3.getValue();
}
if( p3value > 21)
{
p3result = "Bust!";
}
if(p3value <21 && p3value >21) //stand
{
}
while(dvalue < 17)
{
dealer.addCard(deck[count]);
count++;
dvalue = dealer.getValue();
}
if(dvalue > 21) //Bust
{
p1value = player1.getValue();
p2value = player2.getValue();
p3value = player3.getValue();
if(p1value == 21 || p1value <21)
{
p1result = "Win!";
}
if(p2value == 21 || p2value <21)
{
p2result = "Win!";
}
if(p3value == 21 || p3value <21 )
{
p3result = "Win!";
}
}
if(dvalue < 21 && dvalue >= 17) //For Dealer values in between
{
p1value = player1.getValue();
p2value = player2.getValue();
p3value = player3.getValue();
dvalue = dealer.getValue();
if(p1value == dvalue)
{
p1result = "Push!";
}
if(p1value > dvalue)
{
p1result = "Win!";
}
if(p1value < dvalue)
{
p1result = "Lose!";
}
if(p2value == dvalue)
{
p2result = "Push!";
}
if(p2value > dvalue)
{
p2result = "Win!";
}
if(p2value < dvalue)
{
p2result = "Lose!";
}
if(p3value == dvalue)
{
p3result = "Push!";
}
if(p3value > dvalue)
{
p3result = "Win!";
}
if(p3value < dvalue)
{
p3result = "Lose!";
}
}
if(dvalue == 21 )
{
p1value = player1.getValue();
p2value = player2.getValue();
p3value = player3.getValue();
dvalue = dealer.getValue();
if(p1value == dvalue)
{
p1result = "Push!";
}
if(p1value < dvalue || p1value > dvalue)
{
p1result = "Lose!";
}
if(p2value == dvalue)
{
p2result = "Push!";
}
if(p2value < dvalue || p2value > dvalue)
{
p2result = "Lose!";
}
if(p3value == dvalue)
{
p3result = "Push!";
}
if(p3value < dvalue || p3value > dvalue)
{
p3result = "Lose!";
}
}
System.out.println("The BlackJack Game is Complete: ");
System.out.println("Results: ");
System.out.println("Dealer: " +deck[3] + " " + deck[7] + " " +("total of " +dealer.getValue() ));
System.out.println("Player1: " +deck[0] + " " + deck[4] + " "+("total of " +player1.getValue() )+ ": " +p1result);
System.out.println("Player2: " +deck[1] + " " + deck[5] + " "+("total of " +player2.getValue() )+ ": " +p2result);
System.out.println("Player3: " +deck[2] + " " + deck[6] + " "+("total of " +player3.getValue() )+ ": " +p3result);
i++;
}
while(i <1);
}
}
Example Output:
BlackJack Game is Complete!
Results!
Dealer : 6 of Clubs 8 of Spades total 17
Player1: 2 of Clubs 2 of Spades total 16 Lose!
Player2: Q of Hearts Q of Spades total 20 Win!
Player3: A of Spades 6 of Hearts total 17 Push!
// The Problem is when the cards are printed , I print out the first two cards each player has and the dealer which are known. The problem I run into is if the player or dealer 'hits' as you can see with Player 1 I don't know which cards are hit/ and which player has them and was wondering how to have a method in the Player class that would print the hand of each player including the dealer and the cards they have for my output. That is what I need help with. Everything else works fine.
First, I would suggest using a switch statement instead of all those else ifs for rank, it'll be a lot neater :)
Second, in Blackjack an Ace doesn't have a "random" value of either 1 or 11; it is an 11 unless it being an 11 makes the player bust.
Third, I would consider using a list for the deck. Then all you need to do is "shuffle" the list and when you deal a card, remove the top card from the list and add it to the player's hand. (see Collections.Shuffle)
Now to address the initial question, you should have a List hand in the player class. Then add an extension method to the player class to print the hand; it should look something like this: