0

我想打印我的 ArrayList FullDeckArray 以查看我的 Deck 是否包含所有 52 张卡片和值。

这是我下面的卡片和甲板课程

package blackjack;

/**
 *
 * @author mvisser
 */
public class Card 
{

   private int rank;
   private int suit;

   public String tostring(Card card1)
   {

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

正如您在我的 Deck Class 中看到的那样,我有一个 ArrayList FullDeckArray,我要做的就是将
它打印出来,看看带来了什么价值

public class Deck 
{
//     private Card[][] fullDeck = new Card[0][0];
     private Random shuffle = new Random();
     public ArrayList<Card> FullDeckArray = new ArrayList<Card>();

//     private int numberOfCards = 52;

   public Deck()
   {


    for (int rank = 1; rank <= 13; rank++) {

            for (int suit = 1; suit <= 4; suit++)
            {
               FullDeckArray.add(new Card(rank, suit));


            }
        }
   }        

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

    public Card DrawCard() {
        int cardPosition = shuffle.nextInt(FullDeckArray.size()+1);
        return FullDeckArray.remove(cardPosition);
    }

     public int TotalCards() {
            return FullDeckArray.size();
     }

     public void test() {
            System.out.println( ArrayList<Card>( FullDeckArray ) );
     }
}
4

4 回答 4

1

我会使用枚举,并且使用该enum.values()方法,您可以轻松地遍历枚举的所有值。

public class Card {
    public enum Rank { 
        ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING;
        public String toString() {
            switch(this) {
            case ACE: return "Ace";
            case TWO: return "Two";
            case THREE: return "Three";
            case FOUR: return "Four";
            case FIVE: return "Five";
            case SIX: return "Six";
            case SEVEN: return "Seven";
            case EIGHT: return "Eight";
            case NINE: return "Nine";
            case TEN: return "Ten";
            case JACK: return "Jack";
            case QUEEN: return "Queen";
            case KING: return "King";
            default: return "ERROR: no valid rank"; 
            }
        }
    }
    public enum Suit { 
        CLUBS, DIAMONDS, HEARTS, SPADES;
        public String toString() {
            switch(this) {
            case CLUBS: return "Clubs";
            case DIAMONDS: return "Diamonds";
            case HEARTS: return "Hearts";
            case SPADES: return "Spades";
            default: return "ERROR: no valid suit";
            }
        }
    }
    private Rank rank;
    private Suit suit;

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

    public String toString() {
        return rank.toString() + " of " + suit.toString();
    }

    public boolean equals(Object other) {
        if (!(other instanceof Card)) return false;
        Card card = (Card) other;
        if (card.rank == this.rank && card.suit == this.suit) return true;
        return false;
    }
}

在您的套牌课程中,您可以使用这个简单的循环添加所有卡片:

public void fill() {
    for (Rank rank : Card.Rank.values()) {
        for (Suit suit : Card.Suit.values()) {
            Card card = new Card(rank, suit)
            cards.add(card);
            System.out.println(card.toString());
        }
    }
}

你的甲板类的其余部分,你可以维护。

如果要检查仅添加一次的卡片,可以使用 HashSet,因为在集合中,对象只能出现一次(但您需要equals()方法):

HashSet<Card> set = new HashSet<Card>(cards);
cards = new ArrayList<Card>(set);

之后,您可以使用“cards.size()”检查大小。

更新:这里有一些代码用于规避 Card 中数组的使用和规避枚举:

public class Deck {
    private Random shuffle = new Random();
    public ArrayList<Card> fullDeck = new ArrayList<Card>();

    public Deck() {
        for (int rank = 1; rank <= 13; rank++) {
            for (int suit = 1; suit <= 4; suit++) {
                fullDeck.add(new Card(rank, suit));
            }
        }
    }

    public void print() {
        String deckOutput = "";
        for (Card card : fullDeck) {
            deckOutput += card.toString() + "\n";
        }
        System.out.println(deckOutput);
    }

    public static void main(String[] args) {
        Deck deck = new Deck();
        deck.print();
    }
}

对于 Card,使用这个:

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

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

    public String toString() {
        String Srank = "", Ssuit = "";
        switch(rank) {
            case 1: Srank = "Ace"; break;
            case 2: Srank = "Two"; break;
            case 3: Srank = "Three"; break;
            case 4: Srank = "Four"; break;
            case 5: Srank = "Five"; break;
            case 6: Srank = "Six"; break;
            case 7: Srank = "Seven"; break;
            case 8: Srank = "Eight"; break;
            case 9: Srank = "Nine"; break;
            case 10: Srank = "Ten"; break;
            case 11: Srank = "Jack"; break;
            case 12: Srank = "Queen"; break;
            case 13: Srank = "King"; break;
        }
        switch(suit) {
            case 1: Ssuit = "Clubs"; break;
            case 2: Ssuit = "Diamonds"; break;
            case 3: Ssuit = "Hearts"; break;
            case 4: Ssuit = "Spades"; break;
        }
        return Srank + " of " + Ssuit;
    }
}

要进行测试,您仍然可以使用上述 HashMap/ArrayList 方法(仅当您在 Card 中也实现 equals 方法时)并检查fulldeck.size()是否有 52 张卡片(由于 HashMap,这将完全不同)。

于 2012-11-05T14:04:02.153 回答
0

有两种方法可以做到这一点:静态和动态测试。静态更简单,更不容易出错,但它不能是为了任何其他目的而执行的操作,除了简单地验证程序的这部分是否正常工作,然后再继续其余部分。动态测试稍微复杂一些,但是您可以在需要时测试一个卡片组(当用户以某种方式修改卡片组并且您必须对其进行验证时)。

静态测试

静态测试方法和最简单的方法是打印出一副牌中的所有卡片并手动验证。为此,您首先必须进行添加和更正。首先,您应该将类​​中tostring方法的签名修改为不带参数的“”。这会覆盖自动将对象转换为字符串的对象方法。其次,你需要在你的类中重写:CardtoString()toString()toString()Deck

@Override
public void toString() {
    StringBuilder sb = new StringBuilder();
    for(Card card : FullDeckArray) {
        sb.append(card);  // calls Card class's toString() method automatically.
        sb.append('\n');  // newline character after each card
    }
    return sb.toString();
}

现在您所要做的就是使用 Deck 的 toString() 方法。

public void test() {
    System.out.println(this);  // calls Deck class's toString() method automatically
}

动态测试

动态测试它有点复杂,但没那么复杂。要对此进行测试,您将如何手动进行?你会寻找丢失的卡片或重复的卡片。测试这些东西的一个好方法是集合。但是在你可以使用 Sets 之前,你必须首先重写hashCode()equals()方法来重新定义 Card 类如何被认为是唯一的。仅当等级和花色都匹配时,一张牌才等于另一张牌。

因此,将这些添加到您的 Card 类中:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + rank;
    result = prime * result + suit;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Card other = (Card) obj;
    if (rank != other.rank)
        return false;
    if (suit != other.suit)
        return false;
    return true;
}

现在我们有了它,我们继续实际的逻辑。一个简单的测试会是这样的:

For each card in your deck
    If card is not in set
        Add card to set
    Else
        Flag not valid!  Duplicate card!

If set does not have exactly 52 cards
    Flag not valid!  Extra or missing cards!

因此,代码自然会变成:

  public boolean test() {
      boolean valid = true;
      Set<Card> cardSet = new HashSet<Card>();
      for(Card card : FullDeckArray) {
          if(!cardSet.contains(card)) {
              cardSet.add(card);
          } else {
              valid = false;
          }
      }
      if(cardSet.size() != 52) {
         valid = false;
      }
      return valid;
  }

此外,您应该在这里寻找使用 enums。除了使代码更具可读性之外,它还允许您像类一样添加方法,并且像类一样添加toString()方法。另请参阅jUnit以获得一个不错的 Java 测试库。

于 2012-11-05T13:09:20.373 回答
0

您应该覆盖您的 tostring 方法,例如:

@override
public String toString(Card card1){
.....
}

并且应该只将数组列表名称传递给 System.out.println()。不需要数组列表的类型。

希望这可以帮助...

于 2012-11-05T13:22:00.400 回答
0

你应该试试这个

public class DeckTest{
    public static void main(String []args){
        System.out.println(new Deck().FullDeckArray);
    }
}

你的Card课程应该像下面的代码

 public class Card 
    {

       private int rank;
       private int suit;


       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;    
       }
    }
于 2012-11-05T12:57:58.617 回答