https://github.com/xyaneon/xyaneon.games.dice
A .NET Standard 2.0 library for dice, including standard and custom dice.
https://github.com/xyaneon/xyaneon.games.dice
csharp dice die dotnet games library netstandard netstandard20
Last synced: 9 months ago
JSON representation
A .NET Standard 2.0 library for dice, including standard and custom dice.
- Host: GitHub
- URL: https://github.com/xyaneon/xyaneon.games.dice
- Owner: Xyaneon
- License: mit
- Created: 2020-03-22T22:22:28.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-12-31T17:50:35.000Z (over 1 year ago)
- Last Synced: 2025-01-10T23:29:57.571Z (over 1 year ago)
- Topics: csharp, dice, die, dotnet, games, library, netstandard, netstandard20
- Language: C#
- Homepage:
- Size: 339 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Xyaneon.Games.Dice
[][License]
[][NuGet package]
[](https://github.com/Xyaneon/Xyaneon.Games.Dice/actions/workflows/main.yml)
[](https://coveralls.io/github/Xyaneon/Xyaneon.Games.Dice?branch=main)
[](https://github.com/semantic-release/semantic-release)
![Package Icon][icon]
A .NET Standard 2.0 library for dice, including standard and custom dice.
## Usage
### Setup
To use this library, you must first install the [NuGet package][NuGet package]
for it, then add the following `using` statement at the top of each C# source
file where you plan on using it:
```csharp
using Xyaneon.Games.Dice;
```
### Rolling standard dice
#### Single rolls
This library provides several built-in classes representing standard dice,
including:
- D4
- D6
- D8
- D10
- D12
- D20
These each inherit from `Dice`. You can just call them with an empty
constructor to start using them right away. An optional `seed` integer can
also be passed into a die's constructor if you want a repeatable pseudorandom
sequence of rolled values.
After creation, you can simply call the `Roll` method to get a random result:
```csharp
var die = new D6();
int rollResult = die.Roll(); // Can be 1, 2, 3, 4, 5 or 6.
```
#### Multiple rolls
To avoid having to call `Roll` multiple times, you can also supply an integer
indicating how many rolls you want, then get the results as an
`IEnumerable`:
```csharp
var die = new D20();
// Sample returned sequence: [6, 18, 4, 20, 1]
IEnumerable rollResult = die.Roll(5);
```
### Flipping coins
This library also provides a `Coin` class, which is considered to be a
two-sided die. Flips are returned as `CoinFlipResult` enum values:
```csharp
var coin = new Coin();
// Could return CoinFlipResult.Heads or CoinFlipResult.Tails.
CoinFlipResult flipResult = coin.Flip();
```
### Custom dice
You are not restricted to standard number dice. The `Die` base class is
generic and can be instantiated using a custom collection of your choice
representing the die's faces.
```csharp
var colorDie = new Die(new Color[] {
Color.Red,
Color.Blue,
Color.Yellow,
Color.Green
});
Color rollResult = die.Roll();
```
### Symbols conversion
A static `Symbols` class is also provided with constants for D6 faces, and a
couple conversion methods:
- `Symbols.GetD6ValueForSymbol(char symbol)`
- `Symbols.GetSymbolForD6Value(int value)`
You can use these to get from a ⚃ character to a 4 or vice versa, for example.
## License
This library is free and open-source software provided under the MIT license.
Please see the [LICENSE.txt][License] file for details.
[icon]: https://raw.githubusercontent.com/Xyaneon/Xyaneon.Games.Dice/main/Xyaneon.Games.Dice/images/icon.png
[License]: https://github.com/Xyaneon/Xyaneon.Games.Dice/blob/main/LICENSE.txt
[NuGet package]: https://www.nuget.org/packages/Xyaneon.Games.Dice/