r/learnjava • • Jan 16 '20

IM STUCK CAN SOMEONE HEP ME?

Iv been trying to develop a black jack game,in hope that i will learn along side of developing it,I just want it to be a console game thats it . I have cvs class where that handles the file that i send (Deck file) as well as my main method so i wont be posting thoes two . My problems is after i send my parametars to constructor of a deck class i have this method called drawOneCard where i send data fileds to a constructor of a dealer class where i store it into Hand arraylist but the parametars arent going trought,where am i mistakin here?
CLASS DECK

private int cardValue;

private String cardRank;

private String suitRank;

static ArrayList<Deck> deck = new ArrayList<Deck>();

Deck(){
}

public Deck(int cardValue,String cardRank,String suitRank) {

this.cardValue = cardValue;

this.cardRank = cardRank;

this.suitRank = suitRank;

deck.add(this);

}

public static void drawOneCard() {

String drawnCard;

int drawnCardValue;

drawnCard = deck.get(51).getCardSuit() + deck.get(51).getCardRank();

drawnCardValue = deck.get(51).getCardValue();

new Dealer (drawnCard,drawnCardValue);

Collections.swap(deck, 51, 0);

Collections.swap(deck, 51, 50);

}

public static void shuffle() {
Collections.shuffle(Deck.deck);
}

public int getCardValue() {

return cardValue;

}

public String getCardRank() {

return cardRank;

}

public String getCardSuit() {

return suitRank;

}

}

CLASS DEALER

String cards;

int values;

static ArrayList<Dealer> Hand = new ArrayList<Dealer>();

Dealer(){

}

Dealer(String cards,int values){

this.cards = cards;

this.values = values;

Hand.add(this);

}

public static void deal() {
Deck.shuffle();

for(Dealer arrayItem : Hand)
{

System.out.println(arrayItem);

}

}

int getCardValue() {
return values;
}
String getCard() {
return cards;
}

}

0 Upvotes

4 comments sorted by

3

u/Dhalias_ Jan 16 '20

WHY ARE YOU YELLING ??

Pls post the actual error message :)

-1

u/trofix99 Jan 16 '20

Im not im just grabbing ur attention with big headline :) I dont have a error message :) My question is simple why is my arraylist called Hand empty

1

u/Dhalias_ Jan 16 '20

public static void deal() {Deck.shuffle();

for(Dealer arrayItem : Hand){

System.out.println(arrayItem);

}

}

Deck.shuffle(); You're calling the static method without giving it parameters

public static void shuffle() {Collections.shuffle(Deck.deck);}

Then, Collections.shuffle(Deck.deck); Is using a static method again using a deck that is most likely empty since Deck.deck doesn't refer to an actual object you have created.

for(Dealer arrayItem : Hand){

System.out.println(arrayItem);

}

Then you're printing the Object memory string since you're not using an override .toString() or another method.

Why is it empty ? The logic of the project seems wrong to me, you're creating new ArrayList everywhere and I have difficulties understanding why. It's most likely empty since you're System.out.println(arrayItem); the content of Hand, but Hand is only known inside of one Dealer object, it does not have access to other object. IMO, you should have ONE arraylist which contains 1 Dealer object and many players objects (Every player has an arraylist that contains many Card objects). Since in java array are not cloned when giving in parameter but only giving a memory reference you should use this to simplify modifications. It's hard to explain but if you need clarifications, I will !

1

u/Dhalias_ Jan 16 '20

Don't know if this graph can help you