Skip to content

Commit

Permalink
Compile on rust 1.61, resolve warnings, fix some lints (#12)
Browse files Browse the repository at this point in the history
* Fix getopts version

The pinned version does not compile anymore because of mutable aliasing:
* rust-lang/getopts#61
* rust-lang/getopts#110

This was achieved by temporarily setting the getopts version to "0.2.21"
in Cargo.toml, and running `cargo check`.

Note that this also converts the Cargo.lock to a new format.

* Fix warning: Instead of deprecated macro try!, use question mark operator

* Fix warning: Avoid anonymous parameters

* Fix warning: Use dyn on trait objects

* Fix warning: Avoid unneeded mutability

* Fix warning: Avoid redundant format in panic or assert

* Fix lint: Avoid redundant field names in initializers

* Fix lint: Avoid redundant clone

* Fix lint: Avoid literal cast

* Fix lint: Collapse if/else where applicable

I left some if/else branches in place, if there was a certain symmetry between the branches.

* Fix lint: Avoid needless borrow

I left some if/else branches in place, if there was a certain symmetry between the branches.

* Fix lint: Use cloned instead of custom closure

* Fix lint: Avoid unneeded trait bound

* Fix lint: Avoid unneeded trait bound (2) avoid redundant clone

* Fix lint: Use &[T] instead of &Vec<T>

* Fix lint: Avoid & on each pattern

* Fix lint: Avoid manual assign

* Fix lint: Use implicit return

* Fix lint: Merge if/else branches with same value

I left one complicated branch in place.

* Fix lint: Use is_empty instead of comparing len against 0

* Fix lint: Use sum instead of fold

* Fix lint: Avoid clone on Copy types
  • Loading branch information
phimuemue committed Jul 5, 2022
1 parent cf65b0e commit c9b21fa
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 204 deletions.
45 changes: 28 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 61 additions & 65 deletions src/game.rs
Expand Up @@ -19,7 +19,7 @@ pub fn get_count_for_value(value: Value) -> u32 {
1 => 3,
2 | 3 | 4 => 2,
5 => 1,
_ => { panic!(format!("Unexpected value: {}", value)); }
_ => { panic!("Unexpected value: {}", value); }
}
}

Expand All @@ -30,7 +30,7 @@ pub struct Card {
}
impl Card {
pub fn new(color: Color, value: Value) -> Card {
Card { color: color, value: value }
Card { color, value }
}
}
impl fmt::Display for Card {
Expand All @@ -57,7 +57,7 @@ impl CardCounts {
}
}
CardCounts {
counts: counts,
counts,
}
}

Expand All @@ -78,20 +78,20 @@ impl CardCounts {
impl fmt::Display for CardCounts {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for &color in COLORS.iter() {
try!(f.write_str(&format!(
f.write_str(&format!(
"{}: ", color,
)));
))?;
for &value in VALUES.iter() {
let count = self.get_count(&Card::new(color, value));
let total = get_count_for_value(value);
try!(f.write_str(&format!(
f.write_str(&format!(
"{}/{} {}s", count, total, value
)));
))?;
if value != FINAL_VALUE {
try!(f.write_str(", "));
f.write_str(", ")?;
}
}
try!(f.write_str("\n"));
f.write_str("\n")?;
}
Ok(())
}
Expand Down Expand Up @@ -127,9 +127,9 @@ impl Discard {
}
impl fmt::Display for Discard {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// try!(f.write_str(&format!(
// f.write_str(&format!(
// "{}", self.cards,
// )));
// ))?;
write!(f, "{}", self.counts)
}
}
Expand All @@ -145,7 +145,7 @@ pub struct Firework {
impl Firework {
pub fn new(color: Color) -> Firework {
Firework {
color: color,
color,
top: 0,
}
}
Expand Down Expand Up @@ -192,8 +192,8 @@ pub enum Hinted {
impl fmt::Display for Hinted {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Hinted::Color(color) => { write!(f, "{}", color) }
&Hinted::Value(value) => { write!(f, "{}", value) }
Hinted::Color(color) => { write!(f, "{}", color) }
Hinted::Value(value) => { write!(f, "{}", value) }
}
}
}
Expand Down Expand Up @@ -274,9 +274,9 @@ impl BoardState {
}).collect::<FnvHashMap<_, _>>();

BoardState {
deck_size: deck_size,
deck_size,
total_cards: deck_size,
fireworks: fireworks,
fireworks,
discard: Discard::new(),
num_players: opts.num_players,
hand_size: opts.hand_size,
Expand Down Expand Up @@ -332,7 +332,7 @@ impl BoardState {
return value - 1;
}
}
return FINAL_VALUE;
FINAL_VALUE
}

// is never going to play, based on discard + fireworks
Expand All @@ -357,24 +357,20 @@ impl BoardState {
true
} else {
let needed = firework.needed_value().unwrap();
if card.value < needed {
if card.value < needed || card.value > self.highest_attainable(card.color) {
true
} else {
if card.value > self.highest_attainable(card.color) {
true
} else {
self.discard.remaining(&card) != 1
}
self.discard.remaining(card) != 1
}
}
}

pub fn get_players(&self) -> Range<Player> {
(0..self.num_players)
0..self.num_players
}

pub fn score(&self) -> Score {
self.fireworks.iter().map(|(_, firework)| firework.score()).fold(0, |a, b| a + b)
self.fireworks.iter().map(|(_, firework)| firework.score()).sum()
}

pub fn discard_size(&self) -> u32 {
Expand All @@ -395,35 +391,35 @@ impl BoardState {
impl fmt::Display for BoardState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_over() {
try!(f.write_str(&format!(
f.write_str(&format!(
"Turn {} (GAME ENDED):\n", self.turn
)));
))?;
} else {
try!(f.write_str(&format!(
f.write_str(&format!(
"Turn {} (Player {}'s turn):\n", self.turn, self.player
)));
))?;
}

try!(f.write_str(&format!(
f.write_str(&format!(
"{} cards remaining in deck\n", self.deck_size
)));
))?;
if self.deck_size == 0 {
try!(f.write_str(&format!(
f.write_str(&format!(
"Deck is empty. {} turns remaining in game\n", self.deckless_turns_remaining
)));
))?;
}
try!(f.write_str(&format!(
f.write_str(&format!(
"{}/{} hints remaining\n", self.hints_remaining, self.hints_total
)));
try!(f.write_str(&format!(
))?;
f.write_str(&format!(
"{}/{} lives remaining\n", self.lives_remaining, self.lives_total
)));
try!(f.write_str("Fireworks:\n"));
))?;
f.write_str("Fireworks:\n")?;
for &color in COLORS.iter() {
try!(f.write_str(&format!(" {}\n", self.get_firework(color))));
f.write_str(&format!(" {}\n", self.get_firework(color)))?;
}
try!(f.write_str("Discard:\n"));
try!(f.write_str(&format!("{}\n", self.discard)));
f.write_str("Discard:\n")?;
f.write_str(&format!("{}\n", self.discard))?;

Ok(())
}
Expand All @@ -432,7 +428,7 @@ impl fmt::Display for BoardState {
// complete game view of a given player
pub trait GameView {
fn me(&self) -> Player;
fn get_hand(&self, &Player) -> &Cards;
fn get_hand(&self, player: &Player) -> &Cards;
fn get_board(&self) -> &BoardState;

fn my_hand_size(&self) -> usize;
Expand All @@ -459,13 +455,13 @@ pub trait GameView {

fn can_see(&self, card: &Card) -> bool {
self.get_other_players().iter().any(|player| {
self.has_card(&player, card)
self.has_card(player, card)
})
}

fn someone_else_can_play(&self) -> bool {
self.get_other_players().iter().any(|player| {
self.get_hand(&player).iter().any(|card| {
self.get_hand(player).iter().any(|card| {
self.get_board().is_playable(card)
})
})
Expand Down Expand Up @@ -518,9 +514,9 @@ impl OwnedGameView {
}).collect::<FnvHashMap<_, _>>();

OwnedGameView {
player: borrowed_view.player.clone(),
player: borrowed_view.player,
hand_size: borrowed_view.hand_size,
other_hands: other_hands,
other_hands,
board: (*borrowed_view.board).clone(),
}
}
Expand Down Expand Up @@ -550,22 +546,22 @@ pub struct GameState {
}
impl fmt::Display for GameState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.write_str("\n"));
try!(f.write_str("======\n"));
try!(f.write_str("Hands:\n"));
try!(f.write_str("======\n"));
f.write_str("\n")?;
f.write_str("======\n")?;
f.write_str("Hands:\n")?;
f.write_str("======\n")?;
for player in self.board.get_players() {
let hand = &self.hands.get(&player).unwrap();
try!(f.write_str(&format!("player {}:", player)));
f.write_str(&format!("player {}:", player))?;
for card in hand.iter() {
try!(f.write_str(&format!(" {}", card)));
f.write_str(&format!(" {}", card))?;
}
try!(f.write_str(&"\n"));
f.write_str("\n")?;
}
try!(f.write_str("======\n"));
try!(f.write_str("Board:\n"));
try!(f.write_str("======\n"));
try!(f.write_str(&format!("{}", self.board)));
f.write_str("======\n")?;
f.write_str("Board:\n")?;
f.write_str("======\n")?;
f.write_str(&format!("{}", self.board))?;
Ok(())
}
}
Expand All @@ -585,9 +581,9 @@ impl GameState {
}).collect::<FnvHashMap<_, _>>();

GameState {
hands: hands,
board: board,
deck: deck,
hands,
board,
deck,
}
}

Expand All @@ -612,9 +608,9 @@ impl GameState {
}
}
BorrowedGameView {
player: player,
player,
hand_size: self.hands.get(&player).unwrap().len(),
other_hands: other_hands,
other_hands,
board: &self.board,
}
}
Expand Down Expand Up @@ -646,7 +642,7 @@ impl GameState {
debug!("Hint to player {}, about {}", hint.player, hint.hinted);

assert!(self.board.player != hint.player,
format!("Player {} gave a hint to himself", hint.player));
"Player {} gave a hint to himself", hint.player);

let hand = self.hands.get(&hint.player).unwrap();
let results = match hint.hinted {
Expand Down Expand Up @@ -703,9 +699,9 @@ impl GameState {
}
};
let turn_record = TurnRecord {
player: self.board.player.clone(),
player: self.board.player,
result: turn_result,
choice: choice,
choice,
};
self.board.turn_history.push(turn_record.clone());

Expand Down

0 comments on commit c9b21fa

Please sign in to comment.