0

有人可以请帮忙。我创建了一个卡片类和甲板类,但我只是不知道如何创建手类。

这是我下面的卡片类。

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;           
  }
}

这是我的甲板课

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;
  } 
}

如果你能帮我打我的手电话,我将不胜感激。

4

1 回答 1

0

创建类,其中将包含例如 ArrayList 手。

 public class Hand {

      private ArrayList<Card> hand
      .
      .

然后制作添加(绘制)卡片的方法:

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

您还需要检查特定类型的卡是否存在的方法:

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;
}

同样适用于西装。当然,您很可能需要其他方法,但我相信您可以自己解决。

于 2012-11-07T14:17:38.557 回答