0% found this document useful (0 votes)
82 views

Blackjack Calculation Java Program

This document contains the code for a Java program that simulates blackjack games to calculate the probabilities of getting different hands. It creates a Player class with methods to generate random hands, calculate the value, and clear hands. The main method runs 200 million simulations, tracking results in categories like 17, 18, 19, 20, blackjack, and bust. It outputs the percentage of results in each category.

Uploaded by

Leo Ivan Brown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Blackjack Calculation Java Program

This document contains the code for a Java program that simulates blackjack games to calculate the probabilities of getting different hands. It creates a Player class with methods to generate random hands, calculate the value, and clear hands. The main method runs 200 million simulations, tracking results in categories like 17, 18, 19, 20, blackjack, and bust. It outputs the percentage of results in each category.

Uploaded by

Leo Ivan Brown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Blackjack Calculation Java Program

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import java.util.Random;

import java.util.Scanner;

public class Blackjack {

public static void main(String[] args) {

/*

Scanner input = new Scanner(System.in);

System.out.print("How many times to run? ");

int reps = input.nextInt();

*/

Player player = new Player();

int reps = 200000000;

ArrayList<Integer> handTotals = new ArrayList<>();

for(int i =0;i<reps;i++) {

handTotals.add(player.getResult());

int seventeens = 0;

int eighteens = 0;

int nineteens = 0;

int twenties = 0;

int blackjacks = 0;

int busts = 0;

for(int result:handTotals) {

if (result== -1) {

busts+=1;

else if (result== 21) {

blackjacks+=1;

else if (result== 17) {

seventeens+=1;

else if (result== 18) {

eighteens+=1;

else if (result== 19) {

nineteens+=1;

else if (result== 20) {

twenties+=1;

String printout = "";

printout += "Seventeens: "+


((float)seventeens/(float)handTotals.size())*100+"\n";

printout += "Eighteens: "+


((float)eighteens/(float)handTotals.size())*100+"\n";

printout += "Nineteens: "+


((float)nineteens/(float)handTotals.size())*100+"\n";

printout += "Twenties: "+


((float)twenties/(float)handTotals.size())*100+"\n";

printout += "Blackjacks: "+


((float)blackjacks/(float)handTotals.size())*100+"\n";

printout += "Busts: "+


((float)busts/(float)handTotals.size())*100+"\n";
System.out.println(printout);

class Player {

private ArrayList<Integer> hand = new ArrayList<>();

private ArrayList<Integer> deck = new ArrayList<>();

Player(){

for(int i=1;i<10;i++) {

deck.add(i);

for(int i=0;i<=3;i++) {

deck.add(10);

public int getResult() {

while (CalculateHand() <17){

Draw();

if(CalculateHand()>21) {

Clear();

return -1;

int result = CalculateHand();

Clear();

return result;

private void Clear() {

hand = new ArrayList<>();

private int CalculateHand() {

int total = 0;

int aces = 0;

for(int number:hand) {

if(number !=1) {

total += number;

else {

aces+=1;

for (int i=0;i<aces-1;i++) {

total +=1;

if(aces>=1) {

if (total+11<=21) {

total += 11;

else {

total += 1;

return total;

public void Draw() {

Random rand = new Random();

int index = (int)(Math.random()*(13-1+1)+1);

int card = deck.get(index-1);

hand.add(card);

public String ShowHand() {

String result = "";

for (int card:hand) {

result += String.valueOf(card);

return result;

You might also like