首先,我想表达的是,我写这篇文章的目的是遵循网站本身的准则,该准则指出,任何人都可以记录其工作,以帮助将来再次需要它。这是上面的截图:
问题很简单,有人希望创建一款具有d20风格的游戏,如何为战斗系统和游戏框架内的一般用途创建骰子滚轮类?
最佳答案
创建一个“骰子袋”非常简单;编写它几乎不需要花费任何精力。但是,在继续操作之前,需要说明一些事项。骰子(复数“骰子”)永远不能为零或负数,因此在编写类时,我们需要为此做好准备。我们将使用我们自己的“ 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/