c# - 如何制作一个简单的骰子系统

标签 c# probability code-snippets

首先,我想表达的是,我写这篇文章的目的是遵循网站本身的指导方针,该指导方针规定任何人都可以记录他们的工作,以帮助促进将来再次需要它的可能性。这是它所说的屏幕截图: enter image description here

问题很简单,如果有人希望创建一个 d20 风格的游戏,如何在游戏框架内为战斗系统和一般用途创建一个骰子类?

最佳答案

创建“Dicebag”非常简单;编写它几乎不需要任何努力。但是,在继续之前需要说明一些事情。骰子(复数“骰子”)永远不能为零或负数,因此在编写类(class)时我们需要为此做好准备。我们将用我们自己的“Dice”枚举包装参数重载,它将无条件地是一个无符号整数。这样做将有助于避免各种未定义的行为。我们还将向返回值添加 +1,以确保数字永远不会为 0,正如我所说,物理骰子不可能实现这一点。

使用这些规则和法律,这里是规定的类别:

using System;
using System.Collections.Generic;

namespace Utilities {
    /**
     * Original Author: Gordon Kyle Wallace, "Krythic"
     * 
     * This class is designed to emulate/facilitate the rolling of real-world
     * dice within a d20 stylized game/system.
     * 
     * License:
     * There is not one; this snippet may be used/modified by anyone for
     * any arbitrary reason. I, Gordon Kyle Wallace "Krythic", lay no claim upon
     * this document, the program it ultimately produces, or the thought-patterns
     * that may—or may not—emerge from using it.
     * 
     * This disclaimer may be deleted at your whim.
     * 
     * ~Krythic
     */
    public class DiceBag {
        public enum Dice : uint {
            /// <summary>
            /// This can be considered a double-sided coin;
            /// used to delimit a 50/50 probability.
            /// </summary>
            D2 = 2 ,
            /// <summary>
            /// A Tetrahedron
            /// A 4 Sided Die
            /// </summary>
            D4 = 4 ,
            /// <summary>
            /// A Cube
            /// A 6 Sided Die
            /// </summary>
            D6 = 6 ,
            /// <summary>
            /// A Octahedron
            /// A 8 Sided Die
            /// </summary>
            D8 = 8 ,
            /// <summary>
            /// A Pentagonal Trapezohedron
            /// A 10 Sided Die
            /// </summary>
            D10 = 10 ,
            /// <summary>
            /// A Dodecahedron
            /// A 12 Sided Die
            /// </summary>
            D12 = 12 ,
            /// <summary>
            /// A Icosahedron
            /// A 20 Sided Die
            /// </summary>
            D20 = 20 ,
            /// <summary>
            /// A Rhombic Triacontahedron
            /// A 30 Sided Die
            /// </summary>
            D30 = 30 ,
            /// <summary>
            /// A Icosakaipentagonal Trapezohedron
            /// A 50 Sided Die
            /// </summary>
            D50 = 50 ,
            /// <summary>
            /// A Pentagonal Hexecontahedron
            /// A 60 Sided Die
            /// </summary>
            D60 = 60 ,
            /// <summary>
            /// A Zocchihedron
            /// A 100 Sided Die
            /// </summary>
            D100 = 100
        };

        private Random _rng;

        public DiceBag() {
            _rng = new Random();
        }

        /**
         * The default dice-rolling method. All methods link to this one.
         */
        private int InternalRoll( uint dice ) {
            return 1 + _rng.Next( ( int )dice );
        }

        /// <summary>
        /// Rolls the specified dice.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <returns>The Number rolled.</returns>
        public int Roll( Dice d ) {
            return InternalRoll( ( uint )d );
        }

        /// <summary>
        /// Rolls the chosen dice then adds a modifier
        /// to the rolled number.
        /// </summary>
        /// <param name="dice">The dice.</param>
        /// <param name="modifier">The modifier.</param>
        /// <returns></returns>
        public int RollWithModifier( Dice dice , uint modifier ) {
            return InternalRoll( ( uint )dice ) + ( int )modifier;
        }

        /// <summary>
        /// Rolls a series of dice and returns a collection containing them.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="times">The times.</param>
        /// <returns>A Collection Holding the dice rolls.</returns>
        public List<int> RollQuantity( Dice d , uint times ) {
            List<int> rolls = new List<int>();
            for( int i = 0 ; i < times ; i++ ) {
                rolls.Add( InternalRoll( ( uint )d ) );
            }
            return rolls;
        }
    }
}

如何使用这个类:

实现类非常简单。首先,您必须创建“Dicebag”类的一个实例,然后选择您选择的方法。这是一个掷出 1d20(一个二十面骰子)的例子:

DiceBag bag = new DiceBag();
Console.WriteLine( bag.Roll( DiceBag.Dice.D20 ) );

如何将修饰符属性应用于卷:

同样,这非常简单。我们将使用名为“RollWithModifier”的第二种方法,并使用选定的骰子,还使用我们选择的任何无符号整数输入第二个重载。这是一个片段,在使用 d20 的过程中,将向最终掷骰添加 22 的修饰符:

DiceBag bag = new DiceBag();
Console.WriteLine( bag.RollWithModifier( DiceBag.Dice.D20 , 22 ) );

您可能还会注意到,我冒昧地添加了一个用于大量掷骰子的辅助方法。这在某些情况下可能很有用。下面的代码片段将使用 d20 生成 131 个骰子:

DiceBag bag = new DiceBag();
List<int> rolls = bag.RollQuantity( DiceBag.Dice.D20 , 131 );
for( int i = 0 ; i < rolls.Count ; i++ ) {
   Console.WriteLine( rolls[ i ] );
}

仅此而已。

关于c# - 如何制作一个简单的骰子系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27854865/

相关文章:

c# - 复杂对象的JSON解析

c# - Xamarin Studio,GTK 2.0 C# 应用程序,运行时错误,无法加载 libglib-2.0-0.dll

algorithm - 从一组 x 和 y 中选择 k 项以满足特定条件

javascript - 将链接替换为其他链接中的文本内容 - 抽象代码

sublimetext3 - Sublime Text 3 片段中的变量

java - 如何在 Eclipse 中将 "sysout"片段与选定的文本一起使用?

javascript - ASP.net、JavaScript/JQuery - 服务器发送事件 (SSE) 无法与 ASHX 处理程序一起使用

c# - 使用 SendGrid v3 的身份将交易模板作为确认电子邮件发送

python - 均匀生成不同整数的随机对

php - 广告系统提示