Skip to content

Commit

Permalink
added clippy pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienPensart committed Jan 21, 2024
1 parent 7b65949 commit 5dd625e
Show file tree
Hide file tree
Showing 19 changed files with 331 additions and 264 deletions.
309 changes: 166 additions & 143 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ thiserror = "1.0"
rand = "0.8"
rand_distr = "0.4.3"
derive_more = "0.99.17"
derive-new = "0.5.9"
itertools = "0.11"
derive-new = "0.6.0"
itertools = "0.12"
strum = { version = "0.25", features = ["derive"] }
indoc = "2.0"
regex = "1"
colored = "2.0.0"
array-init = "2.0.1"
ordered-float = "3.2.0"
ordered-float = "4.2.0"
lazy-regex = "3.1.0"

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
enum_glob_use = "deny"
pedantic = "deny"
nursery = "deny"
unwrap_used = "deny"
must_use_candidate = "allow"
missing_errors_doc = "allow"
2 changes: 2 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
cognitive-complexity-threshold = 40
max-struct-bools = 100
too-many-lines-threshold = 300
4 changes: 2 additions & 2 deletions src/card.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::normal::Normal;
use crate::points::HasPoints;
use crate::points::Points;
use crate::suit::Suit;
use crate::suit_value::SuitValue;
use crate::traits::{Discardable, Power, Representation};
Expand All @@ -23,7 +23,7 @@ impl fmt::Display for Card {
}
}

impl HasPoints for Card {
impl Points for Card {
fn points(&self) -> OrderedFloat<f64> {
match self {
Self::Trump(v) => v.points(),
Expand Down
6 changes: 6 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use ordered_float::OrderedFloat;

pub const MAX_CARDS: usize = 78;
pub const BASE_CONTRACT_POINTS: OrderedFloat<f64> = OrderedFloat(25.0);
pub const MAX_POINTS: OrderedFloat<f64> = OrderedFloat(91.0);
pub const MAX_POINTS_WITHOUT_FOOL: OrderedFloat<f64> = OrderedFloat(87.0);
6 changes: 4 additions & 2 deletions src/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use std::fmt;
use strum::IntoEnumIterator;

use crate::card::Card;
use crate::constants::{MAX_CARDS, MAX_POINTS, MAX_POINTS_WITHOUT_FOOL};
use crate::errors::TarotErrorKind;
use crate::points::{HasPoints, MAX_CARDS, MAX_POINTS, MAX_POINTS_WITHOUT_FOOL};
use crate::points::Points;
use crate::suit::Suit;
use crate::suit_value::SuitValue;
use crate::traits::{Discardable, Representation};
Expand Down Expand Up @@ -44,7 +45,7 @@ impl fmt::Display for Deck {
}
}

impl HasPoints for Deck {
impl Points for Deck {
fn points(&self) -> OrderedFloat<f64> {
// RULE: if a slam is occuring and player has only fool or everyting except fool, fool = 4 points
if self.len() == MAX_CARDS - 1 && !self.has_fool() {
Expand Down Expand Up @@ -180,6 +181,7 @@ impl Deck {
Card::Trump(_) => false,
})
}
#[must_use]
pub fn give(&mut self, size: usize) -> Self {
Self(self.0.drain(0..size).collect())
}
Expand Down
4 changes: 3 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub enum TarotErrorKind {
#[error("Invalid deck : {0}")]
InvalidDeck(Deck),
#[error("Invalid case")]
InvalidCase,
InvalidCase(String),
// #[error("Impossible case, taker cannot have all kings, queens, knights, jacks")]
// AllSuitsError,
#[error("Sum of score is not zero")]
InvalidScores(String),
#[error("Invalid number of oudlers : {0}")]
Expand Down
20 changes: 10 additions & 10 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use array_init::array_init;
use array_init::{array_init, try_array_init};
use ordered_float::OrderedFloat;
use std::fmt;

Expand All @@ -18,7 +18,7 @@ pub struct Game<const MODE: usize> {
dealer: usize,
}

pub fn launch_game(mode: Mode, options: Options, deals: u16) -> Result<(), TarotErrorKind> {
pub fn launch(mode: Mode, options: Options, deals: u16) -> Result<(), TarotErrorKind> {
if mode == Mode::Three {
Game::<{ Mode::Three.players() }>::new(options)?.start(deals)?;
return Ok(());
Expand All @@ -35,7 +35,7 @@ pub fn launch_game(mode: Mode, options: Options, deals: u16) -> Result<(), Tarot
impl<const MODE: usize> fmt::Display for Game<MODE> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Players : ")?;
for player in self.players.iter() {
for player in &self.players {
writeln!(f, "\t{player}")?;
}
Ok(())
Expand All @@ -45,12 +45,12 @@ impl<const MODE: usize> fmt::Display for Game<MODE> {
impl<const MODE: usize> Game<MODE> {
pub fn new(options: Options) -> Result<Self, TarotErrorKind> {
let mode: Mode = MODE.try_into()?;
let players: [Player; MODE] = array_init(|i| {
let name = mode.player_name(i);
let players: [Player; MODE] = try_array_init(|i| -> Result<Player, TarotErrorKind> {
let name = mode.player_name(i)?;
let random = options.test || name != "South";
let player_options = Options { random, ..options };
Player::new(name.to_string(), mode, player_options)
});
Ok(Player::new(name.to_string(), mode, player_options))
})?;
Ok(Self {
players,
mode,
Expand Down Expand Up @@ -115,12 +115,12 @@ impl<const MODE: usize> Game<MODE> {
let mut new_deck = Deck::random();
let mut dog = new_deck.give(self.mode.dog_size());
dog.sort();
for player in players_in_game.iter_mut() {
for player in &mut players_in_game {
let buffer = new_deck.give(self.mode.cards_per_player());
player.extend_hand(&buffer);
}

for player in players_in_game.iter() {
for player in &players_in_game {
if player.petit_sec() {
if !self.options.quiet {
dbg!("Petit sec, cancel the game");
Expand Down Expand Up @@ -161,6 +161,6 @@ fn game_tests() {
attack: false,
};
for mode in Mode::iter() {
launch_game(mode, options, 1).unwrap();
assert_eq!(launch(mode, options, 1), Ok(()));
}
}
9 changes: 7 additions & 2 deletions src/game_distributed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ impl<'a, const MODE: usize> GameDistributed<'a, MODE> {
players_in_game: [PlayerInGame; MODE],
options: Options,
) -> Self {
Self { game, options, dog, players_in_game }
Self {
game,
options,
dog,
players_in_game,
}
}
pub fn game(&mut self) -> &mut Game<MODE> {
self.game
Expand Down Expand Up @@ -110,7 +115,7 @@ impl<'a, const MODE: usize> GameDistributed<'a, MODE> {
self.rotate_at(slammer);
}

let callee = self.players_in_game[taker_index].call();
let callee = self.players_in_game[taker_index].call()?;
for (current_player_index, current_player) in self.players_in_game.iter_mut().enumerate() {
current_player.set_callee(callee);
current_player.set_team(Team::Defense);
Expand Down
69 changes: 38 additions & 31 deletions src/game_started.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use derive_new::new;
use ordered_float::OrderedFloat;
use std::fmt;

use crate::constants::{BASE_CONTRACT_POINTS, MAX_CARDS};
use crate::contract::Contract;
use crate::errors::TarotErrorKind;
use crate::game_distributed::GameDistributed;
use crate::helpers::wait_input;
// use crate::helpers::wait_input;
use crate::mode::Mode;
use crate::options::Options;
use crate::player::Player;
use crate::player_in_game::PlayerInGame;
use crate::points::{HasPoints, BASE_CONTRACT_POINTS, MAX_CARDS};
use crate::points::Points;
use crate::role::Role;
use crate::team::Team;
use crate::turn::Turn;
Expand Down Expand Up @@ -84,10 +85,12 @@ impl<'a, const MODE: usize> GameStarted<'a, MODE> {
println!("Current player {current_player_name} (index : {current_player_index})");
}
let &Some(team) = current_player_in_game.team() else {
return Err(TarotErrorKind::NoTeamForPlayer(current_player.name().to_string()));
return Err(TarotErrorKind::NoTeamForPlayer(
current_player.name().to_string(),
));
};

let card = current_player_in_game.play_card(current_player, &mut turn)?;
let card = current_player_in_game.play_card(current_player, &turn)?;
if card.is_fool() {
if current_player_in_game.last_turn() {
// RULE: exception in the last turn, the fool is in game and can be lost
Expand Down Expand Up @@ -172,7 +175,9 @@ impl<'a, const MODE: usize> GameStarted<'a, MODE> {
}

let Some(master_player_team) = master_player_in_game.team() else {
return Err(TarotErrorKind::NoTeamForPlayer(master_player_name.to_string()));
return Err(TarotErrorKind::NoTeamForPlayer(
master_player_name.to_string(),
));
};

let turn_cards = turn.take_cards_except_fool();
Expand All @@ -184,7 +189,7 @@ impl<'a, const MODE: usize> GameStarted<'a, MODE> {
"{master_player_name} (team: {master_player_team}) has Petit in last turn (Petit au bout) : +10 points",
);
}
wait_input();
// wait_input();
Some(*master_player_team)
} else {
None
Expand All @@ -203,8 +208,11 @@ impl<'a, const MODE: usize> GameStarted<'a, MODE> {
self.defense_cards += defense_cards;
Ok(())
}

#[allow(clippy::manual_assert)]
pub fn count_points(&mut self) -> Result<(), TarotErrorKind> {
let mut ally_index: Option<usize> = None;
#[allow(clippy::collection_is_never_read)]
let mut attack: Vec<usize> = Vec::new();
let mut defense: Vec<usize> = Vec::new();
let mut owning_card_player_index: Option<usize> = None;
Expand Down Expand Up @@ -232,7 +240,7 @@ impl<'a, const MODE: usize> GameStarted<'a, MODE> {
attack.push(current_player_index);
}
Some(Role::Ally) => {
assert!(ally_index.is_none());
// assert!(ally_index.is_none());
ally_index = Some(current_player_index);
attack.push(current_player_index);
}
Expand All @@ -246,43 +254,42 @@ impl<'a, const MODE: usize> GameStarted<'a, MODE> {
}
}
}
match self.game_distributed.game().mode() {
Mode::Three => {
assert_eq!(defense.len(), 2);
assert_eq!(attack.len(), 1);
},
Mode::Four => {
assert_eq!(defense.len(), 3);
assert_eq!(attack.len(), 1);
},
Mode::Five => {
if ally_index.is_some() {
assert_eq!(defense.len(), 3);
assert_eq!(attack.len(), 2);
} else {
assert_eq!(defense.len(), 4);
assert_eq!(attack.len(), 1);
}
}
};
// match self.game_distributed.game().mode() {
// Mode::Three => {
// assert_eq!(defense.len(), 2);
// assert_eq!(attack.len(), 1);
// }
// Mode::Four => {
// assert_eq!(defense.len(), 3);
// assert_eq!(attack.len(), 1);
// }
// Mode::Five => {
// if ally_index.is_some() {
// assert_eq!(defense.len(), 3);
// assert_eq!(attack.len(), 2);
// } else {
// assert_eq!(defense.len(), 4);
// assert_eq!(attack.len(), 1);
// }
// }
// };

// give a low card if someone owe a card to someone else
if let (Some(owning_card_player_index), Some(missing_card_player_index)) =
(owning_card_player_index, missing_card_player_index)
{
let (players, players_in_game) = self.players_and_their_game_mut();
let owning_card_player_name = players[owning_card_player_index].name();
let low_card = players_in_game[owning_card_player_index].give_low();
if let Some(low_card) = low_card {
players_in_game[owning_card_player_index].give_low().map_or_else(|| if !quiet {
println!("Player {owning_card_player_name} cannot give a low card");
}, |low_card| {
let missing_card_player_name = players[missing_card_player_index].name();
let missing_card_player_in_game = &mut players_in_game[missing_card_player_index];
missing_card_player_in_game.push_owned(low_card);
if !quiet {
println!("Player {owning_card_player_name} own a card to {missing_card_player_name}, giving a {low_card} in exchange");
}
} else if !quiet {
println!("Player {owning_card_player_name} cannot give a low card");
}
});
}

let taker_index = self.taker_index;
Expand Down
4 changes: 2 additions & 2 deletions src/handle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::points::HasPoints;
use crate::points::Points;
use ordered_float::OrderedFloat;
use strum::{Display, EnumIter};

Expand All @@ -12,7 +12,7 @@ pub enum Handle {
Triple,
}

impl HasPoints for Handle {
impl Points for Handle {
fn points(&self) -> OrderedFloat<f64> {
OrderedFloat(match self {
Self::Refused => 0.0,
Expand Down
10 changes: 5 additions & 5 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub fn read_index() -> usize {
}
}

pub fn wait_input() {
use std::io::prelude::*;
let mut stdin = io::stdin();
let _ = stdin.read(&mut [0u8]).unwrap();
}
// pub fn wait_input() {
// use std::io::prelude::*;
// let mut stdin = io::stdin();
// let _ = stdin.read(&mut [0u8]).unwrap();
// }

#[must_use]
pub fn binomial(mut n: usize, mut k: usize) -> usize {
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::thread;
use strum::IntoEnumIterator;

pub mod card;
pub mod constants;
pub mod contract;
pub mod deck;
pub mod errors;
Expand All @@ -28,7 +29,7 @@ pub mod traits;
pub mod trump;
pub mod turn;

use crate::game::launch_game;
use crate::game::launch;
use crate::mode::Mode;
use crate::options::Options;

Expand Down Expand Up @@ -88,7 +89,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
children.push(thread::spawn(move || {
println!("Spawned thread {:?}", thread::current());
for mode in Mode::iter().cycle() {
let result = launch_game(mode, options, opt.deals);
let result = launch(mode, options, opt.deals);
if let Err(e) = result {
eprintln!("{:?} : {}", thread::current(), e);
}
Expand All @@ -100,7 +101,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
}
} else {
let mode = Mode::from_str(&opt.players)?;
let result = launch_game(mode, options, opt.deals);
let result = launch(mode, options, opt.deals);
if let Err(e) = result {
eprintln!("{e}");
};
Expand Down
Loading

0 comments on commit 5dd625e

Please sign in to comment.