haxe - 枚举与严格秩序的比较

时间:2014-03-19 20:21:18

标签: enums compare haxe

我有一个枚举类型:

enum HandRank {
    HighCard;
    OnePair;
    Set;
    TwoPair;
    Straight;
    Flush;
    FullHouse;
    Quads;
    StraightFlush;
    RoyalFlush;
}

我想比较一对值来选择更好的值。类似的东西:

maxRank = <...some method...>(firstRank, secondRank) ? firstRank : secondRank;

在这里,我认为枚举的所有值都遵循可预测的升序。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:4)

你可以这样做:

maxRank = Type.enumIndex(firstRank) > Type.enumIndex(secondRank) ? firstRank : secondRank;

答案 1 :(得分:1)

我会写一个这样的最大函数:

class HandRanks {
    public static function toInt (a:HandRank) return switch a {
        case HighCard: 1;
        case OnePair: 2;
        case Set: 3;
        case TwoPair: 4;
        case Straight: 5;
        case Flush: 6;
        case FullHouse: 7;
        case Quads: 8;
        case StraightFlush: 9;
        case RoyalFlush: 10;
    }
    public static function max (a:HandRank, b:HandRank) {
        var a1 = toInt(a);
        var b1 = toInt(b);
        return if (a1 > b1) a else b;
    }
}

// usage
using HandRanks;
HighCard.max(Set); // Set