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

时间:2015-01-09 06:15:36

标签: c# probability code-snippets

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

问题很简单,有人希望如何制作d20风格的游戏,为战斗系统制作骰子滚轮类以及在游戏框架内的一般用途?

1 个答案:

答案 0 :(得分:3)

创建“Dicebag”非常简单;编写它几乎不需要任何努力。但是,在继续之前还有一些事情需要描述。骰子(复数“骰子”)永远不能为零或负数,因此在写课时我们需要为此做好准备。我们将使用我们自己的“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 ] );
}

这就是它的全部内容。