diff --git a/rs48/src/main.rs b/rs48/src/main.rs index 79e4371..f450caf 100644 --- a/rs48/src/main.rs +++ b/rs48/src/main.rs @@ -12,10 +12,10 @@ pub enum ControllerParam { impl Display for ControllerParam { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(match self { - &ControllerParam::Player => "player", - &ControllerParam::Random => "random", - &ControllerParam::Simulated => "simulated", + f.write_str(match *self { + ControllerParam::Player => "player", + ControllerParam::Random => "random", + ControllerParam::Simulated => "simulated", }) } } diff --git a/rs48_lib/src/game.rs b/rs48_lib/src/game.rs index e417364..c797838 100644 --- a/rs48_lib/src/game.rs +++ b/rs48_lib/src/game.rs @@ -110,7 +110,7 @@ impl Game { } let potential_count = potentials.len() as f32; if potential_count == 0. { - return Err(GameError::GridIsFull.into()); + return Err(GameError::GridIsFull); } let random = rand::random::() * potential_count; let index = random.floor() as usize; @@ -160,7 +160,7 @@ impl Game { direction: (isize, isize), tile_pos: (usize, usize), ) -> usize { - if self.board.get(tile_pos.clone()).unwrap().is_empty() { + if self.board.get(tile_pos).unwrap().is_empty() { 0 } else { let mut displacement = Displacement::new(&mut self.board, tile_pos, direction); @@ -202,7 +202,7 @@ impl<'g> Displacement<'g> { } fn move_once(&mut self) -> bool { - let current_pos = self.position.clone(); + let current_pos = self.position; let current_value = self.grid.get_val(current_pos).unwrap(); if let Some(next_pos) = self.get_next_pos() { match self.grid.get_val(next_pos) { @@ -225,8 +225,8 @@ impl<'g> Displacement<'g> { } fn get_next_pos(&self) -> Option<(usize, usize)> { - let (current_x, current_y) = self.position.clone(); - let (dx, dy) = self.direction.clone(); + let (current_x, current_y) = self.position; + let (dx, dy) = self.direction; if would_overflow(current_x, dx, self.grid.size() - 1) || would_overflow(current_y, dy, self.grid.size() - 1) { diff --git a/rs48_lib/src/grid.rs b/rs48_lib/src/grid.rs index 53263bc..357b8e2 100644 --- a/rs48_lib/src/grid.rs +++ b/rs48_lib/src/grid.rs @@ -12,15 +12,11 @@ impl Tile { } pub fn value(&self) -> Option { - self.value.clone() + self.value } pub fn is_empty(&self) -> bool { - if let Some(_) = self.value { - false - } else { - true - } + self.value.is_none() } } @@ -90,7 +86,7 @@ impl Grid { /// move a tile over another one, replace the previously occupied place by an empty tile and overrides the destination /// pub fn move_tile(&mut self, (src_x, src_y): (usize, usize), (dst_x, dst_y): (usize, usize)) { - let src = self.tiles[src_y][src_x].clone(); + let src = self.tiles[src_y][src_x]; self.tiles[dst_y][dst_x] = src; self.tiles[src_y][src_x] = Tile::new_empty(); } @@ -101,12 +97,11 @@ impl Grid { pub fn biggest_value(&self) -> usize { self.tiles() .iter() - .map(|row| { + .filter_map(|row| { row.iter() .filter_map(|tile| tile.value()) .reduce(|a, b| a.max(b)) }) - .filter_map(|value| value) .reduce(|a, b| a.max(b)) .unwrap_or(0) } diff --git a/rs48_lib/src/grid_displayer.rs b/rs48_lib/src/grid_displayer.rs index c944446..6540005 100644 --- a/rs48_lib/src/grid_displayer.rs +++ b/rs48_lib/src/grid_displayer.rs @@ -34,14 +34,13 @@ impl TileDisplayer { } fn display_number(value: usize) -> String { - let result = [ + [ // number tile "┌─ ─┐", &Self::pad_both(value.to_string(), Self::TILE_LENGTH), "└─ ─┘", ] - .join("\n"); - result + .join("\n") } fn pad_both(text: String, length: usize) -> String { @@ -62,7 +61,7 @@ impl TileDisplayer { let reset_code = color::Bg(color::Reset); let text = text - .split("\n") + .split('\n') .map(|line| format!("{color_code}{line}{reset_code}")) .collect::>() .join("\n"); @@ -140,7 +139,7 @@ impl GridDisplayer { // join lines of [`row_lines`] let row_lines = row_lines .iter_mut() - .map(|line_parts| line_parts.join(Self::DISPLAY_CHAR[10]).to_string()) + .map(|line_parts| line_parts.join(Self::DISPLAY_CHAR[10])) .map(|line| [Self::DISPLAY_CHAR[10], &line, Self::DISPLAY_CHAR[10]].join("")) .collect::>(); row_lines.join("\n")