簡體   English   中英

如何在紙牌游戲中使用比較器

[英]how to use Comparator in a card game

對於作業,我給出了以下代碼,方法為空白。 我一直在努力,但我仍然不明白mutator setComparer或Comparator比較器如何在這個程序中工作。 我在網上研究過如何使用Comparators,但這個想法仍然不清楚。 任何人都可以給我一些指導嗎?

  1. 我是否必須初始化比較器比較器? 如果是的話,我該怎么做?
  2. private int上面的注釋比較(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);
    }
}

當您想要在相同類型的對象之間進行不同類型的比較時,基本上您使用Comparator。 比如你有

List<Integer> list = new ArrayList<Integer>();

你想要升序排序和降序排序。 你要做的是寫入實現Comparator的不同類:

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

你的問題的答案:

1-這取決於你如何使用類PlayingCardHand。 從我看到你需要初始化它。

2-注釋意味着使用PlayingCardHand的代碼將決定使用哪種排序方法。

總有2種選擇

1.Your Class實現類似的接口,從而提供compareTo()方法的實現。

2.您可以通過創建自己的比較器類來創建自己的比較器,該比較器類實現比較器接口,從而提供compare()方法的實現。

在第一種情況下,您只需要調用Collections.sort(your_collection)或Arrays.sort(your_array),在第二種情況下調用Collections.sort(your_collection,your_comparator的對象)或Arrays.sort(you_array,集合的對象) )

使用II'nd Version總是更好,因為它沒有為您的類的所有集合定義默認排序行為。

有關更多詳細信息,請參閱Java中的Comparator和Comparable

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM