1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
//################################################################################################## //******************************************* DEPENDENCIES **************************************** //################################################################################################## extern crate rand; //-------------------------------------------------------------------------------------------------- use dice::Die; use rand::Rng; use std::fmt::{Display,Formatter,Result}; //################################################################################################## //**************************************** STRUCT DECLARATIONS ************************************* //################################################################################################## //================================================================================================== /// A die that has values of a fixed type, but no other relationship between the values must exist. /// /// For example: /// /// - a die with symbols, /// - a numeric die without a fixed interval between values /// - a numeric die with repeated values //================================================================================================== pub struct ValueDie<E: Copy> { /// Collection of all possible values values: Vec<E>, /// Value last rolled; die is initialized to the first item in *values* current_value: E, /// Source of randomness rng: Box<Rng>, } //################################################################################################## //************************************* STRUCT IMPLEMENTATIONS ************************************* //################################################################################################## //================================================================================================== impl<E: Copy> ValueDie<E> { //================================================================================================== //============================================================================================== /// Pseudo-constructor for ValueDie. //---------------------------------------------------------------------------------------------- /// /// ##### Parameters /// - vals : non-empty vector containing all possible die values; may contain duplicates /// - rng : optional RNG; if None, defaults to a ThreadRng /// //---------------------------------------------------------------------------------------------- /// ##### Return value /// A ValueDie constructed to the given specifications /// //---------------------------------------------------------------------------------------------- /// # Examples /// /// ``` /// # extern crate ezra; /// extern crate rand; /// use ezra::dice::{Die,ValueDie}; /// use rand::OsRng; /// # fn main() { /// /// // Values a die may have /// #[derive(Copy,Clone,Debug)] /// enum DiceValues { /// Value1, /// Value2, /// Value3, /// } /// /// let values = vec![DiceValues::Value1, DiceValues::Value2, DiceValues::Value3]; /// let rng = OsRng::new().unwrap(); /// let mut die = ValueDie::new(values, Some(Box::new(rng))); /// /// println!("The value {:?} was rolled.", die.roll().get_value()); /// # } /// ``` //---------------------------------------------------------------------------------------------- /// The die may be constructed without an RNG, defaulting to its own thread-local RNG. /// /// ``` /// # extern crate ezra; /// # use ezra::dice::{Die,ValueDie}; /// # fn main() { /// // Values a die may have /// #[derive(Copy,Clone)] /// enum DiceValues { /// Value1, /// Value2, /// Value3, /// } /// /// let values = vec![DiceValues::Value1, DiceValues::Value2, DiceValues::Value3]; /// let mut die = ValueDie::new(values, None); /// # } /// ``` //============================================================================================== pub fn new(vals: Vec<E>, rng: Option<Box<Rng>>) -> Self { // vals vector must be nonempty assert!(!vals.is_empty()); ValueDie { current_value: vals[0], values: vals, // Use RNG if given, otherwise default to a ThreadRng rng: match rng { Some(r) => r, None => Box::new(rand::thread_rng()), }, } } } //================================================================================================== impl<E: Copy> Die for ValueDie<E> { //================================================================================================== //============================================================================================== type ValueType = E; //---------------------------------------------------------------------------------------------- // Type of value the die may have. //============================================================================================== //============================================================================================== fn roll(&mut self) -> &mut Self { //---------------------------------------------------------------------------------------------- // Randomizes the value of the die to a new value in `values`, and returns itself. //---------------------------------------------------------------------------------------------- // TAKES: nothing // // RETURNS: mutable reference to itself //============================================================================================== self.current_value = *self.rng.choose(self.values.as_slice()).unwrap(); self } //============================================================================================== fn get_value(&self) -> Self::ValueType { //---------------------------------------------------------------------------------------------- // Obtain the value of the die. //---------------------------------------------------------------------------------------------- // TAKES: nothing // // RETURNS: current value of the die //============================================================================================== self.current_value } //============================================================================================== fn values(&self) -> Vec<Self::ValueType> { //---------------------------------------------------------------------------------------------- // Obtain a list of all values the die may have. //---------------------------------------------------------------------------------------------- // TAKES: nothing // // RETURNS: //============================================================================================== self.values.clone() } } //================================================================================================== impl<E: Display + Copy> Display for ValueDie<E> { //================================================================================================== //============================================================================================== fn fmt(&self, f: &mut Formatter) -> Result { //---------------------------------------------------------------------------------------------- // Print the die using a given formatter. //---------------------------------------------------------------------------------------------- // TAKES: f -> formatter with which to print the die // // RETURNS: Ok(()) -> printed die without error // Err(...) -> something went wrong while attempting to print the die //============================================================================================== write!(f, "{}", self.current_value) } }