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
use rand::{self, Rng}; use color::{self, Color}; use std::cmp; use std::fmt; #[derive(Clone)] pub struct Peg { color: Color, found: bool, } impl Peg { pub fn new(color: Color) -> Peg { Peg { color, found: false, } } pub fn new_random() -> Peg { Peg { color: Color::new(rand::thread_rng().gen_range(0, color::NUM_COLORS)), found: false, } } pub fn color(&self) -> Color { self.color.clone() } pub fn found(&self) -> bool { self.found } pub fn find(&mut self) { self.found = true; } } impl cmp::PartialEq for Peg { fn eq(&self, other: &Peg) -> bool { self.color == other.color } } impl fmt::Debug for Peg { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}, {}", self.color, self.found) } } impl fmt::Display for Peg { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.color) } } impl Into<Peg> for char { fn into(self) -> Peg { use Color::*; match self.to_lowercase().to_string().as_ref() { "r" => Peg::new(Red), "o" => Peg::new(Orange), "y" => Peg::new(Yellow), "g" => Peg::new(Green), "l" => Peg::new(Blue), "i" => Peg::new(Indigo), "p" => Peg::new(Purple), "b" => Peg::new(Black), "w" => Peg::new(White), _ => Peg::new(Black), } } } pub fn convert(guess: String) -> Vec<Peg> { let mut converted: Vec<Peg> = Vec::new(); for c in guess.chars() { converted.push(c.into()); } converted }