Jump to content
Hey many of you have helped me with my other project the Black Jack game. This is an extension of that post. Im looking for a way to assign images to each card in a deck. 

 

In the code a deck goes from 0-51. 

0 = Ace of Spades

51 = King of Clubs

 

My images fallow the naming of:  "Suit"_"Rank".png

 

I know i need to do this in a loop as if i was stacking and initializing an array. In fact to create the deck i do just this. 

 

BlackJack




 
package Black_Jack;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
 
public class BlackJack extends Application {
 
public void start(Stage primaryStage) throws Exception{
 
Deck deck = new Deck(5);
 
HBox text = new HBox();
Label label1 = new Label("Let's play some black jack!");
text.getChildren().addAll(label1);
 
HBox cards = new HBox(); //this is just how im checking if everything works
        ImageView imgView = new ImageView("cards/Spade_3.png");
        ImageView imgView0 = new ImageView("cards/b1fv.png");
        ImageView imgView1 = new ImageView("cards/b1fv.png");
        ImageView imgView2 = new ImageView("cards/b1fv.png");
        ImageView imgView3 = new ImageView("cards/b1fv.png");
        cards.getChildren().addAll(imgView, imgView0, imgView1, imgView2,imgView3);
 
//HBox hand = new HBox();
 
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.setCenter(hand);
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);
}
}
 



 

Deck




 
package Black_Jack;
 
public class Deck {
 
private int[] deck;
private int next_card = 0;
 
public static final int SIZE_OF_DECK = 52;
 
public Deck(){
this(1);
}
 
public Deck(int num_decks){
this.deck  = new int[num_decks*Deck.SIZE_OF_DECK];
System.out.println(num_decks);
for(int i = 0 ; i < this.deck.length; i++){
int k = i%Deck.SIZE_OF_DECK;
this.deck[i] = k;
//System.out.println(this.deck[i]);
}
}
 
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;
System.out.println(this.deck[i]);
}
 
System.out.println("Shuffled");
this.next_card = 0;
}
 
public Card nextCard(){
return new Card(this.deck[this.next_card++]);
}
 
public boolean hasNext(){
return this.next_card<this.deck.length;
}
}
 



 

Card




 
package Black_Jack;
 
public class Card {
 
private static final String[] suits = {"Club", "Diamond","Heart", "Spade"};
private static final String[] ranks = {"0","1","2","3","4","5","6","7","8","9","10","11", "12"};
private int card;
 
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();
     }
 
}
 



 

Player




 
package Black_Jack;
 
import java.util.Scanner;
import java.util.ArrayList;
 
public class Player {
 
ArrayList<Card> player = new ArrayList<>();
}
 



Consume The Darkness

Link to comment
https://linustechtips.com/topic/366858-image-array/
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

×