2015-01-09 5 views
1

먼저, 웹 사이트 자체의 지침을 따르는 의도로이 글을 쓰고 싶습니다. 누구나 자신의 작업을 문서화하여 다시 필요로하는 상황을 촉진 할 수 있다고 말하고 싶습니다. 미래에. enter image description here간단한 주사위 시스템을 만드는 방법

질문은 아주 간단합니다. d20 스타일의 게임을 만들고 게임 시스템의 주사위 롤러 클래스를 만들고 게임 프레임 워크 내에서 일반적인 용도로 사용하려면 어떻게해야할까요? ?

답변

2

"Dicebag"만들기는 간단합니다. 그것을 쓰기위한 노력이 전혀 필요하지 않습니다. 그러나 계속하기 전에 설명해야 할 몇 가지 사항이 있습니다. 다이 (복수형 주사위)는 절대로 0이나 음수가 될 수 없으므로 클래스를 작성할 때이를 염두에두고 준비해야합니다. 무조건적으로 부호없는 정수가되는 자체 "Dice"열거 형으로 매개 변수 오버로드를 래핑합니다. 이렇게하면 모든 종류의 정의되지 않은 동작을 멀리 유지하는 데 도움이됩니다. 또한 숫자가 0이 아닌지 확인하기 위해 반환 값에 +1을 추가합니다. 이것은 내가 말했듯이 실제 다이가 성취하는 것이 불가능합니다. 이러한 규칙과 법을 사용

, 여기에 규정 된 클래스는 다음과 같습니다

이 클래스의 사용 방법
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"를 사용하고 선택한 주사위를 사용하여 우리가 선택한 부호없는 정수로 두 번째 오버로드를 공급합니다.

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

당신은 내가 대량 주사위 압연을위한 도우미 메서드를 추가하는 자유를했다 것 또한 알 수 있습니다 : 여기에 D20을 사용하는 중, 최종 롤 (22)의 수정을 추가하는 조각이다. 특정 시나리오에서 유용 할 수 있습니다. 아래 스 니펫은 d20을 사용하여 131 개의 주사위 굴림을 생성합니다 :

그리고 그게 전부입니다.

관련 문제