From 91fc4c279d5d04ed29a3ad057ee0a8fe1789fc0f Mon Sep 17 00:00:00 2001 From: timotree3 Date: Wed, 11 May 2022 15:21:10 -0400 Subject: [PATCH] Clean up formatting code --- src/game.rs | 48 +++++++++++++++++------------------ src/helpers.rs | 6 ++--- src/simulator.rs | 2 +- src/strategies/information.rs | 4 +-- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/game.rs b/src/game.rs index 8d08dce..49dca76 100644 --- a/src/game.rs +++ b/src/game.rs @@ -78,11 +78,11 @@ impl CardCounts { impl fmt::Display for CardCounts { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { for &color in COLORS.iter() { - f.write_str(&format!("{}: ", color,))?; + write!(f, "{}: ", color)?; for &value in VALUES.iter() { let count = self.get_count(&Card::new(color, value)); let total = get_count_for_value(value); - f.write_str(&format!("{}/{} {}s", count, total, value))?; + write!(f, "{}/{} {}s", count, total, value)?; if value != FINAL_VALUE { f.write_str(", ")?; } @@ -123,9 +123,7 @@ impl Discard { } impl fmt::Display for Discard { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - // f.write_str(&format!( - // "{}", self.cards, - // ))?; + // write!(f, "{}", self.cards)?; write!(f, "{}", self.counts) } } @@ -387,35 +385,35 @@ impl BoardState { impl fmt::Display for BoardState { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.is_over() { - f.write_str(&format!("Turn {} (GAME ENDED):\n", self.turn))?; + writeln!(f, "Turn {} (GAME ENDED):", self.turn)?; } else { - f.write_str(&format!( - "Turn {} (Player {}'s turn):\n", - self.turn, self.player - ))?; + writeln!(f, "Turn {} (Player {}'s turn):", self.turn, self.player)?; } - f.write_str(&format!("{} cards remaining in deck\n", self.deck_size))?; + writeln!(f, "{} cards remaining in deck", self.deck_size)?; if self.deck_size == 0 { - f.write_str(&format!( - "Deck is empty. {} turns remaining in game\n", + writeln!( + f, + "Deck is empty. {} turns remaining in game", self.deckless_turns_remaining - ))?; + )?; } - f.write_str(&format!( - "{}/{} hints remaining\n", + writeln!( + f, + "{}/{} hints remaining", self.hints_remaining, self.hints_total - ))?; - f.write_str(&format!( - "{}/{} lives remaining\n", + )?; + writeln!( + f, + "{}/{} lives remaining", self.lives_remaining, self.lives_total - ))?; + )?; f.write_str("Fireworks:\n")?; for &color in COLORS.iter() { - f.write_str(&format!(" {}\n", self.get_firework(color)))?; + writeln!(f, " {}", self.get_firework(color))?; } f.write_str("Discard:\n")?; - f.write_str(&format!("{}\n", self.discard))?; + writeln!(f, "{}\n", self.discard)?; Ok(()) } @@ -550,16 +548,16 @@ impl fmt::Display for GameState { f.write_str("======\n")?; for player in self.board.get_players() { let hand = &self.hands.get(&player).unwrap(); - f.write_str(&format!("player {}:", player))?; + write!(f, "player {}:", player)?; for card in hand.iter() { - f.write_str(&format!(" {}", card))?; + write!(f, " {}", card)?; } f.write_str("\n")?; } f.write_str("======\n")?; f.write_str("Board:\n")?; f.write_str("======\n")?; - f.write_str(&format!("{}", self.board))?; + write!(f, "{}", self.board)?; Ok(()) } } diff --git a/src/helpers.rs b/src/helpers.rs index ce0358c..9895edd 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,7 +1,7 @@ use std::cmp::Eq; use std::collections::{HashMap, HashSet}; use std::convert::From; -use std::fmt; +use std::fmt::{self, Write}; use std::hash::Hash; use std::ops::{Index, IndexMut}; use std::slice; @@ -268,7 +268,7 @@ impl fmt::Display for SimpleCardInfo { //} for &value in &VALUES { if self.value_info.is_possible(value) { - string.push_str(&format!("{}", value)); + write!(string, "{}", value).unwrap(); } } f.pad(&string) @@ -401,7 +401,7 @@ impl CardInfo for CardPossibilityTable { impl fmt::Display for CardPossibilityTable { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { for (card, weight) in &self.possible { - f.write_str(&format!("{} {}, ", weight, card))?; + write!(f, "{} {}, ", weight, card)?; } Ok(()) } diff --git a/src/simulator.rs b/src/simulator.rs index db0a1b2..71ffc11 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -122,7 +122,7 @@ impl fmt::Display for Histogram { let mut keys = self.hist.keys().collect::>(); keys.sort(); for val in keys { - f.write_str(&format!("\n{}: {}", val, self.get_count(val),))?; + write!(f, "\n{}: {}", val, self.get_count(val))?; } Ok(()) } diff --git a/src/strategies/information.rs b/src/strategies/information.rs index d91b17d..8ab6c24 100644 --- a/src/strategies/information.rs +++ b/src/strategies/information.rs @@ -159,14 +159,14 @@ impl CardPossibilityPartition { n_partitions += 1; } - // let mut s : String = "Partition: |".to_string(); + // let mut s: String = "Partition: |".to_string(); // for i in 0..n_partitions { // for (card, block) in partition.iter() { // if *block == i { // s = s + &format!(" {}", card); // } // } - // s = s + &format!(" |"); + // s.push_str(" |"); // } // debug!("{}", s);