掷骰子会产生更多的掷骰子吗?

时间:2020-05-11 08:54:15

标签: c#

我正在做一项作业,我必须做一个可以扔1-4个骰子的应用程序。然后将结果添加到列表中。 但是,如果其中任何一个是 6 ,则不会在列表中添加一个 ,而是抛出 2个额外的骰子

只要骰子返回 6 ,就必须再掷2个骰子,直到没有骰子返回6。

这里有人知道如何解决吗?我的编程技能是非常基础的,从去年开始我就没有使用它们。

for (int i = 0; i < qty; i++)
{
  int diceNr = RollDice(random);
  dicelist.Add(diceNr);
  Console.WriteLine(diceNr);
  if (diceNr == 6)
{
dicelist.Remove(diceNr);
Console.WriteLine("You got a six, that means you get 2 extra throws!");

   for (int x = 0; x < 2; x++)
   {
       diceNr = RollDice(random);
       dicelist.Add(diceNr);
   }

3 个答案:

答案 0 :(得分:1)

您可以使用while loop,一旦出局就退出。如果滚动六,则将剩余数量再增加两个,否则将滚动值添加到列表中。

int qty = 6;

// decrement remaining quantity each iteration 
// until zero rolls remain
while(qty-- > 0)
{
   int diceNr = RollDice(random);

   Console.WriteLine(diceNr);

   if (diceNr == 6) 
   {
      // rolled six, add two more rolls
      Console.WriteLine("You got a six, that means you get 2 extra throws!");
      qty += 2; 
   } 
   else 
   {
      // not six, add to list
      dicelist.Add(diceNr); 
   } 
}

// output all the non-six values 
var allRolls = string.Join(",", diceList);
Console.WriteLine("All rolls: " + allRolls);

可以也可以使用recursion来做到这一点(尽管我不建议您针对此问题):

private static void Play(List<int> diceList, Random random, int rolls)
{
   // base case, no rolls remain 
   if (rolls == 0) 
      return;

   int diceNr = RollDice(random);
   Console.WriteLine(diceNr);

   if (diceNr == 6)
   {
      Console.WriteLine("You got a six, that means you get 2 extra throws!");
      // add one to rolls since we are "reusing" this slot 
      // adding two would give us one more roll than we really want
      rolls++
   } 
   else 
   {
      diceList.Add(diceNr);
      // decrement rolls by one
      rolls--;
   }
   // recursively call Play with updated roll count
   // which is either original + 1 OR original - 1
   Play(diceList, random, rolls);
} 

private static int RollDice(Random r)
{
    return r.Next(1, 7);
} 

public static void Main()
{
   var diceList = new List<int>();
   var random = new Random();
   int qty = 6;

   Play(diceList, random, qty);

   // output all the non-six values 
   var allRolls = string.Join(",", diceList);
   Console.WriteLine("All rolls: " + allRolls);
} 

答案 1 :(得分:1)

您可以尝试使用while循环,如下所示:

int i = quantity; #initialize a variable to your maximum number of throws
while(i > 0){ #until you have throws yet
    i--; #this is the equivalent of one throw
    int diceNr = RollDice(random);
    dicelist.Add(diceNr);
    Console.WriteLine(diceNr);
    if(diceNr == 6){
        Console.WriteLine("You got a six, that means you get 2 extra throws!");
        i = i + 2; #add your bonus throws
    }
}

答案 2 :(得分:0)

我的(可读)解决方案如下:

class Program
{
    static void Main(string[] args)
    {
        var game = new Game(4);
        game.Play();
    }
}

public class Game
{
    private readonly int _noOfDices;
    private readonly List<int> _result;
    private readonly Random _random = new Random();

    public Game(int noOfDices)
    {
        _noOfDices = noOfDices;
        _result = new List<int>();
    }


    public void RollDice()
    {
        var no = _random.Next(1, 7);

        if (no == 6)
        {
            RollDice();
            RollDice();
        }
        else
        {
            _result.Add(no);
        }
    }

    public void Play()
    {
        for (var i = 1; i <= _noOfDices; i++)
        {
            RollDice();
        }

        Console.WriteLine($"Output: {string.Join(',',_result)}");
    }
}

输出:

enter image description here