Jump to content

 

Hey im working on a project for school and ive hit a major road block. We have to essentially create a game of blackjack using a JavaFX GUI. Part one was to initialize 5 decks of cards which i did, part 2 was to shuffle the decks together using a button. Im having a problem with part 3 and 4. For them we have to create a hand of 2 cards (basic blackjack rules) and at the press of a button add another card. You have to use images of cards to do this also. I have no idea on how to go about doing this...

 

 

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;

 

public class ProjectBlakcJack extends Application {

public void start(Stage primaryStage) throws Exception{

 

DeckOfCards deck = new DeckOfCards();

 

HBox text = new HBox();

Label label1 = new Label("Let's play some black jack!");

text.getChildren().addAll(label1);

 

HBox cards = new HBox();

   Image img = new Image("1.png");

   ImageView imgView = new ImageView(img);

   Image img0 = new Image("b1fv.png");

   ImageView imgView0 = new ImageView(img0);

   Image img1 = new Image("b1fv.png");

   ImageView imgView1 = new ImageView(img1);

   Image img2 = new Image("b1fv.png");

   ImageView imgView2 = new ImageView(img2);

   Image img3 = new Image("b1fv.png");

   ImageView imgView3 = new ImageView(img3);

   cards.getChildren().addAll(imgView, imgView0, imgView1, imgView2,imgView3);

 

HBox hb = new HBox(50);

 

Button btn1 = new Button("Shuffle");

Button btn2 = new Button("Deal");

hb.getChildren().addAll(btn1,btn2);

 

BorderPane pane = new BorderPane();

pane.setTop(text);

pane.setCenter(cards);

pane.setBottom(hb);

 

btn2.setOnAction((clicked) -> {System.out.println("Hit me");});

btn1.setOnAction((clicked) -> {deck.shuffle();});

 

   Scene scene = new Scene(pane, 360, 150);

   primaryStage.setTitle("Card Game"); 

   primaryStage.setScene(scene); 

   primaryStage.show();

}

 

public static void main(String[] args){

Application.launch(args);

}

 

public class DeckOfCards {

 

int[] deck;

int next_card = 0;

 

 

public static final int SIZE_OF_DECK = 52;

 

public DeckOfCards(){

this(1);

}

 

public DeckOfCards(int num_decks){

 

this.deck  = new int[num_decks*DeckOfCards.SIZE_OF_DECK];

 

for(int i = 0; i<this.deck.length; i++){

int k = i%DeckOfCards.SIZE_OF_DECK;

this.deck = k;

}

 

}

 

public void shuffle(){

 

for(int i = 0; i<this.deck.length; i++){

int j = (int)(Math.random()*this.deck.length);

int temp = this.deck;

this.deck = this.deck[j];

this.deck[j] = temp;

}

this.next_card = 0;

System.out.println("Shuffled");

 

}

 

public Card nextCard(){

return new Card(this.deck[this.next_card++]);

}

 

public boolean hasNext(){

return this.next_card<this.deck.length;

}

 

 

}

}

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/
Share on other sites

Link to post
Share on other sites

Could you paste the full code? Or perhaps put it up on github?

It's much easier to work with a full working example.

Also, make sure that all of your classes are in their own files--I can't tell just by looking at what you've pasted, but in your paste you have to classes in the same file.

Edit:

but as a rule of thumb with JavaFX--you'll just want to add a new image (the new card) to your HBox there, and probably update the scene/frame.

--Neil Hanlon

Operations Engineer

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4955736
Share on other sites

Link to post
Share on other sites

Could you paste the full code? Or perhaps put it up on github?

It's much easier to work with a full working example.

Also, make sure that all of your classes are in their own files--I can't tell just by looking at what you've pasted, but in your paste you have to classes in the same file.

Edit:

but as a rule of thumb with JavaFX--you'll just want to add a new image (the new card) to your HBox there, and probably update the scene/frame.

 

 

That is the full code. The only thing its missing is the image files. I have the images in the center HBox right now. I know i need to make that the hand but have no idea what i can do about it.

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4955809
Share on other sites

Link to post
Share on other sites

That is the full code. The only thing its missing is the image files. I have the images in the center HBox right now. I know i need to make that the hand but have no idea what i can do about it.

 

It's missing a "Card" class.

 

2015-05-13_1247.png

--Neil Hanlon

Operations Engineer

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4955866
Share on other sites

Link to post
Share on other sites

Oh it got cut off when i was copying it over

 

public class Card { private int card; private static final String[] suits = {"Clubs", "Diamonds","Hearts", "Spades"};private static final String[] ranks = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen", "King"}; public Card(int c){this.card = c;} private String getSuit(){return Card.suits[this.card/13];} private String getRank(){return Card.ranks[this.card%13];} public int compareTo(Card otherCard) {if(this.getSuit() == otherCard.getSuit()){   if(otherCard.getRank() == "Ace") return -1;if(this.getRank() == "Ace") return 1;}if(this.card < otherCard.card) return -1;else if (this.card > otherCard.card) return 1;else return 0;} @[member=OverRide]public String toString(){return  this.getRank() + " of " + this.getSuit();}}

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4955880
Share on other sites

Link to post
Share on other sites

- snip -

 

hey im getting this string of errorsarrow-10x10.png and i have no idea why. Ive changes nothing but it worked earlier today 

 

Exception in Application start methodjavalang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at com.sun.javafx application.LauncherImpl.launchApplicationWithArgs(Unknown Source)at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)Caused by: java.lang.RuntimeException: Exception in Application start methodat com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(Unknown Source)at com.sun.javafx.application.LauncherImpl$$Lambda$48/128893786.run(Unknown Source)at java.lang.Thread.run(Unknown Source)Caused by: java.lang.IllegalArgumentException: invalid URL: Invalid URL or resource not foundat javafx.scene.image.Image.validateUrl(Unknown Source)at javafx.scene.image.Image.<init>(Unknown Source)at Black_Jack.FinalProjectBlackJack.start(FinalProjectBlackJack.java:22)at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)at com.sun.javafx.application.LauncherImpl$$Lambda$51/544216194.run(Unknown Source)at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)at com.sun.javafx.application.PlatformImpl$$Lambda$45/1051754451.run(Unknown Source)at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)at com.sun.javafx.application.PlatformImpl$$Lambda$47/1400508243.run(Unknown Source)at java security.AccessController.doPrivileged(Native Method)at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)... 1 moreCaused by: java.lang.IllegalArgumentException: Invalid URL or resource not found... 17 moreException running application Black_Jack.FinalProjectBlackJack

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4960016
Share on other sites

Link to post
Share on other sites

hey im getting this string of errors and i have no idea why. Ive changes nothing but it worked earlier today

 

My guess would be you tried to load an image that couldn't be found. Possibly due to using the incorrect file name.

 

If I were you, the first thing I would do would be to get the game working without worrying about the images. Once you can play the game, it shouldn't be too hard to finish it by adding in the code to display the images.

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4960049
Share on other sites

Link to post
Share on other sites

My guess would be you tried to load an image that couldn't be found. Possibly due to using the incorrect file name.

 

If I were you, the first thing I would do would be to get the game working without worrying about the images. Once you can play the game, it shouldn't be too hard to finish it by adding in the code to display the images.

 

That was exactly it. I split the code up intop a package with seperate classes and the immage files didnt add. How would i add the immage files to my package? They are not appearing in the navigator at all

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4960129
Share on other sites

Link to post
Share on other sites

That was exactly it. I split the code up intop a package with seperate classes and the immage files didnt add. How would i add the immage files to my package? They are not appearing in the navigator at all

 

What IDE are you using? If you're using eclipse--then i'll just go throw up. ;)

 

You need to put the image in your src folder--you can make it a subfolder like "resources"--but it has to be inside src.

--Neil Hanlon

Operations Engineer

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4960211
Share on other sites

Link to post
Share on other sites

What IDE are you using? If you're using eclipse--then i'll just go throw up. ;)

 

You need to put the image in your src folder--you can make it a subfolder like "resources"--but it has to be inside src.

 

....i am in eclipse LOL xD

 

And yah i know it is in my src folder but the IDE is not seeing them at all.

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4960314
Share on other sites

Link to post
Share on other sites

If you want to try other nice java IDE try netbeans that's what I used with my java projects.

 

Or Intellij Idea. Free if you're a student. Otherwise, rates are pretty reasonable.

 

....i am in eclipse LOL xD

 

And yah i know it is in my src folder but the IDE is not seeing them at all.

 

You probably have to reload the project.

--Neil Hanlon

Operations Engineer

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4962862
Share on other sites

Link to post
Share on other sites

I found my source code if you wanted to see it just know that it is a bit messy and there are some errors in the rules of the blackjack game. But the code runs fine in most cases. And don't expect to much it was my first time using images and I only spent a day or 2 on it.

Needs Update

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4963048
Share on other sites

Link to post
Share on other sites

I found my source code if you wanted to see it just know that it is a bit messy and there are some errors in the rules of the blackjack game. But the code runs fine in most cases. And don't expect to much it was my first time using images and I only spent a day or 2 on it.

 

Sure mabey it will help to give me ideas lol

 

 

Or Intellij Idea. Free if you're a student. Otherwise, rates are pretty reasonable.

 

 

You probably have to reload the project.

 

 

Idk what happened i tryed that but they still are not appearing. idk ill take a another swing at it

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4963924
Share on other sites

Link to post
Share on other sites

package blackjackv1;

 
import java.awt.Container;
import java.awt.TextField;
import javax.swing.JFrame;
 
public class BlackJackApplet
  extends JFrame
{
  TextField PlayerCards;
  TextField AICards;
  String A;
  String P;
  
  public BlackJackApplet()
  {
    getContentPane().setLayout(null);
    this.PlayerCards = new TextField(this.P);
    add(this.PlayerCards);
    this.PlayerCards.setBounds(50, 50, 100, 100);
    
    this.AICards = new TextField(this.A);
    this.AICards.setBounds(150, 50, 100, 100);
    add(this.AICards);
  }
}

 

package blackjackv1;
 
import java.awt.Container;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class BlackJackV1
  extends JFrame
  implements ActionListener
{
  static boolean[] Avalible = new boolean[52];
  static String[] Cards = new String[52];
  static int randomcard;
  static int w = 0;
  static int[] PCurrentCards = new int[5];
  static int[] AICurrentCards = new int[5];
  private static final long serialVersionUID = 1L;
  static boolean PBust = false;
  static boolean AIBust = false;
  static boolean HasRun0 = false;
  static boolean HasRun1 = false;
  static boolean HasRun2 = false;
  static boolean HasRun3 = false;
  static boolean HasRun4 = false;
  static boolean HasRun02 = false;
  static boolean HasRun12 = false;
  static boolean HasRun22 = false;
  static boolean HasRun32 = false;
  static boolean HasRun42 = false;
  static int PPointsTotal;
  static int AIPointsTotal;
  static int toDraw = 2;
  static int toDraw2 = 2;
  TextField PlayerCards;
  TextField AICards;
  JButton butt1;
  JButton butt2;
  JButton butt3;
  JLabel AIBusted;
  JLabel PBusted;
  JLabel PCard1;
  JLabel PCard2;
  JLabel PCard3;
  JLabel PCard4;
  JLabel PCard5;
  JLabel ACard1;
  JLabel ACard2;
  JLabel ACard3;
  JLabel ACard4;
  JLabel ACard5;
  JLabel PCard50;
  JLabel PCard51;
  JLabel PCard52;
  
  public static void main(String[] args)
    throws IOException
  {}
  
  public static int DrawCard()
  {
    int Card = 1;
    Random();
    if (Avalible[randomcard] != 0)
    {
      Card = randomcard;
      Avalible[randomcard] = false;
    }
    else
    {
      w -= 1;
    }
    return Card;
  }
  
  private static void Random()
  {
    randomcard = (int)Math.floor(Math.random() * 51.0D + 1.0D);
  }
  
  private static void Reset()
    throws IOException
  {
    int i = 0;
    while (i < 50)
    {
      Avalible = true;
      i += 1;
    }
    StartOfMatch();
    PBust = false;
    AIBust = false;
    HasRun0 = false;
    HasRun1 = false;
    HasRun2 = false;
    HasRun3 = false;
    HasRun4 = false;
    HasRun02 = false;
    HasRun12 = false;
    HasRun22 = false;
    HasRun32 = false;
    HasRun42 = false;
  }
  
  private static void ResetGame()
    throws IOException
  {
    int i = 0;
    while (i < 50)
    {
      Avalible = true;
      i += 1;
    }
    StartOfMatch();
    PBust = false;
    AIBust = false;
    HasRun0 = false;
    HasRun1 = false;
    HasRun2 = false;
    HasRun3 = false;
    HasRun4 = false;
    HasRun02 = false;
    HasRun12 = false;
    HasRun22 = false;
    HasRun32 = false;
    HasRun42 = false;
  }
  
  public static void StartOfGame()
    throws IOException
  {
    int c = 0;
    while (c < 50)
    {
      Cards[c] = ("" + c);
      c += 1;
    }
    Reset();
  }
  
  public static void StartOfMatch()
    throws IOException
  {
    PCurrentCards[0] = DrawCard();
    PCurrentCards[1] = DrawCard();
    PCurrentCards[2] = 0;
    PCurrentCards[3] = 0;
    PCurrentCards[4] = 0;
    
    AICurrentCards[0] = DrawCard();
    AICurrentCards[1] = DrawCard();
    AICurrentCards[2] = 0;
    AICurrentCards[3] = 0;
    AICurrentCards[4] = 0;
    
    new BlackJackV1().dispose();
    new BlackJackV1().repaint();
  }
  
  private static String CardResolver(int a)
  {
    String[] names = { "nocard.png", "HA.png", "H2.png", "H3.png", "H4.png", "H5.png", "H6.png", "H7.png", "H8.png", "H9.png", "H10.png", "HJ.png", "HQ.png", "HK.png", "SA.png", "S2.png", "S3.png", "S4.png", "S5.png", "S6.png", "S7.png", "S8.png", "S9.png", "S10.png", "SJ.png", "SQ.png", "SK.png", "DA.png", "D2.png", "D3.png", "D4.png", "D5.png", "D6.png", "D7.png", "D8.png", "D9.png", "D10.png", "DJ.png", "DQ.png", "DK.png", "CA.png", "C2.png", "C3.png", "C4.png", "C5.png", "C6.png", "C7.png", "C8.png", "C9.png", "C10.png", "CJ.png", "CQ.png", "CK.png" };
    return names[a];
  }
  
  private void CheckBust()
    throws IOException
  {
    int[] number = { 0, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
    PPointsTotal = number[PCurrentCards[0]] + number[PCurrentCards[1]] + number[PCurrentCards[2]] + number[PCurrentCards[3]] + number[PCurrentCards[4]];
    AIPointsTotal = number[AICurrentCards[0]] + number[AICurrentCards[1]] + number[AICurrentCards[2]] + number[AICurrentCards[3]] + number[AICurrentCards[4]];
    if (PPointsTotal > 21)
    {
      if (((PCurrentCards[0] == 1) || (PCurrentCards[0] == 14) || (PCurrentCards[0] == 27) || (PCurrentCards[0] == 40)) && (!HasRun0))
      {
        PPointsTotal -= 10;
        HasRun0 = true;
      }
      if (((PCurrentCards[1] == 1) || (PCurrentCards[1] == 14) || (PCurrentCards[1] == 27) || (PCurrentCards[1] == 40)) && (!HasRun1))
      {
        PPointsTotal -= 10;
        HasRun1 = true;
      }
      if (((PCurrentCards[2] == 1) || (PCurrentCards[2] == 14) || (PCurrentCards[2] == 27) || (PCurrentCards[2] == 40)) && (!HasRun2))
      {
        PPointsTotal -= 10;
        HasRun2 = true;
      }
      if (((PCurrentCards[3] == 1) || (PCurrentCards[3] == 14) || (PCurrentCards[3] == 27) || (PCurrentCards[3] == 40)) && (!HasRun3))
      {
        PPointsTotal -= 10;
        HasRun3 = true;
      }
      if (((PCurrentCards[4] == 1) || (PCurrentCards[4] == 14) || (PCurrentCards[4] == 27) || (PCurrentCards[4] == 40)) && (!HasRun4))
      {
        PPointsTotal -= 10;
        HasRun4 = true;
      }
      if (PPointsTotal > 21)
      {
        File file1 = new File("IMG/BUST.png");
        BufferedImage image1 = ImageIO.read(file1);
        this.PBusted = new JLabel(new ImageIcon(image1));
        this.PBusted.setBounds(50, -20, 200, 100);
        add(this.PBusted);
        PBust = true;
        winner();
      }
    }
    if (AIPointsTotal > 21)
    {
      if (((AICurrentCards[0] == 1) || (AICurrentCards[0] == 14) || (AICurrentCards[0] == 27) || (AICurrentCards[0] == 40)) && (!HasRun02))
      {
        AIPointsTotal -= 10;
        HasRun02 = true;
      }
      if (((AICurrentCards[1] == 1) || (AICurrentCards[1] == 14) || (AICurrentCards[1] == 27) || (AICurrentCards[1] == 40)) && (!HasRun12))
      {
        AIPointsTotal -= 10;
        HasRun12 = true;
      }
      if (((AICurrentCards[2] == 1) || (AICurrentCards[2] == 14) || (AICurrentCards[2] == 27) || (AICurrentCards[2] == 40)) && (!HasRun22))
      {
        AIPointsTotal -= 10;
        HasRun22 = true;
      }
      if (((AICurrentCards[3] == 1) || (AICurrentCards[3] == 14) || (AICurrentCards[3] == 27) || (AICurrentCards[3] == 40)) && (!HasRun32))
      {
        AIPointsTotal -= 10;
        HasRun32 = true;
      }
      if (((AICurrentCards[4] == 1) || (AICurrentCards[4] == 14) || (AICurrentCards[4] == 27) || (AICurrentCards[4] == 40)) && (!HasRun42))
      {
        AIPointsTotal -= 10;
        HasRun42 = true;
      }
      if (AIPointsTotal > 21)
      {
        File file20 = new File("IMG/BUST.png");
        BufferedImage image20 = ImageIO.read(file20);
        this.AIBusted = new JLabel(new ImageIcon(image20));
        this.AIBusted.setBounds(340, -20, 200, 100);
        add(this.AIBusted);
        AIBust = true;
        winner();
      }
    }
  }
  
  private BlackJackV1()
    throws IOException
  {
    super("BlackJack-V1");
    setVisible(true);
    setSize(600, 250);
    setResizable(false);
    setDefaultCloseOperation(3);
    getContentPane().setLayout(null);
    
    File file1 = new File("IMG/" + PCurrentCards[0] + ".png");
    BufferedImage image1 = ImageIO.read(file1);
    this.PCard1 = new JLabel(new ImageIcon(image1));
    this.PCard1.setBounds(10, 50, 200, 100);
    add(this.PCard1);
    
    File file2 = new File("IMG/" + PCurrentCards[1] + ".png");
    BufferedImage image2 = ImageIO.read(file2);
    this.PCard2 = new JLabel(new ImageIcon(image2));
    this.PCard2.setBounds(50, 50, 200, 100);
    add(this.PCard2);
    
    File file6 = new File("IMG/Back.png");
    BufferedImage image6 = ImageIO.read(file6);
    this.ACard1 = new JLabel(new ImageIcon(image6));
    this.ACard1.setBounds(300, 50, 200, 100);
    add(this.ACard1);
    
    File file7 = new File("IMG/Back.png");
    BufferedImage image7 = ImageIO.read(file7);
    this.ACard2 = new JLabel(new ImageIcon(image7));
    this.ACard2.setBounds(340, 50, 200, 100);
    add(this.ACard2);
    
    this.butt1 = new JButton("Hit");
    this.butt1.addActionListener(this);
    this.butt1.setBounds(50, 160, 70, 20);
    add(this.butt1);
    
    this.butt2 = new JButton("Reset");
    this.butt2.addActionListener(this);
    this.butt2.setBounds(190, 160, 70, 20);
    add(this.butt2);
    
    this.butt3 = new JButton("Stand");
    this.butt3.addActionListener(this);
    this.butt3.setBounds(120, 160, 70, 20);
    add(this.butt3);
    
    AIDrawCheck();
    CheckBust();
    AIDrawCheck();
    CheckBust();
    AIDrawCheck();
    CheckBust();
    
    repaint();
  }
  
  private void AIDrawCheck()
    throws IOException
  {
    if (!AIBust)
    {
      if (AIPointsTotal <= 12) {
        AIDraw();
      }
      if ((AIPointsTotal < 15) && (AIPointsTotal > 12))
      {
        int Chance = (int)Math.floor(Math.random() * 99.0D + 1.0D);
        if (Chance > 70) {
          AIDraw();
        }
      }
      if ((AIPointsTotal > 15) && (AIPointsTotal < 18))
      {
        int Chance = (int)Math.floor(Math.random() * 99.0D + 1.0D);
        if (Chance > 80) {
          AIDraw();
        }
      }
    }
  }
  
  private void AIDraw()
    throws IOException
  {
    if (toDraw2 == 2)
    {
      AICurrentCards[2] = DrawCard();
      File file10 = new File("IMG/Back.png");
      BufferedImage image10 = ImageIO.read(file10);
      this.ACard3 = new JLabel(new ImageIcon(image10));
      this.ACard3.setBounds(380, 50, 200, 100);
      add(this.ACard3);
    }
    if (toDraw2 == 3)
    {
      AICurrentCards[3] = DrawCard();
      File file8 = new File("IMG/Back.png");
      BufferedImage image8 = ImageIO.read(file8);
      this.ACard4 = new JLabel(new ImageIcon(image8));
      this.ACard4.setBounds(420, 50, 200, 100);
      add(this.ACard4);
    }
    if (toDraw2 == 4)
    {
      AICurrentCards[4] = DrawCard();
      File file9 = new File("IMG/Back.png");
      BufferedImage image9 = ImageIO.read(file9);
      this.ACard5 = new JLabel(new ImageIcon(image9));
      this.ACard5.setBounds(460, 50, 200, 100);
      add(this.ACard5);
    }
    repaint();
    toDraw2 += 1;
  }
  
  public void actionPerformed(ActionEvent e)
  {
    try
    {
      CheckBust();
    }
    catch (IOException ex)
    {
      Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
    }
    if ((e.getSource() == this.butt1) && (toDraw < 5)) {
      try
      {
        PCurrentCards[toDraw] = DrawCard();
        
        toDraw += 1;
        if (toDraw == 3)
        {
          File file3 = new File("IMG/" + PCurrentCards[2] + ".png");
          BufferedImage image3 = ImageIO.read(file3);
          this.PCard3 = new JLabel(new ImageIcon(image3));
          this.PCard3.setBounds(90, 50, 200, 100);
          add(this.PCard3);
          CheckBust();
          repaint();
        }
        else if (toDraw == 4)
        {
          File file4 = new File("IMG/" + PCurrentCards[3] + ".png");
          BufferedImage image4 = ImageIO.read(file4);
          this.PCard4 = new JLabel(new ImageIcon(image4));
          this.PCard4.setBounds(130, 50, 200, 100);
          add(this.PCard4);
          CheckBust();
          repaint();
        }
        else if (toDraw == 5)
        {
          File file5 = new File("IMG/" + PCurrentCards[4] + ".png");
          BufferedImage image5 = ImageIO.read(file5);
          this.PCard5 = new JLabel(new ImageIcon(image5));
          this.PCard5.setBounds(170, 50, 200, 100);
          add(this.PCard5);
          CheckBust();
          repaint();
        }
        repaint();
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (e.getSource() == this.butt2)
    {
      toDraw = 2;
      toDraw2 = 2;
      setEnabled(false);
      setVisible(false);
      try
      {
        ResetGame();
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (e.getSource() == this.butt3) {
      winner();
    }
  }
  
  private void winner()
  {
    if (((PPointsTotal > AIPointsTotal) && (!PBust)) || (AIBust == true)) {
      try
      {
        File file51 = new File("IMG/WINNER.png");
        BufferedImage image51 = ImageIO.read(file51);
        this.PCard50 = new JLabel(new ImageIcon(image51));
        this.PCard50.setBounds(100, -20, 200, 100);
        add(this.PCard50);
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (((PPointsTotal < AIPointsTotal) && (!AIBust)) || (PBust == true)) {
      try
      {
        File file51 = new File("IMG/WINNER.png");
        BufferedImage image51 = ImageIO.read(file51);
        this.PCard51 = new JLabel(new ImageIcon(image51));
        this.PCard51.setBounds(300, -20, 200, 100);
        add(this.PCard51);
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (PPointsTotal == AIPointsTotal) {
      try
      {
        File file53 = new File("IMG/DRAW.png");
        BufferedImage image53 = ImageIO.read(file53);
        this.PCard52 = new JLabel(new ImageIcon(image53));
        this.PCard52.setBounds(200, -20, 200, 100);
        add(this.PCard52);
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    repaint();
  }
}

 

 

Needs Update

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4965113
Share on other sites

Link to post
Share on other sites

package blackjackv1;

 
import java.awt.Container;
import java.awt.TextField;
import javax.swing.JFrame;
 
public class BlackJackApplet
  extends JFrame
{
  TextField PlayerCards;
  TextField AICards;
  String A;
  String P;
  
  public BlackJackApplet()
  {
    getContentPane().setLayout(null);
    this.PlayerCards = new TextField(this.P);
    add(this.PlayerCards);
    this.PlayerCards.setBounds(50, 50, 100, 100);
    
    this.AICards = new TextField(this.A);
    this.AICards.setBounds(150, 50, 100, 100);
    add(this.AICards);
  }
}

 

package blackjackv1;
 
import java.awt.Container;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class BlackJackV1
  extends JFrame
  implements ActionListener
{
  static boolean[] Avalible = new boolean[52];
  static String[] Cards = new String[52];
  static int randomcard;
  static int w = 0;
  static int[] PCurrentCards = new int[5];
  static int[] AICurrentCards = new int[5];
  private static final long serialVersionUID = 1L;
  static boolean PBust = false;
  static boolean AIBust = false;
  static boolean HasRun0 = false;
  static boolean HasRun1 = false;
  static boolean HasRun2 = false;
  static boolean HasRun3 = false;
  static boolean HasRun4 = false;
  static boolean HasRun02 = false;
  static boolean HasRun12 = false;
  static boolean HasRun22 = false;
  static boolean HasRun32 = false;
  static boolean HasRun42 = false;
  static int PPointsTotal;
  static int AIPointsTotal;
  static int toDraw = 2;
  static int toDraw2 = 2;
  TextField PlayerCards;
  TextField AICards;
  JButton butt1;
  JButton butt2;
  JButton butt3;
  JLabel AIBusted;
  JLabel PBusted;
  JLabel PCard1;
  JLabel PCard2;
  JLabel PCard3;
  JLabel PCard4;
  JLabel PCard5;
  JLabel ACard1;
  JLabel ACard2;
  JLabel ACard3;
  JLabel ACard4;
  JLabel ACard5;
  JLabel PCard50;
  JLabel PCard51;
  JLabel PCard52;
  
  public static void main(String[] args)
    throws IOException
  {}
  
  public static int DrawCard()
  {
    int Card = 1;
    Random();
    if (Avalible[randomcard] != 0)
    {
      Card = randomcard;
      Avalible[randomcard] = false;
    }
    else
    {
      w -= 1;
    }
    return Card;
  }
  
  private static void Random()
  {
    randomcard = (int)Math.floor(Math.random() * 51.0D + 1.0D);
  }
  
  private static void Reset()
    throws IOException
  {
    int i = 0;
    while (i < 50)
    {
      Avalible = true;
      i += 1;
    }
    StartOfMatch();
    PBust = false;
    AIBust = false;
    HasRun0 = false;
    HasRun1 = false;
    HasRun2 = false;
    HasRun3 = false;
    HasRun4 = false;
    HasRun02 = false;
    HasRun12 = false;
    HasRun22 = false;
    HasRun32 = false;
    HasRun42 = false;
  }
  
  private static void ResetGame()
    throws IOException
  {
    int i = 0;
    while (i < 50)
    {
      Avalible = true;
      i += 1;
    }
    StartOfMatch();
    PBust = false;
    AIBust = false;
    HasRun0 = false;
    HasRun1 = false;
    HasRun2 = false;
    HasRun3 = false;
    HasRun4 = false;
    HasRun02 = false;
    HasRun12 = false;
    HasRun22 = false;
    HasRun32 = false;
    HasRun42 = false;
  }
  
  public static void StartOfGame()
    throws IOException
  {
    int c = 0;
    while (c < 50)
    {
      Cards[c] = ("" + c);
      c += 1;
    }
    Reset();
  }
  
  public static void StartOfMatch()
    throws IOException
  {
    PCurrentCards[0] = DrawCard();
    PCurrentCards[1] = DrawCard();
    PCurrentCards[2] = 0;
    PCurrentCards[3] = 0;
    PCurrentCards[4] = 0;
    
    AICurrentCards[0] = DrawCard();
    AICurrentCards[1] = DrawCard();
    AICurrentCards[2] = 0;
    AICurrentCards[3] = 0;
    AICurrentCards[4] = 0;
    
    new BlackJackV1().dispose();
    new BlackJackV1().repaint();
  }
  
  private static String CardResolver(int a)
  {
    String[] names = { "nocard.png", "HA.png", "H2.png", "H3.png", "H4.png", "H5.png", "H6.png", "H7.png", "H8.png", "H9.png", "H10.png", "HJ.png", "HQ.png", "HK.png", "SA.png", "S2.png", "S3.png", "S4.png", "S5.png", "S6.png", "S7.png", "S8.png", "S9.png", "S10.png", "SJ.png", "SQ.png", "SK.png", "DA.png", "D2.png", "D3.png", "D4.png", "D5.png", "D6.png", "D7.png", "D8.png", "D9.png", "D10.png", "DJ.png", "DQ.png", "DK.png", "CA.png", "C2.png", "C3.png", "C4.png", "C5.png", "C6.png", "C7.png", "C8.png", "C9.png", "C10.png", "CJ.png", "CQ.png", "CK.png" };
    return names[a];
  }
  
  private void CheckBust()
    throws IOException
  {
    int[] number = { 0, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
    PPointsTotal = number[PCurrentCards[0]] + number[PCurrentCards[1]] + number[PCurrentCards[2]] + number[PCurrentCards[3]] + number[PCurrentCards[4]];
    AIPointsTotal = number[AICurrentCards[0]] + number[AICurrentCards[1]] + number[AICurrentCards[2]] + number[AICurrentCards[3]] + number[AICurrentCards[4]];
    if (PPointsTotal > 21)
    {
      if (((PCurrentCards[0] == 1) || (PCurrentCards[0] == 14) || (PCurrentCards[0] == 27) || (PCurrentCards[0] == 40)) && (!HasRun0))
      {
        PPointsTotal -= 10;
        HasRun0 = true;
      }
      if (((PCurrentCards[1] == 1) || (PCurrentCards[1] == 14) || (PCurrentCards[1] == 27) || (PCurrentCards[1] == 40)) && (!HasRun1))
      {
        PPointsTotal -= 10;
        HasRun1 = true;
      }
      if (((PCurrentCards[2] == 1) || (PCurrentCards[2] == 14) || (PCurrentCards[2] == 27) || (PCurrentCards[2] == 40)) && (!HasRun2))
      {
        PPointsTotal -= 10;
        HasRun2 = true;
      }
      if (((PCurrentCards[3] == 1) || (PCurrentCards[3] == 14) || (PCurrentCards[3] == 27) || (PCurrentCards[3] == 40)) && (!HasRun3))
      {
        PPointsTotal -= 10;
        HasRun3 = true;
      }
      if (((PCurrentCards[4] == 1) || (PCurrentCards[4] == 14) || (PCurrentCards[4] == 27) || (PCurrentCards[4] == 40)) && (!HasRun4))
      {
        PPointsTotal -= 10;
        HasRun4 = true;
      }
      if (PPointsTotal > 21)
      {
        File file1 = new File("IMG/BUST.png");
        BufferedImage image1 = ImageIO.read(file1);
        this.PBusted = new JLabel(new ImageIcon(image1));
        this.PBusted.setBounds(50, -20, 200, 100);
        add(this.PBusted);
        PBust = true;
        winner();
      }
    }
    if (AIPointsTotal > 21)
    {
      if (((AICurrentCards[0] == 1) || (AICurrentCards[0] == 14) || (AICurrentCards[0] == 27) || (AICurrentCards[0] == 40)) && (!HasRun02))
      {
        AIPointsTotal -= 10;
        HasRun02 = true;
      }
      if (((AICurrentCards[1] == 1) || (AICurrentCards[1] == 14) || (AICurrentCards[1] == 27) || (AICurrentCards[1] == 40)) && (!HasRun12))
      {
        AIPointsTotal -= 10;
        HasRun12 = true;
      }
      if (((AICurrentCards[2] == 1) || (AICurrentCards[2] == 14) || (AICurrentCards[2] == 27) || (AICurrentCards[2] == 40)) && (!HasRun22))
      {
        AIPointsTotal -= 10;
        HasRun22 = true;
      }
      if (((AICurrentCards[3] == 1) || (AICurrentCards[3] == 14) || (AICurrentCards[3] == 27) || (AICurrentCards[3] == 40)) && (!HasRun32))
      {
        AIPointsTotal -= 10;
        HasRun32 = true;
      }
      if (((AICurrentCards[4] == 1) || (AICurrentCards[4] == 14) || (AICurrentCards[4] == 27) || (AICurrentCards[4] == 40)) && (!HasRun42))
      {
        AIPointsTotal -= 10;
        HasRun42 = true;
      }
      if (AIPointsTotal > 21)
      {
        File file20 = new File("IMG/BUST.png");
        BufferedImage image20 = ImageIO.read(file20);
        this.AIBusted = new JLabel(new ImageIcon(image20));
        this.AIBusted.setBounds(340, -20, 200, 100);
        add(this.AIBusted);
        AIBust = true;
        winner();
      }
    }
  }
  
  private BlackJackV1()
    throws IOException
  {
    super("BlackJack-V1");
    setVisible(true);
    setSize(600, 250);
    setResizable(false);
    setDefaultCloseOperation(3);
    getContentPane().setLayout(null);
    
    File file1 = new File("IMG/" + PCurrentCards[0] + ".png");
    BufferedImage image1 = ImageIO.read(file1);
    this.PCard1 = new JLabel(new ImageIcon(image1));
    this.PCard1.setBounds(10, 50, 200, 100);
    add(this.PCard1);
    
    File file2 = new File("IMG/" + PCurrentCards[1] + ".png");
    BufferedImage image2 = ImageIO.read(file2);
    this.PCard2 = new JLabel(new ImageIcon(image2));
    this.PCard2.setBounds(50, 50, 200, 100);
    add(this.PCard2);
    
    File file6 = new File("IMG/Back.png");
    BufferedImage image6 = ImageIO.read(file6);
    this.ACard1 = new JLabel(new ImageIcon(image6));
    this.ACard1.setBounds(300, 50, 200, 100);
    add(this.ACard1);
    
    File file7 = new File("IMG/Back.png");
    BufferedImage image7 = ImageIO.read(file7);
    this.ACard2 = new JLabel(new ImageIcon(image7));
    this.ACard2.setBounds(340, 50, 200, 100);
    add(this.ACard2);
    
    this.butt1 = new JButton("Hit");
    this.butt1.addActionListener(this);
    this.butt1.setBounds(50, 160, 70, 20);
    add(this.butt1);
    
    this.butt2 = new JButton("Reset");
    this.butt2.addActionListener(this);
    this.butt2.setBounds(190, 160, 70, 20);
    add(this.butt2);
    
    this.butt3 = new JButton("Stand");
    this.butt3.addActionListener(this);
    this.butt3.setBounds(120, 160, 70, 20);
    add(this.butt3);
    
    AIDrawCheck();
    CheckBust();
    AIDrawCheck();
    CheckBust();
    AIDrawCheck();
    CheckBust();
    
    repaint();
  }
  
  private void AIDrawCheck()
    throws IOException
  {
    if (!AIBust)
    {
      if (AIPointsTotal <= 12) {
        AIDraw();
      }
      if ((AIPointsTotal < 15) && (AIPointsTotal > 12))
      {
        int Chance = (int)Math.floor(Math.random() * 99.0D + 1.0D);
        if (Chance > 70) {
          AIDraw();
        }
      }
      if ((AIPointsTotal > 15) && (AIPointsTotal < 18))
      {
        int Chance = (int)Math.floor(Math.random() * 99.0D + 1.0D);
        if (Chance > 80) {
          AIDraw();
        }
      }
    }
  }
  
  private void AIDraw()
    throws IOException
  {
    if (toDraw2 == 2)
    {
      AICurrentCards[2] = DrawCard();
      File file10 = new File("IMG/Back.png");
      BufferedImage image10 = ImageIO.read(file10);
      this.ACard3 = new JLabel(new ImageIcon(image10));
      this.ACard3.setBounds(380, 50, 200, 100);
      add(this.ACard3);
    }
    if (toDraw2 == 3)
    {
      AICurrentCards[3] = DrawCard();
      File file8 = new File("IMG/Back.png");
      BufferedImage image8 = ImageIO.read(file8);
      this.ACard4 = new JLabel(new ImageIcon(image8));
      this.ACard4.setBounds(420, 50, 200, 100);
      add(this.ACard4);
    }
    if (toDraw2 == 4)
    {
      AICurrentCards[4] = DrawCard();
      File file9 = new File("IMG/Back.png");
      BufferedImage image9 = ImageIO.read(file9);
      this.ACard5 = new JLabel(new ImageIcon(image9));
      this.ACard5.setBounds(460, 50, 200, 100);
      add(this.ACard5);
    }
    repaint();
    toDraw2 += 1;
  }
  
  public void actionPerformed(ActionEvent e)
  {
    try
    {
      CheckBust();
    }
    catch (IOException ex)
    {
      Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
    }
    if ((e.getSource() == this.butt1) && (toDraw < 5)) {
      try
      {
        PCurrentCards[toDraw] = DrawCard();
        
        toDraw += 1;
        if (toDraw == 3)
        {
          File file3 = new File("IMG/" + PCurrentCards[2] + ".png");
          BufferedImage image3 = ImageIO.read(file3);
          this.PCard3 = new JLabel(new ImageIcon(image3));
          this.PCard3.setBounds(90, 50, 200, 100);
          add(this.PCard3);
          CheckBust();
          repaint();
        }
        else if (toDraw == 4)
        {
          File file4 = new File("IMG/" + PCurrentCards[3] + ".png");
          BufferedImage image4 = ImageIO.read(file4);
          this.PCard4 = new JLabel(new ImageIcon(image4));
          this.PCard4.setBounds(130, 50, 200, 100);
          add(this.PCard4);
          CheckBust();
          repaint();
        }
        else if (toDraw == 5)
        {
          File file5 = new File("IMG/" + PCurrentCards[4] + ".png");
          BufferedImage image5 = ImageIO.read(file5);
          this.PCard5 = new JLabel(new ImageIcon(image5));
          this.PCard5.setBounds(170, 50, 200, 100);
          add(this.PCard5);
          CheckBust();
          repaint();
        }
        repaint();
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (e.getSource() == this.butt2)
    {
      toDraw = 2;
      toDraw2 = 2;
      setEnabled(false);
      setVisible(false);
      try
      {
        ResetGame();
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (e.getSource() == this.butt3) {
      winner();
    }
  }
  
  private void winner()
  {
    if (((PPointsTotal > AIPointsTotal) && (!PBust)) || (AIBust == true)) {
      try
      {
        File file51 = new File("IMG/WINNER.png");
        BufferedImage image51 = ImageIO.read(file51);
        this.PCard50 = new JLabel(new ImageIcon(image51));
        this.PCard50.setBounds(100, -20, 200, 100);
        add(this.PCard50);
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (((PPointsTotal < AIPointsTotal) && (!AIBust)) || (PBust == true)) {
      try
      {
        File file51 = new File("IMG/WINNER.png");
        BufferedImage image51 = ImageIO.read(file51);
        this.PCard51 = new JLabel(new ImageIcon(image51));
        this.PCard51.setBounds(300, -20, 200, 100);
        add(this.PCard51);
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    if (PPointsTotal == AIPointsTotal) {
      try
      {
        File file53 = new File("IMG/DRAW.png");
        BufferedImage image53 = ImageIO.read(file53);
        this.PCard52 = new JLabel(new ImageIcon(image53));
        this.PCard52.setBounds(200, -20, 200, 100);
        add(this.PCard52);
      }
      catch (IOException ex)
      {
        Logger.getLogger(BlackJackV1.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    repaint();
  }
}

 

 

 

 

That gives me ideas. Cant use any of it directly because thats swing and im using fx but the logic should transfer quite easily

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4966137
Share on other sites

Link to post
Share on other sites

That gives me ideas. Cant use any of it directly because thats swing and im using fx but the logic should transfer quite easily

Just so you know there is still a bug with the ace. When you need 11 it works and when you need it to be 1 it works but.. When you have 21 with it says bust. I think. The code is a bit old can't even remember what I did DX

Needs Update

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4966539
Share on other sites

Link to post
Share on other sites

Just so you know there is still a bug with the ace. When you need 11 it works and when you need it to be 1 it works but.. When you have 21 with it says bust. I think. The code is a bit old can't even remember what I did DX

 

No worries lol It would be too easy if you gave me the answer. I just was stuck in a wrut and no idea how to move forward 

Consume The Darkness

Link to comment
https://linustechtips.com/topic/365857-blackjack-using-javafx/#findComment-4966581
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×