Refactored code
This commit is contained in:
parent
20af27423b
commit
1aae111af1
@ -1,27 +1,27 @@
|
||||
extern crate libroller;
|
||||
use libroller::config::Config;
|
||||
//use libroller::config::Config;
|
||||
use libroller::roll::Roll;
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
let roll = Config::new(&mut env::args());
|
||||
for rolls in &roll.unwrap_or_else(|e| {println!("Application error: {}", e);
|
||||
std::process::exit(1)})
|
||||
{
|
||||
let x = rolls.to_roll().unwrap_or_else(|e| { println!("Application error: {}", e);
|
||||
std::process::exit(1);
|
||||
// let roll = Config::new(&mut env::args());
|
||||
let rolls = Roll::from_args(&mut env::args()).unwrap_or_else(|err| {
|
||||
eprintln!("Error occured:{}", err);
|
||||
std::process::exit(1)
|
||||
});
|
||||
if x.number() > 1 && x.number() < 26 {
|
||||
for roll in rolls {
|
||||
if roll.number() > 1 && roll.number() < 26 {
|
||||
let mut total = 0;
|
||||
println!("You're rolling {}", x);
|
||||
for value in x.rolls() {
|
||||
println!("You're rolling {}", roll);
|
||||
for value in roll.rolls() {
|
||||
println!("You rolled: {}", value);
|
||||
total += value;
|
||||
}
|
||||
println!("Total value of dice = {}", total);
|
||||
println!("");
|
||||
}else {
|
||||
println!("You're rolling {}", x);
|
||||
println!("Total: {}", x.total())
|
||||
println!("You're rolling {}", roll);
|
||||
println!("Total: {}", roll.total())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,8 +52,8 @@ impl Config {
|
||||
/// Converts a `Config` into a `Roll`. Returns a `Result` that's
|
||||
/// either `Ok(Roll)` or an `Err(_)`
|
||||
pub fn to_roll(&self) -> Result<Roll, ParseIntError> {
|
||||
let number = self.first.parse::<u32>()?;
|
||||
let die = self.second.parse::<u32>()?;
|
||||
let number = self.first.parse::<usize>()?;
|
||||
let die = self.second.parse::<usize>()?;
|
||||
|
||||
Ok(Roll::new(number, die))
|
||||
}
|
||||
|
@ -2,6 +2,9 @@
|
||||
pub extern crate rand;
|
||||
pub mod roll;
|
||||
pub mod config;
|
||||
//pub mod error;
|
||||
|
||||
pub use roll::*;
|
||||
pub use roll::Roll;
|
||||
pub use std::{num, io};
|
||||
//pub use error::RollError;
|
||||
|
36
src/roll.rs
36
src/roll.rs
@ -1,19 +1,21 @@
|
||||
//! A struct in which our data is held for rolling. By ensuring that
|
||||
//! only `u32`s are used we prevent the need for additional type
|
||||
//! only `usize`s are used we prevent the need for additional type
|
||||
//! checking at time of use. */
|
||||
extern crate rand;
|
||||
use rand::Rng;
|
||||
use std::fmt;
|
||||
use std::num::ParseIntError;
|
||||
use super::std;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Roll {
|
||||
die: u32,
|
||||
number: u32,
|
||||
die: usize,
|
||||
number: usize,
|
||||
}
|
||||
impl Roll {
|
||||
/// A fn to create new `Roll` structs, private to prevent
|
||||
/// being used outside the library.
|
||||
pub fn new(number: u32, die: u32) -> Roll {
|
||||
pub fn new(number: usize, die: usize) -> Roll {
|
||||
Roll {
|
||||
die: die,
|
||||
number: number,
|
||||
@ -27,37 +29,51 @@ impl Roll {
|
||||
/// let x = Roll::new(&2, &6);
|
||||
/// assert!(x.number() == 2);
|
||||
/// ```
|
||||
pub fn number(&self) -> u32 {
|
||||
pub fn number(&self) -> usize {
|
||||
self.number
|
||||
}
|
||||
/// Returns the value stored in the die field of a roll.
|
||||
/// Allows the fields of `Roll` to be private (and unchangable)
|
||||
/// while still allowing access to act on their internal value
|
||||
pub fn die(&self) -> u32 {
|
||||
pub fn die(&self) -> usize {
|
||||
self.die
|
||||
}
|
||||
/// Creates a Vector to hold each of the values rolled. Making
|
||||
///a Vector ensures the ability to work on exactly the same
|
||||
///die rolls easily and accurately, since calling the `Roll`
|
||||
///iterator again will produce new results.
|
||||
pub fn rolls(self) -> Vec<u32> {
|
||||
pub fn rolls(self) -> Vec<usize> {
|
||||
self.collect()
|
||||
}
|
||||
|
||||
/// Used on a `Roll` to return only the final value, ignoring
|
||||
///the values used to add up to that result. Allows `.total()`
|
||||
///syntax when used
|
||||
pub fn total(self) -> u32 {
|
||||
pub fn total(self) -> usize {
|
||||
self.sum()
|
||||
}
|
||||
|
||||
pub fn from_args(args: &mut std::env::Args) -> Result<Vec<Roll>, ParseIntError> {
|
||||
args.next();
|
||||
let mut total: Vec<Roll> = vec![];
|
||||
while args.len() >= 2 {
|
||||
let roll: Vec<String> = args.take(2).collect();
|
||||
total.push(Roll::new(
|
||||
roll[0].parse::<usize>()?,
|
||||
roll[1].parse::<usize>()?,
|
||||
)
|
||||
);
|
||||
}
|
||||
Ok(total)
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns `Roll` into an `Iterator`, causing it to create a series of random
|
||||
///numbers between 1 and the chosen die size. It counts down the number field
|
||||
///of the `Roll` and returns `None` to end the iteration. It's only capable of
|
||||
///returning u32 values, which should never be a problem.
|
||||
///returning usize values, which should never be a problem.
|
||||
impl Iterator for Roll {
|
||||
type Item = u32;
|
||||
type Item = usize;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.number != 0 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user