Skip to main content
 首页 » 编程设计

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

2024年08月06日37xing901022

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

问题很简单,如果有人希望创建一个 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 ] ); 
} 

仅此而已。