Clean up formatting code

This commit is contained in:
timotree3 2022-05-11 15:21:10 -04:00
parent d65b7616f1
commit 91fc4c279d
4 changed files with 29 additions and 31 deletions

View file

@ -78,11 +78,11 @@ impl CardCounts {
impl fmt::Display for CardCounts { impl fmt::Display for CardCounts {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for &color in COLORS.iter() { for &color in COLORS.iter() {
f.write_str(&format!("{}: ", color,))?; write!(f, "{}: ", color)?;
for &value in VALUES.iter() { for &value in VALUES.iter() {
let count = self.get_count(&Card::new(color, value)); let count = self.get_count(&Card::new(color, value));
let total = get_count_for_value(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 { if value != FINAL_VALUE {
f.write_str(", ")?; f.write_str(", ")?;
} }
@ -123,9 +123,7 @@ impl Discard {
} }
impl fmt::Display for Discard { impl fmt::Display for Discard {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// f.write_str(&format!( // write!(f, "{}", self.cards)?;
// "{}", self.cards,
// ))?;
write!(f, "{}", self.counts) write!(f, "{}", self.counts)
} }
} }
@ -387,35 +385,35 @@ impl BoardState {
impl fmt::Display for BoardState { impl fmt::Display for BoardState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_over() { if self.is_over() {
f.write_str(&format!("Turn {} (GAME ENDED):\n", self.turn))?; writeln!(f, "Turn {} (GAME ENDED):", self.turn)?;
} else { } else {
f.write_str(&format!( writeln!(f, "Turn {} (Player {}'s turn):", self.turn, self.player)?;
"Turn {} (Player {}'s turn):\n",
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 { if self.deck_size == 0 {
f.write_str(&format!( writeln!(
"Deck is empty. {} turns remaining in game\n", f,
"Deck is empty. {} turns remaining in game",
self.deckless_turns_remaining self.deckless_turns_remaining
))?; )?;
} }
f.write_str(&format!( writeln!(
"{}/{} hints remaining\n", f,
"{}/{} hints remaining",
self.hints_remaining, self.hints_total self.hints_remaining, self.hints_total
))?; )?;
f.write_str(&format!( writeln!(
"{}/{} lives remaining\n", f,
"{}/{} lives remaining",
self.lives_remaining, self.lives_total self.lives_remaining, self.lives_total
))?; )?;
f.write_str("Fireworks:\n")?; f.write_str("Fireworks:\n")?;
for &color in COLORS.iter() { 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("Discard:\n")?;
f.write_str(&format!("{}\n", self.discard))?; writeln!(f, "{}\n", self.discard)?;
Ok(()) Ok(())
} }
@ -550,16 +548,16 @@ impl fmt::Display for GameState {
f.write_str("======\n")?; f.write_str("======\n")?;
for player in self.board.get_players() { for player in self.board.get_players() {
let hand = &self.hands.get(&player).unwrap(); let hand = &self.hands.get(&player).unwrap();
f.write_str(&format!("player {}:", player))?; write!(f, "player {}:", player)?;
for card in hand.iter() { for card in hand.iter() {
f.write_str(&format!(" {}", card))?; write!(f, " {}", card)?;
} }
f.write_str("\n")?; f.write_str("\n")?;
} }
f.write_str("======\n")?; f.write_str("======\n")?;
f.write_str("Board:\n")?; f.write_str("Board:\n")?;
f.write_str("======\n")?; f.write_str("======\n")?;
f.write_str(&format!("{}", self.board))?; write!(f, "{}", self.board)?;
Ok(()) Ok(())
} }
} }

View file

@ -1,7 +1,7 @@
use std::cmp::Eq; use std::cmp::Eq;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::convert::From; use std::convert::From;
use std::fmt; use std::fmt::{self, Write};
use std::hash::Hash; use std::hash::Hash;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::slice; use std::slice;
@ -268,7 +268,7 @@ impl fmt::Display for SimpleCardInfo {
//} //}
for &value in &VALUES { for &value in &VALUES {
if self.value_info.is_possible(value) { if self.value_info.is_possible(value) {
string.push_str(&format!("{}", value)); write!(string, "{}", value).unwrap();
} }
} }
f.pad(&string) f.pad(&string)
@ -401,7 +401,7 @@ impl CardInfo for CardPossibilityTable {
impl fmt::Display for CardPossibilityTable { impl fmt::Display for CardPossibilityTable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (card, weight) in &self.possible { for (card, weight) in &self.possible {
f.write_str(&format!("{} {}, ", weight, card))?; write!(f, "{} {}, ", weight, card)?;
} }
Ok(()) Ok(())
} }

View file

@ -122,7 +122,7 @@ impl fmt::Display for Histogram {
let mut keys = self.hist.keys().collect::<Vec<_>>(); let mut keys = self.hist.keys().collect::<Vec<_>>();
keys.sort(); keys.sort();
for val in keys { for val in keys {
f.write_str(&format!("\n{}: {}", val, self.get_count(val),))?; write!(f, "\n{}: {}", val, self.get_count(val))?;
} }
Ok(()) Ok(())
} }

View file

@ -159,14 +159,14 @@ impl CardPossibilityPartition {
n_partitions += 1; n_partitions += 1;
} }
// let mut s : String = "Partition: |".to_string(); // let mut s: String = "Partition: |".to_string();
// for i in 0..n_partitions { // for i in 0..n_partitions {
// for (card, block) in partition.iter() { // for (card, block) in partition.iter() {
// if *block == i { // if *block == i {
// s = s + &format!(" {}", card); // s = s + &format!(" {}", card);
// } // }
// } // }
// s = s + &format!(" |"); // s.push_str(" |");
// } // }
// debug!("{}", s); // debug!("{}", s);