Separate planks from main
[?]
Jul 11, 2020, 8:55 PM
ZJPL7VNQEYASQ7QIUCR3X2IPWF5MAR6WX7VY6OWMRDMPJK7GYWIACDependencies
- [2]
HJBUNOG2Correct Plank docs - [3]
MLUGR2LLAdd default impl and some basic plank logic - [4]
RQRFFUF6Can build the first basic row - [5]
RXRHPE7SSovrummet - [6]
UPCMFGXFPrint more about the room and define PLAY - [7]
5TH3AA4610 mm extra space - [8]
3H2BGWRGWIP - [9]
ES2PMPT4Using structures instead - [10]
2USDM5CHWIP for more generic - [11]
JBGHRTSWUse the new defaults - [12]
F3GMCMWTMerge branch 'master' into sovrum - [*]
NRQSXJVRInitial commit
Change contents
- file addition: floor.rs[14.487]
// Use the same global constantsuse crate::PLANKMAX;use crate::SAWBLADE;// Define a plank piece structure#[derive(Debug)]pub struct Plank {length: u32,endpiece: bool,new: bool,}impl Default for Plank {/// Create a new, default plankfn default() -> Self {Self {length: PLANKMAX,endpiece: false,new: true,}}}impl Plank {/// Create a new plankpub fn new(&self) -> Self {Plank::default()}/// Is this a new plank?pub fn is_new(&self) -> bool {self.new}/// Is this at the end of a rowpub fn get_endpiece(&self) -> bool {self.endpiece}/// Make this plank an endpiecepub fn set_endpiece(&mut self) -> () {self.endpiece = true;}/// Return the length of the plankpub fn length(&self) -> u32 {self.length}/// Return the original plank with the new length along with leftoverpub fn cut_to_length(mut self, measure: u32) -> (Plank, Plank) {if measure > PLANKMAX {panic!("Can't cut a plank longer than it is!");}// Create the leftover plank, correct for material loss due to sawinglet leftover_plank = Plank {length: self.length - (measure + SAWBLADE),endpiece: false,new: false,};// Trim self to the new lengthself.length = measure;// In case the plank was new, mark it as cutif self.new {self.new = false;}// Return the original plank and the leftover(self, leftover_plank)}}// Define a row structure#[derive(Debug)]pub struct Row {planks: Vec<Plank>,coverage: u32,full: bool,}impl Default for Row {/// Create a new, empty rowfn default() -> Self {Self {planks: vec![],coverage: 0,full: false,}}}impl Row {/// Is this row considered full?pub fn get_full(&self) -> bool {self.full}/// Mark this row as fullpub fn set_full(&mut self) -> () {self.full = true;}/// Get the number of plank rows for this floorpub fn rows_count(&self) -> usize {self.planks.len()}/// Get the number of plank rows for this floorpub fn planks(&mut self) -> &mut Vec<Plank> {&mut self.planks}/// Increase how much of the row is filledpub fn add_coverage(&mut self, coverage: u32) -> () {self.coverage += coverage;}/// Get the number of plank rows for this floorpub fn get_coverage(&self) -> u32 {self.coverage}/// Add a plank to this rowpub fn add(&mut self, plank: Plank) -> () {self.planks.push(plank);}}// Define a floor structure#[derive(Debug)]pub struct Floor {rows: Vec<Row>,coverage: u32,complete: bool,}impl Default for Floor {/// Create a new, empty rowfn default() -> Self {Self {rows: vec![],coverage: 0,complete: false,}}}impl Floor {/// Is this floor considered complete?pub fn is_complete(&self) -> bool {self.complete}/// Get the number of plank rows for this floorpub fn rows_count(&self) -> usize {self.rows.len()}pub fn add(&mut self, row: Row) -> () {self.rows.push(row);}} - replacement in src/main.rs at line 1
static PLANKMAX: u32 = 2200;// Import and use floor.rsmod floor;use floor::{Plank, Row, Floor};//static PLANKMAX: u32 = 2200;static PLANKMAX: u32 = 2010; - edit in src/main.rs at line 9
static SAWBLADE: u32 = 10; - edit in src/main.rs at line 14[3.61]→[3.132:133](∅→∅),[3.71]→[3.132:133](∅→∅),[3.75]→[3.132:133](∅→∅),[3.98]→[3.132:133](∅→∅),[3.185]→[3.132:133](∅→∅),[3.132]→[3.132:133](∅→∅),[3.133]→[3.0:34](∅→∅),[3.34]→[3.214:283](∅→∅),[3.214]→[3.214:283](∅→∅),[3.283]→[3.65:80](∅→∅),[3.80]→[3.35:38](∅→∅),[3.283]→[3.35:38](∅→∅),[3.38]→[3.81:297](∅→∅),[3.297]→[2.0:29](∅→∅),[2.29]→[3.338:392](∅→∅),[3.338]→[3.338:392](∅→∅),[3.392]→[2.30:66](∅→∅),[2.66]→[3.444:509](∅→∅),[3.444]→[3.444:509](∅→∅),[3.509]→[2.67:106](∅→∅),[2.106]→[3.0:57](∅→∅),[3.509]→[3.0:57](∅→∅),[3.57]→[2.107:181](∅→∅),[3.57]→[3.509:997](∅→∅),[2.181]→[3.509:997](∅→∅),[3.509]→[3.509:997](∅→∅)
// Define a plank piece structure#[derive(Debug)]struct Plank {length: u32,endpiece: bool,new: bool,}impl Default for Plank {/// Create a new, default plankfn default() -> Self {Self {length: PLANKMAX,endpiece: false,new: true,}}}impl Plank {/// Is this a new plank?fn is_new(&self) -> bool {self.new}/// Is this at the end of a rowfn is_endpiece(&self) -> bool {self.endpiece}/// Return the length of the plankfn length(&self) -> u32 {self.length}/// Return the original plank with the new length along with leftoverfn cut_to_length(mut self, measure: u32) -> (Plank, Plank) {let leftover_plank = Plank {length: self.length - measure,endpiece: false,new: false,};// Trim self to the new lengthself.length = measure;// In case the plank was new, mark it as cutif self.new {self.new = false;}// Return the original plank and the leftover(self, leftover_plank)}} - edit in src/main.rs at line 15[3.998]→[3.38:118](∅→∅),[3.38]→[3.38:118](∅→∅),[3.118]→[3.999:1018](∅→∅),[3.1018]→[3.118:136](∅→∅),[3.118]→[3.118:136](∅→∅),[3.136]→[3.1019:1212](∅→∅)
// Define a row structure#[derive(Debug)]struct Row {planks: Vec<Plank>,coverage: u32,full: bool,}impl Default for Row {/// Create a new, empty rowfn default() -> Self {Self {planks: vec![],coverage: 0,full: false,}}} - edit in src/main.rs at line 16[3.137]→[3.137:448](∅→∅),[3.448]→[3.283:286](∅→∅),[3.283]→[3.283:286](∅→∅),[3.286]→[3.449:529](∅→∅),[3.529]→[3.1213:1232](∅→∅),[3.1232]→[3.529:551](∅→∅),[3.529]→[3.529:551](∅→∅),[3.551]→[3.0:197](∅→∅),[3.197]→[3.551:869](∅→∅),[3.551]→[3.551:869](∅→∅)
impl Row {/// Is this row considered complete?fn is_full(&self) -> bool {self.full}/// Get the number of plank rows for this floorfn rows_count(&self) -> usize {self.planks.len()}fn add(&mut self, plank: Plank) -> () {self.planks.push(plank);}}// Define a floor structure#[derive(Debug)]struct Floor {rows: Vec<Row>,coverage: u32,complete: bool,}impl Default for Floor {/// Create a new, empty rowfn default() -> Self {Self {rows: vec![],coverage: 0,complete: false,}}}impl Floor {/// Is this floor considered complete?fn is_complete(&self) -> bool {self.complete}/// Get the number of plank rows for this floorfn rows_count(&self) -> usize {self.rows.len()}fn add(&mut self, row: Row) -> () {self.rows.push(row);}} - edit in src/main.rs at line 36
floor.coverage += PLANKWIDTH; - replacement in src/main.rs at line 52
while row.coverage <= ROOMLENGTH {if plank.length > ROOMLENGTH {// Planks are longer than the room, need to cut them//let leftover;let (plankpart, leftover) = plank.cut_to_length(ROOMLENGTH);while row.get_coverage() <= ROOMLENGTH {// TODO should also check if the plank is too short, then WARN or take a newif plank.length() < 200 {// Take a new plank and continuelet newplank: Plank = Default::default();plank = newplank;}if plank.length() > ROOMLENGTH - row.get_coverage() {// Cut the plank to lengthlet (plankpart, leftover) = plank.cut_to_length(ROOMLENGTH - row.get_coverage()); - replacement in src/main.rs at line 66
row.coverage += plankpart.length();row.add_coverage(plankpart.length()); - edit in src/main.rs at line 71
} else if plank.length() == ROOMLENGTH - row.get_coverage() {// If the plank is the same length as the room, no need to cut// Use the whole plank, add to row coveragerow.add_coverage(plank.length());row.add(plank);// Take a new plank and continuelet newplank: Plank = Default::default();plank = newplank;} else {// Plank is shorter than the room, need multiple// Use the whole plank, add to row coveragerow.add_coverage(plank.length());row.add(plank);// Take a new plank and continuelet newplank: Plank = Default::default();plank = newplank; - edit in src/main.rs at line 92
println!("Row: {:#?}", row); - edit in src/main.rs at line 94
// The row is now fullrow.set_full();// Annotate the planks in the beginning and end as endpiecesif let Some(plank) = row.planks().first_mut() {plank.set_endpiece()}if let Some(plank) = row.planks().last_mut() {plank.set_endpiece()}