Hey everyone I'm writing a program that simulates a blackjack game not a playable game but one to find the statistics if you win or lose and find the percents. This is as far as i have gotten and I'm not stuck and at a complete loss. Any ideas or suggestions would be helpful.
PHP Code:
import java.util.*;
public class BlackjackSkeletonNoRand {
public static void main(String[] args) {
String outcome;
int trials = 100;
int wins = 0, losses = 0, pushes = 0;
int rand=(int)(13*Math.random()+1);
//Starts the loop for each trial and prints if its a win or a loss.
for ( int i = 1; i <= trials; i++){
outcome = blackjack(rand);
System.out.println("The player has a " + outcome + "\n");
if ( outcome.equals("win"))
wins++;
else if (outcome.equals("loss"))
losses++;
else
pushes++;
}
System.out.println("For " + trials
+ " hands of blackjack, the totals are:\n"
+ "wins = " + wins + "\nlosses = " + losses
+ "\npushes = " + pushes);
System.out.println("% wins = " + (double) wins/trials * 100
+ " % losses = " + (double)losses/trials * 100
+ " % house advantage = "
+ (double)(losses - wins)/trials * 100);
}
// Returns "win", "loss" or "push"
public static String blackjack (int cardvalue){
int numPlayerAces = 0;
int numDealerAces = 0;
int playerTot = 0;
int dealerShows =0;
int hole;
// First card to player, face up
int currentCard = cardValue();
if (currentCard == 1 )
numPlayerAces++;
else
playerTot = currentCard;
printCurrentPlayerHand(currentCard, playerTot, numPlayerAces);
// First card to dealer, face up
currentCard = cardValue();
if ( currentCard == 1 )
numDealerAces++;
else
dealerShows = currentCard;
printCurrentDealerHand(currentCard, dealerShows, numDealerAces,0);
// Second card to player, face up
currentCard = cardValue();
if (currentCard == 1 )
numPlayerAces++;
else
playerTot += currentCard;
printCurrentPlayerHand( currentCard, playerTot, numPlayerAces);
// Second card to dealer, face down
hole = cardValue();
printCurrentDealerHand(hole, dealerShows, numDealerAces,hole);
int dealerPrivateTot;
if ( hole == 1 )
dealerPrivateTot = dealerShows
+ computeAces(numDealerAces + 1, dealerShows);
else
dealerPrivateTot = dealerShows + hole
+ computeAces(numDealerAces, dealerShows);
// Test for immediate bust or blackjack with push or win.l
int acesValue = computeAces(numPlayerAces, playerTot);
while (playerTot + acesValue < 21 )
{
// your code here
playerTot += 10; // This is here just to make the loop terminate. Delete it.
}
if ( playerTot + acesValue > 21)
return "loss";
while (dealerPrivateTot < 17 )
{
}
if (dealerPrivateTot > 21 )
return "win";
playerTot += computeAces(numPlayerAces, playerTot);
if (playerTot == dealerPrivateTot)
return "push";
if (playerTot > dealerPrivateTot)
return "win";
return "loss";
}
// method cardValue()
// Returns the value of the card. 10 - 13 are 10. 2 - 9 are that value,
// 1 is ace and returned as 1.
public static int cardValue(int rand){
int cardValue = rand;
if (cardValue>=10)
cardValue=10;
else cardValue=rand;
return cardValue;
}
// method computeAces() returns the total current value of all the aces
// given the current total and the number of aces, return the total value of the aces.
// e.g. total = 7, numAces = 2, returns 12, 1 ace is 11 and the other is 1.
// Note: Only 1 ace can be 11.
public static int computeAces(int numAces, int cardValue){
int cards = cardValue;
int ace = 1;
if (cards+ace<=21)
ace=11;
else
ace=1;
numAces=ace;
return numAces;
}
// methods printCurrentPlayerHand() and printCurrentDealerHand()
// In printCurrentDealerHand if the hole card is an ace, it is not counted in the number of aces.
public static void printCurrentPlayerHand(int a, int b , int c){
}
public static void printCurrentDealerHand(int a, int b , int c, int d){
}
}