创建一个骰子,如果它落在 C# 中的特定数字上,它会加倍并继续滚动?
Creating a dice that doubles and continues to roll if it lands on a specific number in C#?
我正在尝试制作一个我认为必须使用递归的程序。简而言之:创建一个将掷骰子的程序。如果它落在 6 上,则创建并滚动两个骰子,依此类推,直到没有更多的 6 被滚动。
问题不是创建新的或真正的随机对象,而是递归骰子。
递归方法如下所示:
public static int Recursion(int a)
{
Random m = new Random();
if (a < 6)
{
return a;
}
else
{
a = m.Next(1, 7);
return Recursion(a) * 2;
}
}
可能是这样的?
public static int Roll() {
return Roll(new Random(), 1);
}
public static int Roll(Random random, int diceCount) {
int sum = 0;
for (int dice = 1; dice <= diceCount; ++dice) {
int rolled = random.Next(1, 7);
if (rolled == 6) {
sum += Roll(random, 2)
}
else
{
sum += rolled;
}
}
return sum;
}
所以,我先掷一个 die/dice。如果它不是 6,那么我接受它的值作为结果。如果是 6,那么我删除那个 die/dice,并用我掷出的另外两个替换它。然后,对于每个新的,我都遵循相同的规则,直到 table 上的所有骰子都滚动并且其中 none 是 6。现在我将所有骰子的值相加。这就是这个递归算法所做的。请注意,尽管它有无限低的机会 - 你可以一直玩到时间结束,因为总是有机会掷出 6,所以你可能只掷出 6 直到你死。
您可以通过创建骰子对象使其更加面向对象:
using System;
using System.Collections.Generic;
class Dices
{
public class Dice
{
private static Random roler = new Random();
private int roledNumber;
public int Role()
{
roledNumber = roler.Next(6) + 1 ;
return roledNumber;
}
public int Number
{
get { return roledNumber; }
}
}
static void Main(string[] args)
{
List<Dice> allDices = new List<Dice>();
Dice firstDice = new Dice();
allDices.Add(firstDice);
if (firstDice.Role() == 6) createDices(allDices);
}
static void createDices(List<Dice> dices)
{
Dice first = new Dice();
dices.Add(first);
if (first.Role() == 6) createDices(dices);
Dice second = new Dice();
dices.Add(second);
if (second.Role() == 6) createDices(dices);
}
}
我正在尝试制作一个我认为必须使用递归的程序。简而言之:创建一个将掷骰子的程序。如果它落在 6 上,则创建并滚动两个骰子,依此类推,直到没有更多的 6 被滚动。
问题不是创建新的或真正的随机对象,而是递归骰子。
递归方法如下所示:
public static int Recursion(int a)
{
Random m = new Random();
if (a < 6)
{
return a;
}
else
{
a = m.Next(1, 7);
return Recursion(a) * 2;
}
}
可能是这样的?
public static int Roll() {
return Roll(new Random(), 1);
}
public static int Roll(Random random, int diceCount) {
int sum = 0;
for (int dice = 1; dice <= diceCount; ++dice) {
int rolled = random.Next(1, 7);
if (rolled == 6) {
sum += Roll(random, 2)
}
else
{
sum += rolled;
}
}
return sum;
}
所以,我先掷一个 die/dice。如果它不是 6,那么我接受它的值作为结果。如果是 6,那么我删除那个 die/dice,并用我掷出的另外两个替换它。然后,对于每个新的,我都遵循相同的规则,直到 table 上的所有骰子都滚动并且其中 none 是 6。现在我将所有骰子的值相加。这就是这个递归算法所做的。请注意,尽管它有无限低的机会 - 你可以一直玩到时间结束,因为总是有机会掷出 6,所以你可能只掷出 6 直到你死。
您可以通过创建骰子对象使其更加面向对象:
using System;
using System.Collections.Generic;
class Dices
{
public class Dice
{
private static Random roler = new Random();
private int roledNumber;
public int Role()
{
roledNumber = roler.Next(6) + 1 ;
return roledNumber;
}
public int Number
{
get { return roledNumber; }
}
}
static void Main(string[] args)
{
List<Dice> allDices = new List<Dice>();
Dice firstDice = new Dice();
allDices.Add(firstDice);
if (firstDice.Role() == 6) createDices(allDices);
}
static void createDices(List<Dice> dices)
{
Dice first = new Dice();
dices.Add(first);
if (first.Role() == 6) createDices(dices);
Dice second = new Dice();
dices.Add(second);
if (second.Role() == 6) createDices(dices);
}
}