pub struct Maze {
pub id: String,
pub name: String,
pub definition: MazeDefinition,
}Expand description
Represents a maze
Fields§
§id: String§name: String§definition: MazeDefinitionMazeDefinition, containing the layout of the maze
Implementations§
Source§impl Maze
impl Maze
Sourcepub fn new(definition: MazeDefinition) -> Maze
pub fn new(definition: MazeDefinition) -> Maze
Creates a new maze instance with the given definition
§Arguments
definition- Maze definition
§Returns
A new maze 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;
use data_model::Maze;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let def = MazeDefinition::from_vec(grid);
let maze = Maze::new(def);
assert_eq!(maze.definition.row_count(), 2);
assert_eq!(maze.definition.col_count(), 3);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 2 rows and 3 columns, verify its dimensions, reset it and then confirm it is empty
use data_model::Maze;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let mut maze = Maze::from_vec(grid);
assert_eq!(maze.definition.row_count(), 2);
assert_eq!(maze.definition.col_count(), 3);
maze.reset();
assert_eq!(maze.definition.is_empty(), true);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 maze instance
§Examples
Create a 2 row x 3 column definition with a start, finish and a wall in the last column
use data_model::Maze;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let maze = Maze::from_vec(grid);
assert_eq!(maze.definition.row_count(), 2);
assert_eq!(maze.definition.col_count(), 3);Sourcepub fn to_json(&self) -> Result<String, Error>
pub fn to_json(&self) -> Result<String, Error>
Generates the JSON string representation for the maze
§Returns
JSON string representing the maze definition
§Examples
Create a 2 row x 3 column definition with a start, finish and a wall in the last column and then convert it to JSON and print it
use data_model::Maze;
let grid: Vec<Vec<char>> = vec![
vec!['S', ' ', 'W'],
vec![' ', 'F', 'W']
];
let maze = Maze::from_vec(grid);
assert_eq!(maze.definition.row_count(), 2);
assert_eq!(maze.definition.col_count(), 3);
match maze.to_json() {
Ok(json) => {
println!("JSON: {}", json);
}
Err(error) => {
panic!(
"failed to convert maze to JSON => {}",
error
);
}
}Sourcepub fn from_json(&mut self, json: &str) -> Result<(), Error>
pub fn from_json(&mut self, json: &str) -> Result<(), Error>
Initializes a maze instance by reading the JSON string content provided
§Returns
This function will return an error if the JSON could not be read
§Examples
Create an empty maze and then reinitialize it from a JSON string definition containing 2 rows and 3 columns
use data_model::MazeDefinition;
use data_model::Maze;
let mut maze = Maze::new(MazeDefinition::new(0, 0));
let json = r#"{"id":"maze_id","name":"maze_name", "definition":{"grid":[[" ","W"," "],[" "," ","W"]]}}"#;
match maze.from_json(json) {
Ok(()) => {
println!(
"JSON successfully read into Maze => new rows = {}, new columns = {}",
maze.definition.row_count(),
maze.definition.col_count()
);
}
Err(error) => {
panic!(
"failed to read JSON into maze => {}",
error
);
}
}