我如何根据只有一张或多于一张来对卡片进行不同的估价?
How do I value card differently depending on whether there's just one or more than one?
我一直在做这个纸牌游戏做作业,结果碰壁了。
我正在使用 Score()
方法尝试评估我所有的卡片。我有一个带有 switch
.
的 foreach
循环
问题是 Ace。
我想说的是,如果超过 1 个 A,则将它们全部赋值为 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;
}
我注释掉了我正在玩的部分,但我无法全神贯注地尝试编写 'if more than one ace, value as 1, else 11'
我会用一个单独的变量来计算 A 并在最后对其进行评估。如果出现 1 个 ace,则加 11,否则加 1 * numberOfAces。
将它添加到分数旁边,在 foreach 循环之外。因此,案例 "A" 的分数评估应该在循环完成并且您有 ace 计数后进行。
我建议在您的 switch case 中和之后添加更多逻辑。例如,您可以使用计数器来跟踪手中有多少 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;
}
我能想到的方法之一是为 A 添加一个计数器。实际上,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.
除了 "more than one ace",您可能希望 A 在其他情况下算作 1。如果用户有 Jack、Three、Ace,您希望 A 算作 1。我会把所有不是 A 的牌加起来。然后取多少 A 减 1 并将该计数加到总数中。最后,检查你的总点数是否<11,你可以把A记为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;
}
我一直在做这个纸牌游戏做作业,结果碰壁了。
我正在使用 Score()
方法尝试评估我所有的卡片。我有一个带有 switch
.
foreach
循环
问题是 Ace。 我想说的是,如果超过 1 个 A,则将它们全部赋值为 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;
}
我注释掉了我正在玩的部分,但我无法全神贯注地尝试编写 'if more than one ace, value as 1, else 11'
我会用一个单独的变量来计算 A 并在最后对其进行评估。如果出现 1 个 ace,则加 11,否则加 1 * numberOfAces。
将它添加到分数旁边,在 foreach 循环之外。因此,案例 "A" 的分数评估应该在循环完成并且您有 ace 计数后进行。
我建议在您的 switch case 中和之后添加更多逻辑。例如,您可以使用计数器来跟踪手中有多少 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;
}
我能想到的方法之一是为 A 添加一个计数器。实际上,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.
除了 "more than one ace",您可能希望 A 在其他情况下算作 1。如果用户有 Jack、Three、Ace,您希望 A 算作 1。我会把所有不是 A 的牌加起来。然后取多少 A 减 1 并将该计数加到总数中。最后,检查你的总点数是否<11,你可以把A记为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;
}