ran clippy

This commit is contained in:
JOLIMAITRE Matthieu 2022-09-03 02:39:45 +02:00
parent 5c5e6357ad
commit 784a5d55a5
4 changed files with 17 additions and 23 deletions

View file

@ -12,10 +12,10 @@ pub enum ControllerParam {
impl Display for ControllerParam { impl Display for ControllerParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self { f.write_str(match *self {
&ControllerParam::Player => "player", ControllerParam::Player => "player",
&ControllerParam::Random => "random", ControllerParam::Random => "random",
&ControllerParam::Simulated => "simulated", ControllerParam::Simulated => "simulated",
}) })
} }
} }

View file

@ -110,7 +110,7 @@ impl Game {
} }
let potential_count = potentials.len() as f32; let potential_count = potentials.len() as f32;
if potential_count == 0. { if potential_count == 0. {
return Err(GameError::GridIsFull.into()); return Err(GameError::GridIsFull);
} }
let random = rand::random::<f32>() * potential_count; let random = rand::random::<f32>() * potential_count;
let index = random.floor() as usize; let index = random.floor() as usize;
@ -160,7 +160,7 @@ impl Game {
direction: (isize, isize), direction: (isize, isize),
tile_pos: (usize, usize), tile_pos: (usize, usize),
) -> usize { ) -> usize {
if self.board.get(tile_pos.clone()).unwrap().is_empty() { if self.board.get(tile_pos).unwrap().is_empty() {
0 0
} else { } else {
let mut displacement = Displacement::new(&mut self.board, tile_pos, direction); 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 { 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(); let current_value = self.grid.get_val(current_pos).unwrap();
if let Some(next_pos) = self.get_next_pos() { if let Some(next_pos) = self.get_next_pos() {
match self.grid.get_val(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)> { fn get_next_pos(&self) -> Option<(usize, usize)> {
let (current_x, current_y) = self.position.clone(); let (current_x, current_y) = self.position;
let (dx, dy) = self.direction.clone(); let (dx, dy) = self.direction;
if would_overflow(current_x, dx, self.grid.size() - 1) if would_overflow(current_x, dx, self.grid.size() - 1)
|| would_overflow(current_y, dy, self.grid.size() - 1) || would_overflow(current_y, dy, self.grid.size() - 1)
{ {

View file

@ -12,15 +12,11 @@ impl Tile {
} }
pub fn value(&self) -> Option<usize> { pub fn value(&self) -> Option<usize> {
self.value.clone() self.value
} }
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
if let Some(_) = self.value { self.value.is_none()
false
} else {
true
}
} }
} }
@ -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 /// 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)) { 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[dst_y][dst_x] = src;
self.tiles[src_y][src_x] = Tile::new_empty(); self.tiles[src_y][src_x] = Tile::new_empty();
} }
@ -101,12 +97,11 @@ impl Grid {
pub fn biggest_value(&self) -> usize { pub fn biggest_value(&self) -> usize {
self.tiles() self.tiles()
.iter() .iter()
.map(|row| { .filter_map(|row| {
row.iter() row.iter()
.filter_map(|tile| tile.value()) .filter_map(|tile| tile.value())
.reduce(|a, b| a.max(b)) .reduce(|a, b| a.max(b))
}) })
.filter_map(|value| value)
.reduce(|a, b| a.max(b)) .reduce(|a, b| a.max(b))
.unwrap_or(0) .unwrap_or(0)
} }

View file

@ -34,14 +34,13 @@ impl TileDisplayer {
} }
fn display_number(value: usize) -> String { fn display_number(value: usize) -> String {
let result = [ [
// number tile // number tile
"┌─ ─┐", "┌─ ─┐",
&Self::pad_both(value.to_string(), Self::TILE_LENGTH), &Self::pad_both(value.to_string(), Self::TILE_LENGTH),
"└─ ─┘", "└─ ─┘",
] ]
.join("\n"); .join("\n")
result
} }
fn pad_both(text: String, length: usize) -> String { fn pad_both(text: String, length: usize) -> String {
@ -62,7 +61,7 @@ impl TileDisplayer {
let reset_code = color::Bg(color::Reset); let reset_code = color::Bg(color::Reset);
let text = text let text = text
.split("\n") .split('\n')
.map(|line| format!("{color_code}{line}{reset_code}")) .map(|line| format!("{color_code}{line}{reset_code}"))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\n"); .join("\n");
@ -140,7 +139,7 @@ impl GridDisplayer {
// join lines of [`row_lines`] // join lines of [`row_lines`]
let row_lines = row_lines let row_lines = row_lines
.iter_mut() .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("")) .map(|line| [Self::DISPLAY_CHAR[10], &line, Self::DISPLAY_CHAR[10]].join(""))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
row_lines.join("\n") row_lines.join("\n")