Overclock.net banner
1 - 20 of 24 Posts

Drackeo

· Registered
Joined
·
30 Posts
Discussion starter · #1 ·
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:
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;
                }

        }
}
 
Just to point out... you are missing a "Stay" button. Also... is there 2 players?

btn2 is the button that will deal the card. Right now you are just writing out "Hit Me" to the console.

Code:

Code:
btn2.setOnAction((clicked) -> {System.out.println("Hit me");});
Instead of writing out "Hit Me", you need to make a function that will draw the card returned from deck.nextCard() Since you have a function called hasNext , you probably need to check to see if there is another card left in the deck before you draw the next card. As for how to draw the card... i dont remember... its been 11 years since i did GUI stuff in Java.
 
Discussion starter · #3 ·
Quote:
Originally Posted by Mrzev View Post

Just to point out... you are missing a "Stay" button. Also... is there 2 players?

btn2 is the button that will deal the card. Right now you are just writing out "Hit Me" to the console.

Code:

Code:
btn2.setOnAction((clicked) -> {System.out.println("Hit me");});
Instead of writing out "Hit Me", you need to make a function that will draw the card returned from deck.nextCard() Since you have a function called hasNext , you probably need to check to see if there is another card left in the deck before you draw the next card. As for how to draw the card... i dont remember... its been 11 years since i did GUI stuff in Java.
Yah that much i figured out. I dont know how to translate that into being shown onto the GUI. Like when i press btn2 it adds the card i draws into the hand. Also i dont know how to initialize the hand
 
You should create another class Hand() that contains all the logic for for player (s) and the dealer, simplest way is to create an empty array and append cards to it. I would also add methods for calculating the value of the hand (keep in min ace can be 1 or 11). I don't know Java ( and I also can't just give you the answer) so here is what a Hand() class might look like in Python. The general concepts should be basically the same. Have the hit button call hand.draw_card(). You could also create a initial_deal() method that calls draw_card() twice for each player and dealer.

Code:

Code:
class Hand():
    def __init__(self):
        self.hand = []

#deck.deal_card() returns and removes the first card from the deck after it's shuffled        
#basically pop function (if there is a Java equivalent)
    def draw_card(self, deck):
        self.hand.append(deck.deal_card())

    def value():
        hand_value = 0
        for card in self.hand:
            hand_value += card.value()
        if "Ace" in self.hand and hand_value < 12:
            hand_value += 10
        return hand_value
Also, you may want another method in the class to display the cards (as the dealer should keep one card hidden but the player(s) should show all cards)
 
Discussion starter · #5 ·
Quote:
Originally Posted by BBZZHH View Post

You should create another class Hand() that contains all the logic for for player (s) and the dealer, simplest way is to create an empty array and append cards to it. I would also add methods for calculating the value of the hand (keep in min ace can be 1 or 11). I don't know Java ( and I also can't just give you the answer) so here is what a Hand() class might look like in Python. The general concepts should be basically the same. Have the hit button call hand.draw_card(). You could also create a initial_deal() method that calls draw_card() twice for each player and dealer.

Code:

Code:
class Hand():
    def __init__(self):
        self.hand = []

#deck.deal_card() returns and removes the first card from the deck after it's shuffled        
#basically pop function (if there is a Java equivalent)
    def draw_card(self, deck):
        self.hand.append(deck.deal_card())

    def value():
        hand_value = 0
        for card in self.hand:
            hand_value += card.value()
        if "Ace" in self.hand and hand_value < 12:
            hand_value += 10
        return hand_value
Also, you may want another method in the class to display the cards (as the dealer should keep one card hidden but the player(s) should show all cards)
Hmmmm That gives me ideas. This wouldnt be too hard to convert the logic to JAVA. Considering Python was the first language i learned lol. I could link the button i have set to hit me is call upon my hand class. I have a simple one set up as a place holder right now,

Code:

Code:
package BlackJack;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Hand {

                ArrayList<Card> player = new ArrayList<>();
}
I could set up a loop just as if i was filling up a single dimension array. Have it add the first card in the shuffled deck each time as the hand.
 
Name that class Player to make things more understandable. When you create instances of Player, you can name them dealer and humanPlayer1. Player would have the attributes of his name, a list of cards in his hand, how much money he has, and with functions like hit and double down.
 
Discussion starter · #7 ·
Quote:
Originally Posted by Mrzev View Post

Name that class Player to make things more understandable. When you create instances of Player, you can name them dealer and humanPlayer1. Player would have the attributes of his name, a list of cards in his hand, how much money he has, and with functions like hit and double down.
Thankfully in the scope of the project we dont need to add a betting and money management element to the program. Nor do we need to include a dealer, tho that is extra credit. That would make this just so much harder then it already is for us all.
 
How are you structuring your deck? It seems that you will just iterate 1-52 five times.
If you keep it this way, you'll probably need to link each number with a specific card image, i.e. 1 = Ace of Hearts, 2 = 2 of Hearts, 14 = Ace of Spades,etc. If I was doing this in python, I would create a dictionary for this and use tkinter label widget to display the corresponding image.
 
Adding an AI for the dealer is very easy. For the human, you prompt for hit or stay. With the AI, you dont prompt, you just calculate if X <16 hit, else stay . The order the cards are delt would be a little harder, but if you create a function deal hand, you can statically deal 1 card to the player, 1 to the dealer, 1 to the player, 1 to the dealer. After that, then it loops on the player until if stay = true OR X = 21 OR Cards.Count = 5. Then its the dealer turn where you do the simple x<16 thing. The tricky part would be that you would need to add a parameter in the cards called hidden. So, when the cards are delt, when drawing, if hidden = true, use the blank card image. When it becomes the dealer turn, you can turn the hidden flag off and reveal the card. .... another option (easier) would be to deal the cards properly, but never fire the draw function for the cards. Once its the dealers turn, you run the draw function on the 2 cards.

In school, its always worth it to do the extra credit. ALWAYS... Things happen and you rarely get the chance to make up for it. It gives you more experience and in this case, prepares you more for the next semester of programming. Writing an essay on the industrial revolution is nice and all, but it doesnt do much if you only had to take 1 history course. With programming, you get more practice and experience with classes which is definitely good. I see this extra credit as something easy to do, and would probably take me 20 minutes, you maybe 1hr, but that 40 extra minutes is learning. When you take the next programming class, or even the next assignment, you will need to do that. So, if you spend the 1 hour now to learn it, it will pay itself back before the end of the year. If this was poker and you needed to create an AI for that.... That I would say is a bit much.

I still recommend the classes be called Player because that is what your representing. When you talk about your hand, the hand isn't making the decisions to hit or say, its the player. So just rename Hand to Player.

Additional Notes....
You create your deck with this.... Kinda lame that you dont keep track of suits, but thats ok. Your issue is... what gets placed into deck?

Code:

Code:
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;
                                }
The use of modulation is nice. But, you end up with 0-51 ..... whats a 34? Is that a 5?
Code:

Code:
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;
        int x = k%4 //Perhaps   k%DeckOfCards.NUMBER_OF_SUITS
        if(x > 10){//IF Jack, Queen, King, or Ace
                if(x > 13){ // An Ace.
                        x = 11;
                }
                else{  
                        x = 11;
                }
        }
        this.deck[i] = x;
}

You have this function....

Code:

Code:
public Card nextCard(){
       return new Card(this.deck[this.next_card++]);
}
Card is a type... where have you defined this Class? If you did create Card as a class, then it makes a bit more sense. You can also move the logic i used above to compensate for the suits into the constructor and be able to have an attribute for suit and value.

Code:

Code:
package BlackJack;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Hand {

                ArrayList<Card> player = new ArrayList<>();
}
The naming is a bit odd on this one. Why would "player" be an array of Cards?
Quote:
Originally Posted by BBZZHH View Post

You should create another class Hand() that contains all the logic for for player (s) and the dealer, simplest way is to create an empty array and append cards to it. I would also add methods for calculating the value of the hand (keep in min ace can be 1 or 11). I don't know Java ( and I also can't just give you the answer) so here is what a Hand() class might look like in Python. The general concepts should be basically the same. Have the hit button call hand.draw_card(). You could also create a initial_deal() method that calls draw_card() twice for each player and dealer.

Code:

Code:
class Hand():
    def __init__(self):
        self.hand = []

#deck.deal_card() returns and removes the first card from the deck after it's shuffled        
#basically pop function (if there is a Java equivalent)
    def draw_card(self, deck):
        self.hand.append(deck.deal_card())

    def value():
        hand_value = 0
        for card in self.hand:
            hand_value += card.value()
        if "Ace" in self.hand and hand_value < 12:
            hand_value += 10
        return hand_value
Also, you may want another method in the class to display the cards (as the dealer should keep one card hidden but the player(s) should show all cards)
What if you have 2 aces in your hand? Technically you can have 5 aces.

Code:

Code:
public class Hand {
        ArrayList<Card> cards = new ArrayList<>();
        int totalValue = 0;

        public void draw_card(Card card){
                cards.add(card); // I didnt bother to see how to append to a list in java.
                totalValue = calcTotalValue();
        }

        private int calcTotalValue (){
                int sum = 0;
        int numberOfAces = 0;

                for(x = 0; x < cards.length ; x++)
                {
                        if (cards[x] == 11){ //Ace
                                numberOfAces++;
                        }
                        sum+=x;
                }

                while (numberOfAces){
                        if(x > 21){
                                x-=10;
                                numberOfAces--;
                        }
                        if(x <= 21){
                                break;
                        }       
                }
                return sum;
    }
}
 
Discussion starter · #10 ·
Quote:
Originally Posted by Mrzev View Post

Adding an AI for the dealer is very easy. For the human, you prompt for hit or stay. With the AI, you dont prompt, you just calculate if X <16 hit, else stay . The order the cards are delt would be a little harder, but if you create a function deal hand, you can statically deal 1 card to the player, 1 to the dealer, 1 to the player, 1 to the dealer. After that, then it loops on the player until if stay = true OR X = 21 OR Cards.Count = 5. Then its the dealer turn where you do the simple x<16 thing. The tricky part would be that you would need to add a parameter in the cards called hidden. So, when the cards are delt, when drawing, if hidden = true, use the blank card image. When it becomes the dealer turn, you can turn the hidden flag off and reveal the card. .... another option (easier) would be to deal the cards properly, but never fire the draw function for the cards. Once its the dealers turn, you run the draw function on the 2 cards.

In school, its always worth it to do the extra credit. ALWAYS... Things happen and you rarely get the chance to make up for it. It gives you more experience and in this case, prepares you more for the next semester of programming. Writing an essay on the industrial revolution is nice and all, but it doesnt do much if you only had to take 1 history course. With programming, you get more practice and experience with classes which is definitely good. I see this extra credit as something easy to do, and would probably take me 20 minutes, you maybe 1hr, but that 40 extra minutes is learning. When you take the next programming class, or even the next assignment, you will need to do that. So, if you spend the 1 hour now to learn it, it will pay itself back before the end of the year. If this was poker and you needed to create an AI for that.... That I would say is a bit much.

I still recommend the classes be called Player because that is what your representing. When you talk about your hand, the hand isn't making the decisions to hit or say, its the player. So just rename Hand to Player.

Additional Notes....
You create your deck with this.... Kinda lame that you dont keep track of suits, but thats ok. Your issue is... what gets placed into deck?

Code:

Code:
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;
                                }
The use of modulation is nice. But, you end up with 0-51 ..... whats a 34? Is that a 5?
Code:

Code:
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;
        int x = k%4 //Perhaps   k%DeckOfCards.NUMBER_OF_SUITS
        if(x > 10){//IF Jack, Queen, King, or Ace
                if(x > 13){ // An Ace.
                        x = 11;
                }
                else{  
                        x = 11;
                }
        }
        this.deck[i] = x;
}

You have this function....

Code:

Code:
public Card nextCard(){
       return new Card(this.deck[this.next_card++]);
}
Card is a type... where have you defined this Class? If you did create Card as a class, then it makes a bit more sense. You can also move the logic i used above to compensate for the suits into the constructor and be able to have an attribute for suit and value.

Code:

Code:
package BlackJack;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Hand {

                ArrayList<Card> player = new ArrayList<>();
}
The naming is a bit odd on this one. Why would "player" be an array of Cards?
What if you have 2 aces in your hand? Technically you can have 5 aces.

Code:

Code:
public class Hand {
        ArrayList<Card> cards = new ArrayList<>();
        int totalValue = 0;

        public void draw_card(Card card){
                cards.add(card); // I didnt bother to see how to append to a list in java.
                totalValue = calcTotalValue();
        }

        private int calcTotalValue (){
                int sum = 0;
        int numberOfAces = 0;

                for(x = 0; x < cards.length ; x++)
                {
                        if (cards[x] == 11){ //Ace
                                numberOfAces++;
                        }
                        sum+=x;
                }

                while (numberOfAces){
                        if(x > 21){
                                x-=10;
                                numberOfAces--;
                        }
                        if(x <= 21){
                                break;
                        }       
                }
                return sum;
    }
}
I think i will rename hand to player because that does make a lot more sense now that i think of it. But the deck class i have already accounts for eveything i would need because the class of card is seperate from it. This would account for both the suit, i just need to assign each one to the .jpg to each card. but that can be done at the same time as making an array of 52 of them right? As simple as adding another if statement to the assignment array.
 
Quote:
Originally Posted by Drackeo View Post

I think i will rename hand to player because that does make a lot more sense now that i think of it. But the deck class i have already accounts for eveything i would need because the class of card is seperate from it. This would account for both the suit, i just need to assign each one to the .jpg to each card. but that can be done at the same time as making an array of 52 of them right? As simple as adding another if statement to the assignment array.
Yeah, you could do 1 image per card, or you can do 1 base card, write the value in the middle with text. K♥ A♠
 
Discussion starter · #12 ·
Thought i should post the seperated code.

Class: BlackJack
Code:

Code:
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.Image;
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{

                DeckOfCards deck = new DeckOfCards(5);

                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 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(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);
        }
}

Class: Deck
Code:

Code:
package Black_Jack;

public class DeckOfCards {

                private int[] deck;
                private 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;
                        }

                        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;
                }
        }

Class: Card
Code:

Code:
package Black_Jack;

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;
                }

                @Override
                public String toString(){
                        return  this.getRank() + " of " + this.getSuit();
                }
}

Class: Player
Code:

Code:
package Black_Jack;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Player {

                ArrayList<Card> player = new ArrayList<>();
}
 
Discussion starter · #13 ·
Quote:
Originally Posted by Mrzev View Post

Yeah, you could do 1 image per card, or you can do 1 base card, write the value in the middle with text. K♥ A♠
Im going to do one image per card because thats what we need to do with it in the project. And the professor gave us the image files we need to use.
 
+Yeah, I agree with Mrzev about renaming hand. It does make more sense that way.
+For the initial deal of 2 cards per player, it should be fine to just call the draw_card() method repeatedly. In real life, the dealer would typically shuffle once, then deal cards for the round without shuffling.
+I would create a card class, but it doesn't seem necessary if you don't need to keep track of things like suit or color

For Mrzev:
My thinking on dynamically changing the value of aces is:
+You can only ever have one ace valued at 11; if you count two aces as 11, you would bust
+Calculate the value of the hand forcing aces to have a value of 1
+If you have an ace and your hand value is less than 12, you would always count one ace as 11 (you've already added 1, 10 is 11-1)
I think this should work, seems to be the opposite of your method of first summing with ace =11

Additional:
+Don't know if its necessary, but you may want to check for blackjack on the initial deal
+Also, you may want to force the players to stand if his/her current hand>21 (i.e. already busted)
 
Discussion starter · #15 ·
Quote:
Originally Posted by BBZZHH View Post

+Yeah, I agree with Mrzev about renaming hand. It does make more sense that way.
+For the initial deal of 2 cards per player, it should be fine to just call the draw_card() method repeatedly. In real life, the dealer would typically shuffle once, then deal cards for the round without shuffling.
+I would create a card class, but it doesn't seem necessary if you don't need to keep track of things like suit or color

For Mrzev:
My thinking on dynamically changing the value of aces is:
+You can only ever have one ace valued at 11; if you count two aces as 11, you would bust
+Calculate the value of the hand forcing aces to have a value of 1
+If you have an ace and your hand value is less than 12, you would always count one ace as 11 (you've already added 1, 10 is 11-1)
I think this should work, seems to be the opposite of your method of first summing with ace =11

Additional:
+Don't know if its necessary, but you may want to check for blackjack on the initial deal
+Also, you may want to force the players to stand if his/her current hand>21 (i.e. already busted)
For the project we do need to keep track of the suit and things, but only at the beginning at initialization of the deck to assign each card to a image. See the the new code i just posted above!
smile.gif
Plus the use of a card class just made the deck class easier to write.
 
Discussion starter · #17 ·
Quote:
Originally Posted by BBZZHH View Post

+Yeah, I agree with Mrzev about renaming hand. It does make more sense that way.
+For the initial deal of 2 cards per player, it should be fine to just call the draw_card() method repeatedly. In real life, the dealer would typically shuffle once, then deal cards for the round without shuffling.
+I would create a card class, but it doesn't seem necessary if you don't need to keep track of things like suit or color

For Mrzev:
My thinking on dynamically changing the value of aces is:
+You can only ever have one ace valued at 11; if you count two aces as 11, you would bust
+Calculate the value of the hand forcing aces to have a value of 1
+If you have an ace and your hand value is less than 12, you would always count one ace as 11 (you've already added 1, 10 is 11-1)
I think this should work, seems to be the opposite of your method of first summing with ace =11

Additional:
+Don't know if its necessary, but you may want to check for blackjack on the initial deal
+Also, you may want to force the players to stand if his/her current hand>21 (i.e. already busted)
How do i assign each card a different immage. Like in the array card[1] = ace of spades
 
Quote:
Originally Posted by BBZZHH View Post

+Yeah, I agree with Mrzev about renaming hand. It does make more sense that way.
For Mrzev:
My thinking on dynamically changing the value of aces is:
+You can only ever have one ace valued at 11; if you count two aces as 11, you would bust
Good call, i didn't even think about that.
Quote:
Originally Posted by Drackeo View Post

How do i assign each card a different immage. Like in the array card[1] = ace of spades
Inside the card class, add a property

Code:

Code:
Image  image =  new Image(card+".png");
Display each card in their hand and place them into a preset ImageView location. Instead of doing ImageView1 , ImageView2 , just create an array of ImageViews, so

Code:

Code:
For ( x = 0 ; x < hand.length; x++)
{
    cardPlaceholders[x].setImage(image);
}
Not sure, but i think it would be appropriate to place the ImageView array inside the Player Class, because if you did have multiple players, they would all need their own.
 
Discussion starter · #19 ·
Quote:
Originally Posted by Mrzev View Post

Good call, i didn't even think about that.
Inside the card class, add a property

Code:

Code:
Image  image =  new Image(card+".png");
Display each card in their hand and place them into a preset ImageView location. Instead of doing ImageView1 , ImageView2 , just create an array of ImageViews, so

Code:

Code:
For ( x = 0 ; x < hand.length; x++)
{
    cardPlaceholders[x].setImage(image);
}
Not sure, but i think it would be appropriate to place the ImageView array inside the Player Class, because if you did have multiple players, they would all need their own.
What is the cardPlaceHolers in this code? I tryed placing it in my Deck Class and initiating it on start up.
Code:

Code:
package Black_Jack;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Deck {

                private int[] deck;
                private int next_card = 0;

                private String imageFileName = Card.getSuit() + "_" + Card.getRank() + ".png";
                private Image  image =  new Image(imageFileName);

                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 cardAssign(){

                        for(int i = 0; i < this.deck.length; i++){
                                deck[i].setImage(image);
                        }
                }

                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;
                }
        }

Its throwing an error at me at the point i try to set each card to an image. Idk why!?
 
It seems like you passing the argument image to the method setImage

Code:

Code:
deck[i].setImage(image)
but you haven't defined what image to use. I think you would need additional code that that converts "i" counter variable into correct image.
 
1 - 20 of 24 Posts