简体   繁体   English

三种功能-Yahtzee

[英]Three of a kind function - Yahtzee

I got back my school assignment where we were asked to create some functions for the Yahtzee game, one of them were the Three of a Kind function. 我回到学校的作业中,被要求为Yahtzee游戏创建一些功能,其中之一是“三种功能”。 This is the code I wrote and the comment I got was: "You have to be able to check all numbers up to nrOfDieValues." 这是我编写的代码,得到的注释是:“您必须能够检查直到nrOfDieValues的所有数字。” I'm not quite sure how to check up to x numbers.. I tried using two for loops but that did not end up too well as the saving to the array got messed up. 我不太确定如何检查最多x个数字。.我尝试使用两个for循环,但是结果保存得不够好,因为保存到数组中变得混乱。 Help would be appreciated ^_^ 帮助将不胜感激^ _ ^

int isThreeOfAKind(const int dieValues[], int nrOfDieValues)
{
    int threeOAK, i, x = 0;
    int tOAK[nrOfDievalues];

    for(i = 0; i < nrOfDieValues; i++)
    {
        switch (dieValues[i]
        {
        case 1:
            tOAK[0]++;
            break;
        case 2:
            tOAK[1]++;
            break;
        ....
        ....
        case 6:
            tOAK[5]++;
            break;
        }
    }

    for (i = 0; i < nrOfDieValues; i++)
    {
        if (tOAK[i] >= 3)
        {
           x = i+1;
           threeOAK = 1; 
        }

    }


    if(threeOAK)
    {
        return x;
    }
    else
    {
        return 0;
    }

}

Remove the switch() , all it does it map a value of dieValues[] to an index into tOAK[] , and that mapping is pretty basic: 除去switch()它的所有它的值映射dieValues[]的索引成tOAK[]和该映射是非常基本的:

for(i = 0; i < nrOfDieValues; i++)
{
  tOAK[dieValues[i]] += 1;
}

This works the same, but assumes all indexes are 0-based. 这是相同的,但是假定所有索引都基于0。

I think your problem is that the dice inputs are 1-6 while the arrays are 0-5, and that even after my comment you are still having the same problem with your loops. 我认为您的问题是骰子输入为1-6而数组为0-5,即使在我发表评论后,循环仍然遇到相同的问题。

Assuming that the values on the dice are 1 indexed (unlike in Unwind's answer), I've simplified the algorithm in your function. 假设骰子上的值被索引为1 (与Unwind的答案不同),我已经简化了函数中的算法。 Comments are embedded in the code: 注释嵌入在代码中:

#define MaxScore 6 //maximum score on one die is 6

int isThreeOfAKind(const int dieValues[], int nrOfDieValues)
{
    int i;
    int tOAK[MaxScore];

    memset(tOAK, 0, MaxScore);         //initialise to 0

    for(i = 0; i < nrOfDieValues; i++) //go through each of the inputs
    {
       tOAK[dieValues[i]-1]++;         //increment the count of the result
       if (tOAK[dieValues[i]-1]==3)
       {                               //if we have now seen 3 results 
            return dieValues[i];       //of that value, return it. we don't
       }                               //need to process the rest.
    }
   return 0;      //if we haven't found a three of a kind, return 0.
}

Edit: Ahah! 编辑:啊! Can't believe I missed this: 简直不敢错过:

int tOAK[nrOfDievalues];

This line is a likely culprit. 这条线很可能是罪魁祸首。 You are creating an array of the same size as your input. 您正在创建与输入大小相同的数组。 This will work fine for an input such as: {1,2,3,4,5,6,1,2,3} but will break on, for example: {6,5,4} . 这对于诸如{1,2,3,4,5,6,1,2,3}类的输入将是很好的,但对于诸如{6,5,4}类的输入则会中断。
What this array should be doing is counting the number of times each result appears, which means that you need the same number of cells as possible scores from one input. 此数组应该做的是计算每个结果出现的次数,这意味着您需要与一个输入相同的单元格数作为可能的分数。 So in your case, 6 . 因此,在您的情况下, 6

What your code was doing however was creating an array of the same size of the input, so when for example, you called isThreeOfAKind({6},1) as input, you created an array tOAK[1] . 但是,您的代码正在创建与输入大小相同的数组,因此,例如,当您调用isThreeOfAKind({6},1)作为输入时,就创建了一个数组tOAK[1]
Trying to reference tOAK[6] leads to out of bounds access leading to a crash. 尝试引用tOAK[6]会导致越界访问,从而导致崩溃。
I've fixed this in the code above. 我已经在上面的代码中修复了这个问题。

Note, you could change MaxScore to make the function work for other games too: eg for a hand of cards, set MaxScore to 13 (aces low) or 14 (aces high) and it will find a Three of a Kind in that hand too. 请注意,您可以更改MaxScore以使该功能也适用于其他游戏:例如,对于一手牌,将 MaxScore 设置 13 (王牌低)或 14 (王牌高),它也会在那只手中找到三类。

Another issue I can see in your code: seems to me you confuse the number of die values with the number of dice - I don't know how many dice are thrown in the game, so what are possible values of nrOfDieValues , but tOAK keeps counters for every die value, so it should be declared tOAK[6] (for ordinary cubic dice). 我在您的代码中看到的另一个问题:在我看来,您将骰子值的数量与骰子的数量相混淆-我不知道游戏中会掷多少骰子,因此nrOfDieValues可能值是nrOfDieValues ,但tOAK保持tOAK每个骰子值都会计数,因此应将其声明为tOAK[6] (对于普通立方骰子)。

By the way, did you remember to initialize the counters to zeros before you start counting...? 顺便说一句,您还记得开始计数之前将计数器初始化为零吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM