Question

Can someone please please help. I have created a card class and Deck class but I just dont know how to create the Hand class.

This is my Card class below.

package blackjack;

public class Card {
  private int rank;
  private int suit;

  @Override
  public String tostring() {
    String result = "";
    if (rank == 1) result = "Ace";
    if (rank == 2) result = "Two";
    if (rank == 3) result = "Three";
    if (rank == 4) result = "Four";
    if (rank == 5) result = "Five";
    if (rank == 6) result = "Six";
    if (rank == 7) result = "Seven";
    if (rank == 8) result = "Eight";
    if (rank == 9) result = "Nine";
    if (rank == 10) result = "Ten";
    if (rank == 11) result = "Jack";
    if (rank == 12) result = "Queen";
    if (rank == 13) result = "King";

    if (suit == 1) result = result + " of Clubs ";
    if (suit == 2) result = result + " of Diamonds ";
    if (suit == 3) result = result + " of Hearts ";
    if (suit == 4) result = result + " of Spades ";

    return result;
  }

  public Card(int rank, int suit) {
        this.rank = rank;
        this.suit = suit;           
  }
}

This is my Deck Class

package blackjack;    

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Deck {
  private Random shuffles = new Random();
  public ArrayList<Card> Deck = new ArrayList<Card>();
  Random rand = new Random();

  // private int numberOfCards = 52;

  public Deck() {
    for (int ranks = 1; ranks <= 13; ranks++) {
      for (int suits =1 ; suits <= 4; suits++) {
         Deck.add(new Card(ranks, suits));
         //System.out.println(Deck.get(ranks) + "" +Deck.get(suits)); 
      }
    }
    shuffle();

    for (int i = 1; i < Deck.size(); i++) {
      //int cardPosition2 = shuffles.nextInt(52); 
      //shuffle.nextInt(Deck.get(i);
      System.out.println(Deck.get(i)); 
      //System.out.println(cardPosition2);
      //i++;
    }
  }

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

  public Card DrawCard() {
    int cardPosition = shuffles.nextInt(Deck.size());
    return Deck.remove(cardPosition);
  }

  public int TotalCardsLeft() {
    return Deck.size();
  }

  public Card dealCard() {
    // Deals one card from the deck and returns it.
    if (Deck.size() == 52) {
      shuffle();
    }
    Card temp;
    temp = Deck.get(0);
    Deck.remove(0);
    return temp;
  }

  public Card getCard(int i) {
    return Deck.get(i);
  }

  public Card remove(int i) {
    Card remo = Deck.get(i);
    Deck.remove(i);
    return remo;
  } 
}

If you can help me with my Hand call I would really appreciate it.

Was it helpful?

Solution

Create Class, which will contain for example ArrayList hand.

 public class Hand {

      private ArrayList<Card> hand
      .
      .

Then make methods to add(draw) Card :

public void addCard(Card c){
this.hand.add(c);
}

You will also need method to check whether card of specific type is present:

public boolean checkPresence(int rank){
for(int i=0;i<hand.size();i++){
//note you will need to implement getters to the Card class first
if (hand.get(i).getRank==rank)
return true;
}
 return false;
}

Similarly for suit. Of course you will most probably need other methods, but I am sure you can figure them out yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top