简体   繁体   中英

Scoring my Dice Rolls using c# and Unity

Just trying wanting to be able to score my dice rolls following a rule set. The rules range from making a single 1 rolled worth 100 points and three 1's worth 400, straight flush worth some points and 6x1's worth 8000.

Here is the code.

I don't know how to make the code calculate the scores individually

int[] rollDice(int numDice)
{
    int[] diceRolls = new int[6];
    //int[] diceRolls = null;
    for (int i = 0; i < NumDice; i++)
    {
        diceRolls[i] = rollSingleDie(6);
    }

    return diceRolls;
}

//Score the dice rolls in the array
int scoreDiceRolls(int[] rolls)
{
    int score = 0;



    //TODO: Complete the scoring logic

  // for (int i = 0; i < NumDice; i++)
  // {
  //     if (rolls[i] == 2 && 4)
  //     {
  //         score += 10;
  //     }
  // }

 //  if (rolls[1] == 2)
 //  {
 //      score += 100;
 //  }

    //SIX ONES RULE ////////////////////////////////////////////////////// NEED HELP HERE.
    if (rolls[0] == 1)
    {   
        if (rolls[1] == 1)
        {
            if (rolls[2] == 1)
            {
                if (rolls[3] == 1)
                {
                    if (rolls[4] == 1)
                    {
                        if (rolls[5] == 1)
                        {
                            score += 8000;
                        }
                    }
                }
            }
        }
    }
    // ROLL A ONE RULE = 100 points
    for (int i = 0; i < NumSides; i++)
    {
        if (rolls[i] == 1)
    {
          score += 100;
    }

}

// int intToCheck = 1;

//foreach (int one in rolls)
//{
//    if (x.Equals(1))
//    {
//        score += 10;
//        
//    }
//}


   // if (rolls[)
 //for (int i = 0; i < 6; i++)
 //{
 //    if (rolls[i] == 1)
 //    {
 //        score += 10;
 //    }
 //}




    return score;
}

Note that when aggregating data in arrays (or any other enumerable) there are a number of helpful functions in System.Linq.Enumerable

if(rolls
    .Take(6) // Just to make sure we get six results
    .All(r => r == 1)) // compare each result to 1
    score += 8000;

Ok, bit of a messy question, but if you are looking for the number of times certain values are recieved I would do the following:

int[] DiceRolls(int noOfRolls, int noOfSides)
{
   int[] results = new int[noOfSides];
   for (int i = 0; i < noOfRolls; i++)
   {
      results[rollSingleDie(noOfSides)]++;
   }
   return results;
}

This would allow you to check the number of times a certain value has been rolled.

Edit: Just to clarify - for 6 1s you would need to do:

int[] rolls = DiceRolls(6, 6);
if (rolls[0] == 6)
{
   score += 8000;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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