我想打印我的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;
}
}
正如你在我的甲板课上看到的那样,我有一个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 ) );
}
}
答案 0 :(得分: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()'检查尺寸。
更新:这里有一些代码可以避免使用卡中的数组和逃避枚举:
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)。
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
有两种方法可以做到这一点:静态和动态测试。静态更简单,更不容易出错,但它不能是为任何其他目的执行的操作,只是简单地验证程序的这一部分是否正常工作,然后继续执行。动态测试稍微复杂一些,但是您可以在需要时测试卡组(当用户以某种方式修改卡组并且您必须对其进行验证时)。
静态测试方法和最简单的方法是打印出卡座中的所有卡并手动验证。要做到这一点,您首先必须进行添加和更正。首先,您应该将tostring
课程中的Card
方法的签名修改为“toString()
”,不带参数。这将覆盖自动将对象转换为String的对象方法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
}
动态测试它有点复杂,但不是那么多。为了测试这个,您将如何手动完成?你会找到丢失的卡片或重复的卡片。测试这些东西的好方法是套装。但在使用集合之前,必须先覆盖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;
}
此外,you should look here for using enums。除了使代码更具可读性之外,它还允许您添加类(如类)之类的方法,添加toString()
方法。另请参阅jUnit以获取适合Java的测试库。
答案 3 :(得分:0)
你应该覆盖你的tostring方法,如:
@override
public String toString(Card card1){
.....
}
并且应该将arraylist名称传递给System.out.println()。不需要数组列表的类型。
希望这会有所帮助...