Skip to content

Instantly share code, notes, and snippets.

@jhn--
Last active June 9, 2022 11:41
Show Gist options
  • Save jhn--/5d2b72bb705e815908a01a7b2a32bdff to your computer and use it in GitHub Desktop.
Save jhn--/5d2b72bb705e815908a01a7b2a32bdff to your computer and use it in GitHub Desktop.
trying out of code (refactor and tinkering) with regards to exercise G of repo ultimate_rust_crash_course
#![allow(unused_variables, unused_mut, dead_code)]
#[derive(Debug)]
enum Shot {
// 3 classifications of accuracy for the Shot
Bullseye,
Hit(f64),
Miss,
}
impl Shot {
// traits(?)
fn points(&self) -> i32 {
match self {
// define classifications and their corresponding score
// what's match.. really?
// feels like a switch which is defined in 2 parts
// enum and match
// enum defines the choices
// match defines the selection process(?)
Shot::Bullseye => 5,
Shot::Hit(x) if x < &3.0 => 2,
Shot::Hit(x) => 1,
Shot::Miss => 0,
}
}
}
#[derive(Debug)]
struct Coord {
// creating a Coord object/struct
x: f64,
y: f64,
}
impl Coord {
// traits(? idk if im using the right term)
fn distance_from_center(&self) -> f64 {
// find out the distance from center
(self.x.powf(2.0) + self.y.powf(2.0)).sqrt()
}
fn print_description(&self) {
// print out details of the Coord
println!("coord is {:.1} away, at ({:.1}, {:.1})",
self.distance_from_center(),
self.x,
self.y);
}
}
fn main() {
// now let's bridge/convert a Coord to a Shot(and its corresponding score)
// let's just convert ONE Coord to Shot for now
// define a SINGLE Coord
let coord = create_coord();
// let's print it
coord.print_description();
// now let's convert the single `coord` to a `shot`
// let shot = match coord.distance_from_center() {
// x if x < 1.0 => Shot::Bullseye,
// x if x < 5.0 => Shot::Hit(x),
// _ => Shot::Miss,
// };
// now let's call coord_to_shot to convert the single `coord` to a `shot`
let shot = coord_to_shot(coord);
println!("Single Shot is a {:?}.",shot);
// now let's print the points of that shot
println!("Single Shot points is {:?}",shot.points());
println!("-=-\n");
// now lets create a Vector of Coords
let coords = create_coords(5);
let mut shots: Vec<Shot> = Vec::new();
for c in coords {
c.print_description();
let s = coord_to_shot(c);
println!("Single Shot is a {:?}, with a score of {} points.",s, s.points());
println!();
shots.push(s);
}
let mut total_score = shots.iter().fold(0, |acc, s| acc + s.points());
println!("Total score: {}", total_score);
}
// fn to create a Coord (outside of main...?)
fn create_coord() -> Coord {
let coord = Coord {
x: (rand::random::<f64>() - 0.5) * 12.0,
y: (rand::random::<f64>() - 0.5) * 12.0,
};
coord
}
// fn to call create_coord and push into a Vector
fn create_coords(num: i32) -> Vec<Coord> {
// create_coords is a fn which access an i32 and produces a Vector of Coords
// it seems that we really need
let mut coords: Vec<Coord> = Vec::new(); // need mut so that for loop can borrow
for i in 0..num {
let coord = create_coord();
coords.push(coord);
}
coords
}
// Coords to Shots conversion
fn coord_to_shot(c: Coord) -> Shot {
match c.distance_from_center() {
x if x < 1.0 => Shot::Bullseye,
x if x < 5.0 => Shot::Hit(x),
_ => Shot::Miss,
}
}
@jhn--
Copy link
Author

jhn-- commented Jun 9, 2022

ugh indentations :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment