pub struct MazeDefinition {
pub grid: Vec<Vec<char>>,
}Expand description
Represents a maze definition
Fields§
§grid: Vec<Vec<char>>Implementations§
Source§impl MazeDefinition
impl MazeDefinition
Sourcepub fn new(row_count: usize, col_count: usize) -> Self
pub fn new(row_count: usize, col_count: usize) -> Self
Creates a maze definition instance with the given number of rows x columns empty cells
§Arguments
row_count- Number of rowscol_count- Number of columns
§Returns
A new maze definition instance
§Examples
Create a definition with 3 rows and 4 columns and then verify its dimensions
use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
assert_eq!(definition.row_count(), 3);
assert_eq!(definition.col_count(), 4);Sourcepub fn reset(&mut self) -> &mut Self
pub fn reset(&mut self) -> &mut Self
Resets a maze definition instance to empty
§Returns
The maze definition instance
§Examples
Create a definition with 3 rows and 4 columns, verify its dimensions, reset it and then confirm it is empty
use data_model::MazeDefinition;
let mut definition = MazeDefinition::new(3, 4);
assert_eq!(definition.row_count(), 3);
assert_eq!(definition.col_count(), 4);
assert_eq!(definition.reset().is_empty(), true);Sourcepub fn resize(
&mut self,
new_row_count: usize,
new_col_count: usize,
) -> &mut Self
pub fn resize( &mut self, new_row_count: usize, new_col_count: usize, ) -> &mut Self
Resizes a maze definition instance
§Arguments
new_row_count- New number of rowsnew_col_count- New number of columns
§Returns
The maze definition instance
§Examples
Create an empty maze definition, resize it to 3 rows and 4 columns and then verify its dimensions
use data_model::MazeDefinition;
let mut definition = MazeDefinition::new(0, 0);
assert_eq!(definition.row_count(), 0);
assert_eq!(definition.col_count(), 0);
definition.resize(3, 4);
println!("Resize successful");
assert_eq!(definition.row_count(), 3);
assert_eq!(definition.col_count(), 4);
Sourcepub fn is_valid_char(ch: char) -> bool
pub fn is_valid_char(ch: char) -> bool
Checks whether the given character is valid for use within the definition
§Returns
Boolean
§Examples
Print whether ‘X’ (false) and ‘S’ (true) are valid characters
use data_model::MazeDefinition;
let x_is_valid = MazeDefinition::is_valid_char('X');
println!("Character 'X' is valid => {}", x_is_valid);
let s_is_valid = MazeDefinition::is_valid_char('S');
println!("Character 'S' is valid => {}", s_is_valid);Sourcepub fn verify_not_empty(&self) -> Result<(), Error>
pub fn verify_not_empty(&self) -> Result<(), Error>
Verifies whether the definition instance is empty, returning an error if it is
§Returns
This function will return an error if the definition is empty
§Examples
Create an empty maze definition and then verify it
use data_model::MazeDefinition;
let definition = MazeDefinition::new(0, 0);
match definition.verify_not_empty() {
Err(e) => println!("Verification failed: {}", e.to_string()),
Ok(()) => println!("MazeDefinition is not empty"),
}Sourcepub fn from_vec(grid: Vec<Vec<char>>) -> Self
pub fn from_vec(grid: Vec<Vec<char>>) -> Self
Creates a new maze definition for the given vector of cell definition character rows, where:
'S': Represents the starting cell (limited to one).'F': Represents the finishing cell (limited to one).'W': Represents a wall.' ': Represents an empty cell.
§Arguments
grid- Vector of row-column cell states
§Returns
A new definition instance
§Examples
Create a 2 row x 3 column definition with a start, finish and a wall in the last column
use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let definition = MazeDefinition::from_vec(grid);
assert_eq!(definition.row_count(), 2);
assert_eq!(definition.col_count(), 3);Sourcepub fn to_state(&self) -> Vec<Vec<MazeCellState>>
pub fn to_state(&self) -> Vec<Vec<MazeCellState>>
Converts the definition instance to a vector of row cell states
§Returns
A vector of row-column cell states
§Examples
Create a maze definition with 3 rows and 4 columns, convert it to a row-column state vector and then confirm that the number of rows in the state vector is the same as the number of rows in the definition (3).
use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
let state = definition.to_state();
assert_eq!(state.len(), definition.row_count());
assert_eq!(state.len(), 3);Sourcepub fn is_valid(&self, pt: &MazePoint) -> bool
pub fn is_valid(&self, pt: &MazePoint) -> bool
Checks that a point is valid for the definition instance
§Arguments
pt- MazePoint to validate
§Returns
Boolean
§Examples
Create a maze definition with 3 rows and 4 columns and confirm that [2,1] is valid, but that [3,1] is not
use data_model::MazeDefinition;
use data_model::MazePoint;
let definition = MazeDefinition::new(3, 4);
assert_eq!(definition.is_valid( &MazePoint {row: 2, col: 1}), true);
assert_eq!(definition.is_valid( &MazePoint {row: 3, col: 1}), false);Sourcepub fn to_display_chars(&self) -> Vec<Vec<char>>
pub fn to_display_chars(&self) -> Vec<Vec<char>>
Converts the definition instance to a vector of display characters
§Returns
Vector containing the rows of display characters
§Examples
Create a maze definition with 3 rows and 4 columns and print it
use data_model::MazeDefinition;
let definition = MazeDefinition::new(3, 4);
println!("{:?}", definition.to_display_chars());Sourcepub fn delete_cols(
&mut self,
start_col: usize,
count: usize,
) -> Result<(), Error>
pub fn delete_cols( &mut self, start_col: usize, count: usize, ) -> Result<(), Error>
Deletes one or more consecutive columns from the definition instance
§Arguments
start_col- Start column index (zero-based)count- Number of columns to delete
§Returns
This function will return an error in the following situations:
- If the definition is empty
- If the target columns are out of range
§Examples
Create a maze definition with 2 rows and 4 columns with a start, finish and a wall at the end of each row, delete the second and third columns and print the result
use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', ' ', 'W'],
vec![' ', 'F', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.delete_cols(1,2).expect("delete_cols() failed");
println!("{:?}", definition.to_display_chars());Sourcepub fn insert_cols(
&mut self,
start_col: usize,
count: usize,
) -> Result<(), Error>
pub fn insert_cols( &mut self, start_col: usize, count: usize, ) -> Result<(), Error>
Inserts one or more empty columns into the definition instance
§Arguments
start_col- Start column index (zero-based)count- Number of columns to insert
§Returns
This function will return an error in the following situations:
- If the definition is empty
- If the target columns are out of range
§Examples
Create a maze definition with 2 rows and 4 columns, with a start, finish and a wall at the end of each row, insert 2 columns at the start of each row and print the result
use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', ' ', 'W'],
vec![' ', 'F', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.insert_cols(0,2).expect("insert_cols() failed");
println!("{:?}", definition.to_display_chars());Sourcepub fn delete_rows(
&mut self,
start_row: usize,
count: usize,
) -> Result<(), Error>
pub fn delete_rows( &mut self, start_row: usize, count: usize, ) -> Result<(), Error>
Deletes one or more consecutive rows from the definition instance
§Arguments
start_row- Start row index (zero-based)count- Number of rows to delete
§Returns
This function will return an error in the following situations:
- If the definition is empty
- If the target rows are out of range
§Examples
Create a maze definition with 5 rows and 4 columns, delete the first and and second rows and print the result
use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
vec!['W', ' ', ' ', 'W'],
vec![' ', 'W', ' ', 'W'],
vec![' ', ' ', 'W', 'W'],
vec!['W', ' ', ' ', 'W'],
vec![' ', 'W', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.delete_rows(1,2).expect("delete_rows() failed");
println!("{:?}", definition.to_display_chars());Sourcepub fn insert_rows(
&mut self,
start_row: usize,
count: usize,
) -> Result<(), Error>
pub fn insert_rows( &mut self, start_row: usize, count: usize, ) -> Result<(), Error>
Inserts one or more empty rows into the definition instance
§Arguments
start_row- Start row index (zero-based)count- Number of rows to insert
§Returns
This function will return an error in the following situations:
- If the target rows are out of range
§Examples
Create a maze definition with 5 rows and 4 columns, insert 2 rows after the fourth row and print the result
use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
vec!['W', ' ', ' ', 'W'],
vec![' ', 'W', ' ', 'W'],
vec![' ', ' ', 'W', 'W'],
vec!['W', ' ', ' ', 'W'],
vec![' ', 'W', ' ', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
definition.insert_rows(3,2).expect("insert_rows() failed");
println!("{:?}", definition.to_display_chars());Sourcepub fn get_start(&self) -> Option<MazePoint>
pub fn get_start(&self) -> Option<MazePoint>
Locates the starting position within the maze definition (if any)
§Returns
The starting position, else none
§Examples
Locate the starting position in a 2 row x 3 column definition
use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let definition = MazeDefinition::from_vec(grid);
match definition.get_start() {
Some(start) => {
println!("Start found at point {}", start);
},
None => {
println!("Start not found");
}
};Sourcepub fn set_start(&mut self, new_start: Option<MazePoint>) -> Result<(), Error>
pub fn set_start(&mut self, new_start: Option<MazePoint>) -> Result<(), Error>
Sets the starting position within the maze definition (if any)
§Returns
This function will return an error in the following situations:
- If the new starting position is out of range
§Examples
Reset the starting position in a 2 row x 3 column definition
use data_model::MazeDefinition;
use data_model::MazePoint;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
let new_start = MazePoint {row: 1, col: 2};
definition.set_start(Some(new_start)).expect("set_start() failed");Sourcepub fn get_finish(&self) -> Option<MazePoint>
pub fn get_finish(&self) -> Option<MazePoint>
Locates the finishing position within the maze definition (if any)
§Returns
The finishing position, else none
§Examples
Locate the finishing position in a 2 row x 3 column definition
use data_model::MazeDefinition;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let definition = MazeDefinition::from_vec(grid);
match definition.get_finish() {
Some(finish) => {
println!("Finish found at point {}", finish);
},
None => {
println!("Finish not found");
}
};Sourcepub fn set_finish(&mut self, new_finish: Option<MazePoint>) -> Result<(), Error>
pub fn set_finish(&mut self, new_finish: Option<MazePoint>) -> Result<(), Error>
Sets the finishing position within the maze definition (if any)
§Returns
This function will return an error in the following situations:
- If the new finishing position is out of range
§Examples
Reset the finishing position in a 2 row x 3 column definition
use data_model::MazeDefinition;
use data_model::MazePoint;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let mut definition = MazeDefinition::from_vec(grid);
let new_finish = MazePoint {row: 0, col: 2};
definition.set_start(Some(new_finish)).expect("new_finish() failed");Sourcepub fn set_value(
&mut self,
from: MazePoint,
to: MazePoint,
value: char,
) -> Result<(), Error>
pub fn set_value( &mut self, from: MazePoint, to: MazePoint, value: char, ) -> Result<(), Error>
Modify the value of each cell in a given region of the definition instance
§Arguments
from- Starting point of cell region to modifyto- Ending point of cell region to modifyvalue- Value to set. Must be either'W'(wall) or' '(empty).
§Returns
This function will return an error in the following situations:
- If the target points are out of range
- if the character value is invalid
§Examples
Create a maze definition with 5 rows and 4 columns, then set the central region (1,1) to (3, 2) to be a wall and then print it
use data_model::MazeCellState;
use data_model::MazeDefinition;
use data_model::MazePoint;
let mut definition = MazeDefinition::new(5, 4);
let from = MazePoint { row: 1, col: 1, };
let to = MazePoint { row: 3, col: 2, };
definition.set_value( from, to, 'W').expect("set_value() failed");
println!("{:?}", definition.to_display_chars());Trait Implementations§
Source§impl Clone for MazeDefinition
impl Clone for MazeDefinition
Source§fn clone(&self) -> MazeDefinition
fn clone(&self) -> MazeDefinition
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more