lundi 6 mars 2017

Creating a discard pile for a card deck in java

So I only have one problem with my code and that is checking for the same card, because I do not want these cards to repeat. There is no reshuffling or whatsoever, its just random cards being dealt until there is none left. I have no clue on how to do it. Id appreciate some help; whether its theanswe or just a little nudge. package Card;

import java.util.Random;

public class deckOfCards {
    public String[] suite = { "Hearts", "Spade", "Diamonds", "Clubs" };
    public String[] faceValue = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
    public int deckCount = 0;
    private Card[] deck = new Card[52];
    public int index;

    public void buildDeck() {
        for (int i = 0; i < suite.length; i++) {
            for (int j = 0; j < faceValue.length; j++) {
                deck[deckCount] = new Card(suite[i], faceValue[j]);
                deckCount++;
            }
        }
    }
public Card shuffle(){
    Random rand = new Random();
    int index = rand.nextInt(52);
    return deck[index];
}

}

this creates the card and randomizes them package Card;

public class Card {

private String suite;
private String faceValue;

public Card(){

}

public Card(String suite, String faceValue){
this.setSuite(suite);
this.setFaceValue(faceValue); 
}
    public String getSuite(){
        return suite;
    }


//array list
//
public void setSuite(String suite){
    this.suite = suite;
}

public String getFaceValue(){
    return faceValue; 
}

public void setFaceValue(String faceValue){
    this.faceValue = faceValue;
}
}

package Card;

public class Driver {



public deckOfCards cards = new deckOfCards();
    private Card [] discard = new Card[52];

    public static void main(String[] args) {
        Driver driver = new Driver();
        driver.DealCards();

    }

    public void DealCards(){
        int cardsLeft = 52;
        cards.buildDeck();
        Card randomCard = new Card();
        for (int i = 0; i < 5; i++) {
            cardsLeft--;
            randomCard = cards.shuffle();

            System.out.println(randomCard.getFaceValue() + " of " + randomCard.getSuite());

        }
        System.out.println("Cards left:" + cardsLeft);
    }
}

this deals the cards. Again, any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire