So in my CS class we have to model playing cards with the classes hand, deck, an
ID: 3822767 • Letter: S
Question
So in my CS class we have to model playing cards with the classes hand, deck, and card. I got the card class done and the majority of the deck and hand class, however I'm getting a little lost on how to finish them up. The link to what my professor wants it to be like is https://cs.uwlax.edu/~jmaraist/120-spring-2017/2017/04/11/cs120-project-4/ . My code so far is:
public class Card {
public static final byte CLUB=0;
public static final byte DIAMOND=1;
public static final byte HEART=2;
public static final byte SPADE=3;
public static final int Jack = 11;
public static final int Queen = 12;
public static final int King = 13;
public static final int Ace = 14;
private final byte suit;
private final byte rank;
public Card(final byte suit, final byte rank) {
this.rank = rank;
this.suit = suit;
}
public byte getSuit() {
return suit;
}
public byte getRank() {
return rank;
}
public String suitAsString() { //returns the name for each suit
switch ( suit ) {
case SPADE: return "Spades";
case HEART: return "Hearts";
case DIAMOND: return "Diamonds";
case CLUB: return "Clubs";
default: return "";
}
}
public String rankAsString() { //returns the name for each rank
switch (rank){
case 1:
return "";
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 10:
return "Ten";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
case 14:
return "Ace";
default:
return "";
}
}
public String toString(){ //Method that returns the cards suit and rank in English
return rankAsString() + " of " + suitAsString();
}
}
import java.util.Random;
import java.util.ArrayList;
public class Deck {
private static ArrayList<Card> discarded; //ArrayList that stores the cards in the discard pile
private static ArrayList<Card> unused; //ArrayList that stores the remaining cards in the deck
private static ArrayList<Card> hand; //ArrayList that stores the number of hands
private static int cards; //Declares a private global variable "cards"
private static int hands; //Declares a private global variable "hands3"
public Deck() {
discarded = new ArrayList<>();
unused = new ArrayList<Card>();
hand = new ArrayList<>();
for(byte suit = 0; suit <= 3; suit++){ //Adds the unused cards to the ArrayList unused
for(byte rank = 2; rank <= 14 && rank >=2; rank++){
unused.add(new Card(suit, rank));
}
}
shuffle(); //Makes a call to the Method shuffle() to shuffle the deck(called unused in this case)
}
public void shuffle() { //Method that shuffles the deck of cards
for(int i = unused.size()-1; i > 0; i--){
int random = (int) (Math.random()*(i+1));
unused[random] = unused[i];
}
}
public Card[] getUnused() { //Method that gives the amount of cards in the deck
Card[] cards2 = new Card[unused.size()];
return unused.toArray(cards2);
}
public Card[] getDiscarded() { //Method that gives the amount of cards in the discard pile
Card[] cards1 = new Card[discarded.size()];
return discarded.toArray(cards1);
}
public void deal(final int hands, final int cards) {
this.hands = hands;
this.cards = cards;
//check if enough cards
/*hands.add(new Hand(Hand this, new ArrayList<Card>())); //have to do this h times
* for (i<c){
* for(j<h){
* hands.get(j).getCards().add(unused.get(unused.size()-1));
* unused.remove(unused.size()-1)
*/
for(int i = 0; i < cards; i++){
for(int j = 0; j < hands; j++){
hand.get(j).getCards().add(unused.get(unused.size()-1));
unused.remove(unused.size()-1);
}
}
}
public Hand getHand(Card card) {
}
public Hand[] getHands() {
Hand[] hands1 = new Hand[hand.size()];
return hands1 = hand.toArray(hands1); //puts the number hands into an ArrayList
}
}
import java.util.ArrayList;
public class Hand {
private static ArrayList<Card> handOfCards; //Declares a private global ArrayList
public static Deck deck; //Declares a public global Deck "deck"
public Hand(final Deck d, ArrayList<Card> handOfCards) {
this.handOfCards = handOfCards;
this.deck = deck;
}
public Deck getDeck(Deck deck) {
return deck;
}
// public Card[] getCards() {
// Card[] c = new Card[handOfCards.size()];
// return handOfCards.toArray(c);
// }
public void playCard(final Card card) { //Method takes one card from the hand put it in the discard pile
}
public ArrayList<Card> getCards() {
return handOfCards;
}
public void discardHand() { //Method takes all the cards in the hand and puts it in the discard pile
}
public static void main(String[] args) {
}
}
Explanation / Answer
I have added functions for Hand.java
import java.util.ArrayList;
public class Hand {
private static ArrayList<Card> handOfCards; //Declares a private global ArrayList
public static Deck deck; //Declares a public global Deck "deck"
public Hand(final Deck d, ArrayList<Card> handOfCards) {
this.handOfCards = handOfCards;
this.deck = deck;
}
public Deck getDeck(Deck deck) {
return deck;
}
// public Card[] getCards() {
// Card[] c = new Card[handOfCards.size()];
// return handOfCards.toArray(c);
// }
public void playCard(final Card card) { //Method takes one card from the hand put it in the discard pile
if (card == null)
throw new NullPointerException("Can't add a null card to a hand.");
handOfCards.add(card);
}
public ArrayList<Card> getCards() {
return handOfCards;
}
public void discardHand() { //Method takes all the cards in the hand and puts it in the discard pile
handOfCards.clear();
}
public static void main(String[] args) {
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.