Skip to content

Commit

Permalink
UPDATE linting after running clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kalak-io committed Sep 7, 2024
1 parent 71ae809 commit 60b23bc
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/common/bid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ impl Bid {
pub fn compare_bids(bid: &Bids, active_bid: &Bids) -> bool {
match bid {
Bids::Passe => bid == active_bid,
Bids::Petite => [Bids::Passe].contains(&active_bid),
Bids::Garde => [Bids::Passe, Bids::Petite].contains(&active_bid),
Bids::GardeSans => [Bids::Passe, Bids::Petite, Bids::Garde].contains(&active_bid),
Bids::Petite => [Bids::Passe].contains(active_bid),
Bids::Garde => [Bids::Passe, Bids::Petite].contains(active_bid),
Bids::GardeSans => [Bids::Passe, Bids::Petite, Bids::Garde].contains(active_bid),
Bids::GardeContre => {
[Bids::Passe, Bids::Petite, Bids::Garde, Bids::GardeSans].contains(&active_bid)
[Bids::Passe, Bids::Petite, Bids::Garde, Bids::GardeSans].contains(active_bid)
}
}
}
Expand Down
28 changes: 12 additions & 16 deletions src/common/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ pub struct Suit {

impl Suit {
pub fn new(name: CardSuits) -> Suit {
let suit_data = get_suit_data(name);
match suit_data {
(icon, initial) => Suit {
name,
icon,
initial,
},
let (icon, initial) = get_suit_data(name);
Suit {
name,
icon,
initial,
}
}
}
Expand Down Expand Up @@ -139,23 +137,21 @@ impl CardActions for Card {
Some(played_suit) => match (self.suit.name, card.suit.name, played_suit) {
(CardSuits::Trumps, _, __) => {
if card.suit.name.is_trump() {
return self.rank > card.rank;
self.rank > card.rank
} else {
return true;
true
}
}
(_, CardSuits::Trumps, __) => {
return false;
}
(_, CardSuits::Trumps, __) => false,
_ => {
if self.suit.name == played_suit && card.suit.name == played_suit {
return self.rank > card.rank;
self.rank > card.rank
} else if self.suit.name == played_suit && card.suit.name != played_suit {
return true;
true
} else if self.suit.name != played_suit && card.suit.name == played_suit {
return false;
false
} else {
return false;
false
}
}
},
Expand Down
16 changes: 8 additions & 8 deletions src/common/deal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ pub struct Deal {
pub called_king: Option<Card>,
}
impl Deal {
pub fn new(players: &mut Vec<Player>, deck: &mut Vec<Card>) -> Self {
pub fn new(players: &mut Vec<Player>, deck: &mut [Card]) -> Self {
let mut kitty = Kitty::new(players.len());
draw_cards(&deck, players, &mut kitty);
draw_cards(deck, players, &mut kitty);

Deal {
players: players.to_vec(), // TODO: is it necessary ?
Expand All @@ -56,7 +56,7 @@ impl DealActions for Deal {
fn call_king(&mut self) {
if self.players.len() > 4 {
self.called_king = Some(self.taker.clone().unwrap().player.call_king());
println!("\nThe called king is {}", self.called_king.clone().unwrap());
println!("\nThe called king is {}", self.called_king.unwrap());
}
}
fn compose_kitty(&mut self) {
Expand All @@ -77,12 +77,12 @@ impl DealActions for Deal {
}
}
fn play_tricks(&mut self) {
if self.players[0].hand.cards.len() == 0 {
if self.players[0].hand.cards.is_empty() {
return;
}
let trick = Trick::default();
for player in self.players.clone() {
player.play(&trick);
let mut trick = Trick::default();
for mut player in self.players.clone() {
player.play(&mut trick);
}
let winner_index = 0; // trick.get_best_played_card_index();
self.players = reorder(&self.players, winner_index);
Expand Down Expand Up @@ -142,7 +142,7 @@ fn draw_cards(deck: &[Card], players: &mut Vec<Player>, kitty: &mut Kitty) {
}
Dealing::Player => {
players[player_index].hand.cards.extend(split.to_vec());
player_index = get_next_index(&players, player_index);
player_index = get_next_index(players, player_index);
}
}
index = end_of_range;
Expand Down
12 changes: 6 additions & 6 deletions src/common/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ impl GameActions for Game {
fn collect_deck(&mut self, players: &[Player]) {
let mut deck = Vec::new();
for player in players {
match player.hand.cards.len() > 0 {
true => deck.extend(player.hand.cards.clone()),
false => deck.extend(player.hand.won_cards.clone()),
match player.hand.cards.is_empty() {
true => deck.extend(player.hand.won_cards.clone()),
false => deck.extend(player.hand.cards.clone()),
}
}
self.deck = deck;
Expand Down Expand Up @@ -89,7 +89,7 @@ fn generate_players(n_players: u8) -> Vec<Player> {
players
}

fn set_first_dealer(players: &mut Vec<Player>) {
fn set_first_dealer(players: &mut [Player]) {
let index = random_int_in_range(0, players.len());
players[index].is_dealer = true;
}
Expand All @@ -103,7 +103,7 @@ fn create_players(n_players: u8) -> Vec<Player> {
fn generate_card(n_cards: usize, suit: CardSuits) -> Vec<Card> {
let mut cards = Vec::new();
for rank in 1..=n_cards {
let card = Card::new(rank as u8, suit.clone());
let card = Card::new(rank as u8, suit);
cards.push(card);
}
cards.to_vec()
Expand All @@ -126,6 +126,6 @@ pub fn create_deck() -> Vec<Card> {
deck.to_vec()
}

pub fn find_dealer(players: &Vec<Player>) -> usize {
pub fn find_dealer(players: &[Player]) -> usize {
players.iter().position(|player| player.is_dealer).unwrap()
}
4 changes: 2 additions & 2 deletions src/common/score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use super::{
};

pub fn compute_oudlers(cards: &[Card]) -> usize {
cards.into_iter().filter(|card| card.is_oudler()).count()
cards.iter().filter(|card| card.is_oudler()).count()
}

pub fn compute_points(cards: &[Card]) -> f64 {
cards.into_iter().fold(0.0, |acc, card| acc + card.score())
cards.iter().fold(0.0, |acc, card| acc + card.score())
}

fn compute_needed_points(cards: &[Card]) -> f64 {
Expand Down
8 changes: 4 additions & 4 deletions src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn random_int_in_range(min: usize, max: usize) -> usize {
rng.gen_range(min..max)
}

pub fn get_next_index<T>(vector: &Vec<T>, current_index: usize) -> usize {
pub fn get_next_index<T>(vector: &[T], current_index: usize) -> usize {
if current_index < vector.len() - 1 {
current_index + 1
} else {
Expand All @@ -30,7 +30,7 @@ pub fn compare<T>(a: &T, b: Option<&T>, comparator: fn(&T, &T) -> bool) -> bool
}
}

pub fn reorder<T: Clone>(serie: &Vec<T>, index: usize) -> Vec<T> {
pub fn reorder<T: Clone>(serie: &[T], index: usize) -> Vec<T> {
let start = &serie[index..];
let end = &serie[..index];
[start, end].concat()
Expand All @@ -40,7 +40,7 @@ fn display_enumeration<T: std::fmt::Display>(vector: &[T]) {
for (index, vect) in vector.iter().enumerate() {
print!("{}. {}\t", index, vect);
}
println!("");
println!();
}

fn prompt_selection() -> Result<usize, <usize as FromStr>::Err> {
Expand Down Expand Up @@ -88,6 +88,6 @@ pub fn select<T: std::fmt::Display + std::marker::Copy>(
}
}

pub fn subtract(a: &mut Vec<Card>, b: &Vec<Card>) {
pub fn subtract(a: &mut Vec<Card>, b: &[Card]) {
a.retain(|x| !b.contains(x));
}
20 changes: 10 additions & 10 deletions tests/bid_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,46 @@ mod bid {
fn petite_compare_than_passe() {
let previous_bid = Bids::Passe;
let bid = Bids::Petite;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));
}

#[test]
fn garde_compare_than_petite_and_passe() {
let mut previous_bid = Bids::Passe;
let bid = Bids::Garde;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));

previous_bid = Bids::Petite;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));
}

#[test]
fn garde_sans_compare_than_garde_and_petite_and_passe() {
let mut previous_bid = Bids::Passe;
let bid = Bids::GardeSans;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));

previous_bid = Bids::Petite;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));

previous_bid = Bids::Garde;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));
}

#[test]
fn garde_contre_compare_than_garde_sans_and_garde_and_petite_and_passe() {
let mut previous_bid = Bids::Passe;
let bid = Bids::GardeContre;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));

previous_bid = Bids::Petite;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));

previous_bid = Bids::Garde;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));

previous_bid = Bids::GardeSans;
assert_eq!(compare_bids(&bid, &previous_bid), true);
assert!(compare_bids(&bid, &previous_bid));
}

#[rstest]
Expand Down
4 changes: 2 additions & 2 deletions tests/game_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ mod game {
let mut game = Game::default();
let current_dealer = find_dealer(&game.players);
let next_dealer = get_next_index(&game.players, current_dealer);
assert_eq!(game.players[current_dealer].is_dealer, true);
assert!(game.players[current_dealer].is_dealer);
game.update_dealer();
assert_eq!(game.players[next_dealer].is_dealer, true);
assert!(game.players[next_dealer].is_dealer);
}
}
26 changes: 26 additions & 0 deletions tests/trick_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[cfg(test)]
mod trick {
use rstest::rstest;
use tarot_cli::common::{
card::{Card, CardSuits},
trick::{Trick, TrickActions},
};

#[rstest]
fn trick_get_best_played_card_index(
#[values(
(Vec::new(), None),
(Vec::from([Card::new(2, CardSuits::Clubs), Card::new(14, CardSuits::Clubs)]), Some(1)),
(Vec::from([Card::new(14, CardSuits::Clubs), Card::new(2, CardSuits::Trumps), Card::new(2, CardSuits::Clubs), Card::new(2, CardSuits::Hearts)]), Some(1)),
(Vec::from([Card::new(8, CardSuits::Clubs), Card::new(10, CardSuits::Clubs), Card::new(14, CardSuits::Clubs), Card::new(2, CardSuits::Clubs), Card::new(1, CardSuits::Clubs)]), Some(2)),
(Vec::from([Card::new(2, CardSuits::Trumps), Card::new(2, CardSuits::Clubs)]), Some(0)))]
case: (Vec<Card>, Option<usize>),
) {
let (played_cards, expected_index) = case;
let mut trick = Trick::default();
for played_card in played_cards {
trick.played_cards.push(played_card);
}
assert_eq!(trick.get_best_played_card_index(), expected_index);
}
}

0 comments on commit 60b23bc

Please # to comment.