Java 如何在纸牌游戏中使用比较器

Java 如何在纸牌游戏中使用比较器,java,Java,对于家庭作业,我得到了下面的代码,方法是空白的。我一直在研究它,但我仍然不理解mutator setComparer或Comparator comparer在这个程序中是如何工作的。我在网上研究了如何使用比较器,但这个想法还不清楚。谁能给我一些指导吗 我必须初始化比较器比较器吗?如果是,我该怎么做 上面的注释private int compare(PlayingCard one,PlayingCard two)是什么意思?(this.comparer=null) 谢谢 import java.u

对于家庭作业,我得到了下面的代码,方法是空白的。我一直在研究它,但我仍然不理解mutator setComparer或Comparator comparer在这个程序中是如何工作的。我在网上研究了如何使用比较器,但这个想法还不清楚。谁能给我一些指导吗

  • 我必须初始化比较器比较器吗?如果是,我该怎么做
  • 上面的注释private int compare(PlayingCard one,PlayingCard two)是什么意思?(this.comparer=null)
  • 谢谢

    import java.util.*;
    //Class to represent a "generic" hand of playing-cards
    public class PlayingCardHand {
    //Instance Variables
    
    private int cardsInCompleteHand;     //Maximum # cards in this hand
    private ArrayList<PlayingCard> hand; //A hand of Playing-Cards
    private Comparator comparer;         //Client-provided comparison of PlayingCards
    
    //Constructor
    //Appropriate when PlayingCard compareTo() is to be used to compare PlayingCards
    public PlayingCardHand(int handSize) {
        cardsInCompleteHand = handSize;
        hand = new ArrayList<PlayingCard>();
    
    }
    
    //Helper: Compare 2 PlayingCards
    // if this.comparer is null, comparison uses compareTo()
    //                           otherwise the Comparator is applied
    private int compare(PlayingCard one, PlayingCard two) {
    
        return 0;
    }
    
    //Accessor: return # of cards currently in this hand
    public int getNumberOfCards() {
        return cardsInCompleteHand;
    }
    
    public boolean isComplete() {
        if (hand.size() == cardsInCompleteHand) {
            return true;
        }
        return false;
    }
    
    //Accessor: return COPIES of the cards in this hand
    public PlayingCard[] getCards() {
        PlayingCard[] temp = new PlayingCard[hand.size()];
    
        for (int i = 0; i < hand.size(); i++)//ch
        {
    
            temp[i] = hand.get(i);
        }
        return temp;
    }
    
    //Mutator: allows a client to provide a comparison method for PlayingCards
    public void setComparer(Comparator comparer) {
    }
    
    //Mutator: Append a new card to this hand
    public void appendCard(PlayingCard card) {
        int counter = 0;
        PlayingCard.Suit su = card.getSuit();
        PlayingCard.Rank ra = card.getRank();
    
        PlayingCard temp3 = new PlayingCard(su, ra);
    
        //10 20 goes here 30 40 if insert 25
        for (int i = 0; i < hand.size(); i++) {
            PlayingCard temp4 = hand.get(i);
            PlayingCard.Suit sui = temp4.getSuit();
            PlayingCard.Rank ran = temp4.getRank();
            if (su.ordinal() <= sui.ordinal() && ra.ordinal() <= ran.ordinal()) {
                hand.add(i, temp3);
                counter++;
            }
    
        }
        while (counter == 0) {
            hand.add(temp3);
        }
    }
    
    import java.util.*;
    //类来表示扑克牌的“通用”牌
    公开课玩纸牌{
    //实例变量
    private int cardsInCompleteHand;//此手牌中最多有#张牌
    私人牌手;//一手扑克牌
    private Comparator comparer;//客户端提供的播放卡比较
    //建造师
    //播放卡片时适用。比较()用于比较播放的卡片
    公共播放名片(int handSize){
    cardsInCompleteHand=手持;
    hand=newarraylist();
    }
    //助手:比较2张游戏卡
    //如果this.comparer为null,则比较将使用compareTo()
    //否则将应用比较器
    私人整数比较(玩第一张牌,玩第二张牌){
    返回0;
    }
    //存取者:返回当前在该手牌中的卡片
    public int getNumberOfCards(){
    退卡完成手;
    }
    公共布尔值isComplete(){
    if(hand.size()==cardsInCompleteHand){
    返回true;
    }
    返回false;
    }
    //存取者:归还此手牌的副本
    公共播放卡[]获取卡(){
    PlayingCard[]临时=新的PlayingCard[hand.size()];
    对于(inti=0;iif(su.ordinal()基本上,当您希望在同一类型的对象之间进行不同类型的比较时,可以使用Comparator

    List<Integer> list = new ArrayList<Integer>();
    
    你的问题的答案是:

    1-这取决于你如何使用PlayingCardHand类。从我看来,你需要初始化它


    2-注释表示使用PlayingCardHand的代码将决定使用何种排序方法。

    始终有两个选项可用

    1.您的类实现了comparable接口,从而提供了compareTo()方法的实现

    2.通过创建自己的comparator类来创建自己的comparator,该类实现了comparator接口,因此提供了compare()方法的实现

    在第一种情况下,您只需调用集合.sort(您的集合)或Arrays.sort(您的集合),在第二种情况下调用集合.sort(您的集合,您的比较对象)或Arrays.sort(您的集合,您的集合的对象)

    最好使用II'nd版本,因为它没有为类的所有集合定义默认排序行为


    有关更多详细信息,请参考

    我尝试将标记作为作业,但我无法!您的程序似乎是部分编写的,
    public void setComparer(Comparator comparer){}
    未设置…您的任务是完成一个部分编写的程序吗?想一想,我需要比较Card类中的哪些变量以确定它们是否相等。如果它们是int,则使用“==”如果它们是字符串,则执行
    等于
    。是的,我的任务是完成setComparer mutator,但我不知道如何开始。@Algorithmist,
    家庭作业
    标签被列入黑名单是有原因的。请不要创建替代品。
    public class Ascending implements Comparator<Integer>{
    
       @Override
       public int compare(Integer o1, Integer o2) {
           return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
       }
    }
    public class Descending implements Comparator<Integer>{
    
       @Override
       public int compare(Integer o1, Integer o2) {
           return (o1<o2 ? -1 : (o1==o2 ? 0 : 1));
       }
    }
    
    Collections.sort(list, new Descending());