gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:01:16 24 4
gpt4 key购买 nike

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

  1. 我必须初始化Comparator比较器吗?如果是这样,我该怎么做?
  2. 上面的注释 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);
}
}

最佳答案

基本上,当您想要在相同类型的对象之间进行不同类型的比较时,您可以使用比较器。例如你有

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

您想要同时进行升序排序和降序排序。你所做的就是编写实现比较器的不同类:

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 的代码将决定使用哪种排序方法。

关于java - 如何在纸牌游戏中使用比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16636523/

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