gpt4 book ai didi

java - 如何用 Java 测试我的 Deck 类

转载 作者:行者123 更新时间:2023-12-01 15:03:58 24 4
gpt4 key购买 nike

我想打印我的 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 类中看到的,我有一个 ArrayList FullDeckArray,我想做的就是
打印出来看看能带来什么值(value)

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

最佳答案

我会使用枚举,并且使用 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();
}
}

对于卡,请使用:

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 的原因,这一切都会不同)。

关于java - 如何用 Java 测试我的 Deck 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13232531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com