根据是否只有一个或多个卡,我如何评估卡的不同?

问题描述:

我一直在做这个纸牌游戏的作业,并打了一堵墙。根据是否只有一个或多个卡,我如何评估卡的不同?

我正在努力为Score()方法尝试评估我所有的卡片。我有一个foreach循环与switch

问题是Ace。 我想说如果超过1王牌,重视他们都为1,否则11

public int Score() 
{ 
    int score = 0; 

    foreach (Card c in hand) 
    { 
     switch (c.Value) 
     { 
     // Count all face cards as 10 
     case 'A': 
      score += 1; 
      break; 
     case 'T': 
     case 'J': 
     case 'K': 
     case 'Q': 
      score += 10; 
      break; 
     default: 
      score += (c.Value - '0'); 
      break; 
     } 

     //if (score > 21) 
     // switch (c.Value) 
     // { 
     //  case 'A': 
     //   score += 11; 
     //   break; 
     // } 
    } 
    return score; 
} 

我注释掉一段我与玩弄,但我不能换我的头周围试图码'如果不止一个王牌,值为1,否则为'11'

+0

酒杯适当的解释是这样的:所有的尖子算一个。如果手至少包含一个ace并且其总数小于12,则添加10.就是这样。 –

您可能需要的王牌到的不仅仅是“一个以上的王牌”以外的其他情形都算作1。如果用户有一个Jack,Three,Ace,你希望Ace计为1.我将所有不属于A的牌加入。然后取多少个A减1,并将总数加上。最后,请检查您一共是< 11,可以使王牌计数为11,否则,你要指望它为1

public int Score() 
{ 
    var score = 0; 
    var aceCount = 0; 

    foreach (Card c in hand) 
    { 
     switch (c.Value) 
     { 
      case 'A': 
       aceCount++; 
       break; 
      case 'T': 
      case 'J': 
      case 'K': 
      case 'Q': 
       score += 10; 
       break; 
      default: 
       score += (c.Value - '0'); 
       break; 
     } 
    } 

    if(aceCount == 0) 
    { 
     return score; 
    } 

    //Any ace other than the first will only be worth one point. 
    //If there is only one ace, no score will be added here. 
    score += (aceCount-1); 

    //Now add the correct value for the last Ace. 
    if(score < 11) 
    { 
     score += 11; 
    } 
    else 
    { 
     score++; 
    } 

    return score; 
} 

我会拿一个单独的变量来计算ace并在最后评估它。如果1个ace现在添加11,否则添加1 * numberOfAces。

将它添加到foreach循环之外的分数旁边。因此,在“A”情况下的评分评估应在循环结束后进行,并且您的A计数。

我建议在你的开关盒中和之后添加更多的逻辑。例如,您可以使用一个计数器来跟踪手中有多少个王牌。例如:

public int Score() 
{ 
    int score = 0; 
    int amountOfAces = 0; 


    foreach (Card c in hand) 
    { 
     switch (c.Value) 
     { 
       // Count all face cards as 10 
      case 'A': 
       amountOfAces++;// increment aces by 1 
       score += 11; 
       break; 
      case 'T': 
      case 'J': 
      case 'K': 
      case 'Q': 
       score += 10; 
       break; 
      default: 
       score += (c.Value - '0'); 
       break; 
     } 

     // Then adjust score if needed 
    if(amountOfAces>1){ 
     //since we know how many were found. 
     score = score-amountOfAces*11; 
     score = score+amountOfAces; 
     } 

    } 
    return score; 
} 

我可以想到的一种方法是为ace添加一个计数器。实际上,在case A:是:

case 'A': 
    score+=1; 
    ctrA++; 
    break; 

而且switch外:

if(ctrA == 1) //only one ace 
    score+= 10; //add 10 to make the score for that ace 11.