/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
1 by Gustav Hartvigsson
* Initial code - far from done
1
using GLib;
2
3
namespace VQDR.Expression {
4
  
5
  /**
6
   * A Simple Represetation of a dice.
7
   */
8
  public class Dice {
9
    public int times { get; set; }
10
    public int faces { get; set; }
11
    public int modifier { get; set; }
12
    
13
    
14
    public Dice (int times = 1, int faces = 6, int modifier = 0) {
15
      this.times = times;
16
      this.faces = faces;
17
      this.modifier = modifier;
18
    }
19
    
20
    public int roll () {
21
      if (faces <= 0) {
22
        return modifier;
23
      }
24
      
25
      if (times <= 0) {
26
          return modifier;
27
      }
28
      
29
      if (faces == 1) {
30
        return times + modifier;
31
      }
32
      
33
      int retval = modifier;
34
      for (size_t i = 1; i <= times; i++) {
35
        int r = (VQDR.Common.Random.get_static_int () % faces).abs ();
36
        retval += r;
37
        
38
      }
39
      
40
      return retval;
41
    }
42
    
43
    public string to_string () {
44
      if ((times == 0) && (faces == 0)) {
45
        return "0";
46
      }
47
      
48
      StringBuilder retval = new StringBuilder ();
49
      
50
      if (times > 0) {
51
        retval.append (times.to_string ()).append_c ('d').append (faces.to_string ());
52
      }
53
      if (modifier > 0) {
54
          retval.append_c ('+').append (modifier.to_string ());
55
      } else if (modifier < 0) {
56
          retval.append (modifier.to_string ());
57
      }
58
      
59
      return (string) retval.data;
60
    }
61
    
62
  }
63
}