如何为BlackJack创建手形类java

问题描述:

有人可以请请帮忙。我创建了一个卡类和Deck类,但我不知道如何创建Hand类。如何为BlackJack创建手形类java

这是我的以下卡类。

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

如果你能帮助我与我的手呼唤我真的很感激它。

+0

我的逻辑不起作用。我没有尝试过,因为老实说我不知道​​该怎么做。我知道我必须创建一个比赛和一名球员,然后将卡分配给两个 – mvisser

+0

然后尝试并返回时,当你有。 – 2012-11-07 14:06:37

+0

我只需要知道什么逻辑应该在手类中。 – mvisser

创建类,其中将包含例如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; 
} 

同样的诉讼。 当然你很可能需要其他方法,但我相信你可以自己弄明白。

+0

谢谢你,我只是需要基本的逻辑来让我开始。我很感激。 – mvisser