Search code examples
javablackjack

I am new to programming and I am having trouble making Cards with Suits assigned to them for a BlackJack Game


I am making a BlackJack game but i got a little stuck on how to make a deck with proper suits... My first thought was to have something like int[] cards ={1, 2, etc etc etc}; but then I couldn't figure out how to implement suits with those cards

Now I know my code is far from finished, I still need to have hands and many other things but I would like to first learn how to make Cards and Suits before I move on

Basically my question is: How can I make a Deck of Cards with suits and use them for my Black Jack game Also if you explain to me how and why it works that would be great since I am trying to learn java programming.

MY CODE

package blackjack2;
import java.util.Scanner;
import java.util.Random;
public class blackjackattemp2 {
    final static String NL = System.getProperty("line.separator");
    static Scanner reader = new Scanner(System.in);
    static int playersValue = 0, dealersValue = 0, bet = 0, money = 0;
    static boolean win;
    static char hit;



    public static void main(String[] args) {
    System.out.println("How much money would you like to start with?: ");
    money = reader.nextInt();
    if(money < 0){
        System.out.print("You are out of money!" + NL + "You lose!");
    }

    }   
    public static void hit(char hit){
        while(hit == 'y'){

        }
    }
    static void testWin(boolean testWin){
        if(playersValue > 21 || dealersValue >= playersValue && dealersValue <= 21){
        win = false;
    }
        if(dealersValue > 21 || playersValue <= 21 && playersValue >= dealersValue){
            win = true;
        }


    }
    static void moneyCalc(){
        if(win == true){
            money = money + bet;
        }
        if(win == false){
            money = money - bet;
        }
    }
    static void deck(){

    }

    }

I was thinking of how i would add cards to a users hand and I came up with this, It worked in my first version of my Black Jack game but I don't think it will now...

System.out.println("You get a " + cards[0] + " and a " + cards[1]);
cardValue = cards[0] + cards[1];
System.out.println("Your total is " + cardValue);

I would appreciate it greatly if someone were to tell me how I could change my old code to work for the new game of which I am creating.


Solution

  • Take advantage of the object oriented nature of the language. A card in a deck is a self contained entity so you can replicate that by making a class to hold the attributes that you want (a card's value and suit).

    Here is an example of a Card class.

    public class Card {
        private int value;
        private String suit;
    
        public Card(int value, String suit) {
            this.value = value;
            this.suit = suit;
        }
    
        public int getValue() { return this.value; }
        public String getSuit() { return this.suit; }
    }
    

    You can use this to make a deck in your Blackjack program like this (Note: you could also make a Deck class as well which would allow you to make blackjack games with multiple decks and other advantages)

    public class Blackjack {
        private Card[] deck;
    
        public Blackjack() {
            deck = new Card[52]; // Makes an array which holds 52 Card objects
            deck[0] = new Card(2, "Hearts");
            deck[1] = new Card(3, "Hearts");
            deck[2] = new Card(4, "Hearts");
            deck[3] = new Card(5, "Hearts");
            // And so on
        }
    }
    

    Now as you can see this would result in a long list of you making each card individually. You can improve this by making your cards in a loop, but I will leave that task to you. This should get you started on your way though.

    Now once you have a deck of cards you can shuffle them around. There are many easy ways to do this. After the deck is shuffled you can deal them out 1 by 1.