如果下降有条件的话

时间:2016-04-17 12:52:03

标签: c#

您如何让我的if语句更有效率?基本上我想做的是检查扑克中的“手”是否“直”我想检查玩家是否有“直线”,“同花”及以上。示例:如果游戏牌是齐平的,我不需要检查直线,只需检查“冲洗”及以上。

public enum HandType
{
    Single = 1,
    Pair = 2,
    Triple = 3,
    Straight = 4,
    Flush = 5,
    FullHouse = 6,
    FourOfAKind = 7,
    StraightFlush = 8,
}

public List<CardData> GetBotHands(List<CardData> gameHand)
{
    List<CardData> playerHand = new List<CardData>();

    HandType handType = HandEvaluator.EvaluateHand(gameHand);

    if(handType < HandType.Straight)
    {
        return null;
    }

    if(handType == HandType.Straight)
    {
        playerHand = GetStraight(gameHand);
        if(playerHand != null)
        {
            return playerHand;
        }

        playerHand = GetFlush(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }

        playerHand = GetFullHouse(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }

        playerHand = GetFourKind(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }

        playerHand = GetStraightFlush(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }
    }
    else if(handType == HandType.Flush)
    {
        playerHand = GetFlush(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }

        playerHand = GetFullHouse(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }

        playerHand = GetFourKind(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }

        playerHand = GetStraightFlush(gameHand);
        if (playerHand != null)
        {
            return playerHand;
        }
    }
    else if(handType == HandType.FullHouse)
    ...

    return null;
}

通过跟随@Shiran Dror的解决方案,现在我有了这个代码。但有没有办法缩短它?

if(handType == HandType.Straight)
{
    return GetStraight(gameHand) ?? GetFlush(gameHand) ?? GetFullHouse(gameHand) ?? GetFourKind(gameHand) ?? GetStraightFlush(gameHand);
}
else if(handType == HandType.Flush)
{
    return GetFlush(gameHand) ?? GetFullHouse(gameHand) ?? GetFourKind(gameHand) ?? GetStraightFlush(gameHand);
}
else if(handType == HandType.FullHouse)
...

1 个答案:

答案 0 :(得分:0)

你可以用??像这样:

return GetStraight(gameHand) ?? GetFlush(gameHand) ... ?? GetStraightFlush(gameHand);

它将返回第一个无空值

编辑 - 缩短

switch (handType) {
  case HandType.Straight:
    return GetStraight(gameHand) ?? GetFlush(gameHand) ... ?? GetStraightFlush(gameHand);

  case HandType.Flush:
    return GetStraight(gameHand) ?? GetFlush(gameHand) ... ?? GetStraightFlush(gameHand);
...
  default: 
    return null;
}

如果你反复调用同一个函数,你可以添加一个新函数,这样你就不会重复代码了。例如:

private HandType RepetitiveCheck(gameHand) 
{
  return GetFlush(gameHand) ?? GetFullHouse(gameHand) ?? GetFourKind(gameHand) ?? GetStraightFlush(gameHand)
}

并且在main函数中使用它就像这样

switch (handType) {
  case HandType.Straight:
    return GetStraight(gameHand) ?? RepetitiveCheck(gameHand);

  case HandType.Flush:
    return RepetitiveCheck(gameHand);
...
  default: 
    return null;
}