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...
Code:
Code:
Code:
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[i] = 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[i];
this.deck[i] = 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;
}
}
}