Skip to content

Commit

Permalink
chore: make it more rusty.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlejeune74 committed Feb 19, 2024
1 parent 2b20a41 commit eaab0c3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 65 deletions.
98 changes: 51 additions & 47 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,6 @@ impl Board {
}
}

pub fn load_map(&mut self, map: &str) -> Result<(), StrError> {
let lines: Vec<&str> = map.lines().collect();
if lines.is_empty() {
return Err(StrError::new("Map is empty".to_string()));
} else {
let line = lines[0];
let parts = line.split_whitespace().collect::<Vec<&str>>();
if parts.len() != 3 {
panic!("Height or width missing in map definition.")
}
self.width = FromStr::from_str(parts[0]).unwrap();
self.height = FromStr::from_str(parts[1]).unwrap();
self.diamands = FromStr::from_str(parts[2]).unwrap();
}

if self.height != (lines.len() - 1) as u32 {
return Err(StrError::new(
"Height param and number of lines differ".to_string(),
));
}
// Number of line include the parameter line
(1..(self.height + 1) as usize).for_each(|i| {
let line = lines[i];
let mut cells = Vec::new();
let chars = line.chars().collect::<Vec<char>>();
let mut j: u32 = 0;
let mut _j = 0;
while _j < chars.len() {
let mut cell = Cell::new(j, (i - 1) as u32, chars[_j]);
if cell.has_hidden_diamands() && _j < (chars.len() - 1) {
_j += 1;
cell.set_diamands(chars[_j].to_digit(10).unwrap());
}
cells.push(cell);
j += 1;
_j += 1;
}
// force missing cell description to be a solid Rock.
while j < self.width {
cells.push(Cell::new(j, (i - 1) as u32, 'x'));
j += 1;
}
self.map.push(cells);
});
Ok(())
}

pub fn add_player(&mut self, name: String) -> Result<(), StrError> {
if self.players.contains_key(&name) {
return Err(StrError::new(
Expand Down Expand Up @@ -138,6 +91,57 @@ impl Board {
count
}
}

impl TryFrom<&str> for Board {
type Error = StrError;
fn try_from(map: &str) -> Result<Self, Self::Error> {
let mut board = Board::new();
let lines: Vec<&str> = map.lines().collect();
if lines.is_empty() {
return Err(StrError::new("Map is empty".to_string()));
} else {
let line = lines[0];
let parts = line.split_whitespace().collect::<Vec<&str>>();
if parts.len() != 3 {
panic!("Height or width missing in map definition.")
}
board.width = FromStr::from_str(parts[0]).unwrap();
board.height = FromStr::from_str(parts[1]).unwrap();
board.diamands = FromStr::from_str(parts[2]).unwrap();
}

if board.height != (lines.len() - 1) as u32 {
return Err(StrError::new(
"Height param and number of lines differ".to_string(),
));
}
// Number of line include the parameter line
(1..(board.height + 1) as usize).for_each(|i| {
let line = lines[i];
let mut cells = Vec::new();
let chars = line.chars().collect::<Vec<char>>();
let mut j: u32 = 0;
let mut _j = 0;
while _j < chars.len() {
let mut cell = Cell::new(j, (i - 1) as u32, chars[_j]);
if cell.has_hidden_diamands() && _j < (chars.len() - 1) {
_j += 1;
cell.set_diamands(chars[_j].to_digit(10).unwrap());
}
cells.push(cell);
j += 1;
_j += 1;
}
// force missing cell description to be a solid Rock.
while j < board.width {
cells.push(Cell::new(j, (i - 1) as u32, 'x'));
j += 1;
}
board.map.push(cells);
});
Ok(board)
}
}
impl fmt::Display for Board {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut output = String::new();
Expand Down
31 changes: 13 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,22 @@ x-x-x-x-x-x-x-x-x-x-\n\
--------------------\n";

fn main() {
let mut board = Board::new();

// simulate command `MAP [map.desc]`
match board.load_map(DEFAULT_MAP) {
Err(e) => println!("Error: {}", e),
_ => {
println!("Map loaded");
println!("-------------------------\n");
println!("{}\n", board);
}
}
let mut board = TryInto::<Board>::try_into(DEFAULT_MAP)
.map_err(|e| println!("Error: {}", e))
.unwrap();
println!("Map loaded");
println!("-------------------------\n");
println!("{}\n", board);

// simulate command `PLAYER [NAME]`
match board.add_player("player1".to_string()) {
Err(e) => println!("Error: {}", e),
_ => {
println!("Player added");
println!("-------------------------\n");
println!("{}\n", board);
}
}
board
.add_player("player1".to_string())
.map_err(|e| println!("Error: {}", e))
.unwrap();
println!("Player added");
println!("-------------------------\n");
println!("{}\n", board);

// simulate command `GAME START`
}

0 comments on commit eaab0c3

Please sign in to comment.