用Java实现卡片组 [英] Implementing a Deck of Cards in Java

查看:123
本文介绍了用Java实现卡片组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个实验室(我们被允许寻求外界的帮助,所以我在这里经过多次努力)我们必须实施一副卡片。我们必须使用enum类创建num

So I have a lab (we are allowed to seek outside help on it, so here I am after lots of head scratching) where we have to implement a deck of cards. We have to use the enum class to create num

For Suits:

For Suits:

public enum Suits {
CLUBS, HEARTS, DIAMONDS, SPADES

}

对于数字:

public enum Numerals {
DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), 
TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14);

}

我的卡级很漂亮直截了当,但我不确定这两个代码块:

My card class is pretty straightforward, but I'm not sure about these two blocks of code:

    public int compareTo (Card aCard){
    if (aCard.aNumeral.equals(this.aNumeral) && aCard.aSuit.equals(this.aSuit)){
        return 0;
    }
    else {
        return -1;
    }
}

    public boolean equals (Card aCard){
    if (this.compareTo(aCard) == 0){
        return true;
    }
    else {
        return false;
    }
}

现在是棘手的部分......甲板。 ..

Now for the tricky part...the Deck...

所以我们必须使用Cloneable,Iterable和Comparator来实现这个套牌,所以这就是我到目前为止所做的事情,只是无法弄清楚要做什么。 / p>

So we have to implement the deck using Cloneable, Iterable and Comparator, so here is what I have so far and just can't figure out what to.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

public class Deck implements Cloneable, Iterable<Card>, Comparator<Card> {

private ArrayList<Card> cards;

public Deck (){
    for (Card c : cards){


    }

}

我很难将构造函数放在一起。我打算使用ArrayList来保存52个已排序的卡片(如你所见);但我们必须最终返回一个已排序的牌组。关于去哪儿的建议?

I'm struggling to even put together the constructor. I'm planning on using an ArrayList to essentially "hold" 52 sorted cards (as you can see); but we have to ultimately return a sorted deck. Any suggestions on where to go?

推荐答案

回答关于 compareTo : a.compareTo(b)如果 a 小于 b,则应返回负数,如果 a 大于 b ,则为正,如果它们相等则为0。此外,如果您要订购对象,则排序应遵循的规则之一是,如果 a.compareTo(b)< 0 ,然后 b.compareTo(a)> 0 。 (你不能同时 a 小于 b b 小于 a )。您的 compareTo ,只要卡片不相等就返回 -1 ,不遵循此规则。要解决这个问题,您需要决定订购。 排序甲板是什么样的?可能所有的俱乐部都在一起,其次是所有的钻石等,这意味着任何俱乐部卡将比任何钻石卡少。要正确地做到这一点,你需要首先比较套装,并且只有在套装相同的情况下比较等级:

To answer the question about compareTo: a.compareTo(b) should return something negative if a is less than b, positive if a is greater than b, and 0 if they're equal. Also, if you're ordering the objects, one of the rules the ordering should follow is that if a.compareTo(b) < 0, then b.compareTo(a) > 0. (You can't have both "a is less than b" and "b is less than a"). Your compareTo, which just returns -1 anytime the cards aren't equal, doesn't follow this rule. To fix this, you'll need to decide on the ordering. What does a "sorted deck" look like? Probably all the clubs are together, followed by all the diamonds, etc., which means that any club card will be less than any diamond card. To do this correctly, you'll need to compare the suits first, and the ranks only if the suits are equal:

public int compareTo (Card aCard){
    int suitCompare = this.aSuit.compareTo(aCard.aSuit);
    if (suitCompare != 0)  {
        return suitCompare;
    }
    return this.aNumeral.compareTo(aCard.aNumeral);
}

compareTo on每个枚举将返回< 0 0 ,或者大于0 。所以你可以比较套装,返回< 0 > 0 的值,然后比较如果诉讼相等则排名。这是编写任何需要检查多个数据的 compareTo 方法的一般方法。

The compareTo on each enum will return <0, 0, or >0. So you can compare the suits, return a value that is <0 or >0, and then compare the ranks if the suits are equal. This is the general approach for writing any compareTo method where multiple pieces of data need to be checked.

这篇关于用Java实现卡片组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆