Skip to content

Commit 43859fb

Browse files
committed
win_cards_amount_number + task that could be implemented in win_cards_amount_color
1 parent 613e5c7 commit 43859fb

4 files changed

Lines changed: 106 additions & 7 deletions

File tree

src/task.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod dont_win_cards;
2323
pub mod dont_win_tricks;
2424
pub mod win_cards;
2525
pub mod win_cards_amount_color;
26+
pub mod win_cards_amount_number;
2627
pub mod win_nb_tricks;
2728
pub mod win_nb_tricks_compared_captain;
2829
pub mod win_trick_with;

src/task/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Thank you for your help!
6262

6363
## Checklist
6464

65-
**Status: 73/96.**
65+
**Status: 82/96.**
6666

6767
From [BGG forum](https://boardgamegeek.com/thread/2631311/all-the-mission-cards):
6868

@@ -150,18 +150,19 @@ From [BGG forum](https://boardgamegeek.com/thread/2631311/all-the-mission-cards)
150150
- [x] (3/4/4) Win exactly two blue cards.
151151
- [x] (3/3/4) Win exactly one pink card.
152152
- [x] (2/3/4) Win at least one card of each colour (excluding submarines)
153+
- [x] (3/3/3) Win exactly one submarine (deal new cards if someone has all submarines in hand)
153154
- [x] (3/3/4) Win exactly two submarines (deal new cards if someone has submarines no. 2,3,4 in hand)
154155
- [x] (3/4/4) Win exactly three submarines (deal new cards if someone has all submarines in hand)
156+
- done in [`TaskWinCardsAmountNumber`]
157+
- [x] (3/4/5) Win at least three 5s.
158+
- [x] (3/4/5) Win at least three 9s.
159+
- [x] (2/2/2) Win at least two 7s.
160+
- [x] (3/4/4) Win exactly three 6s
161+
- [x] (2/3/3) Win exactly two 9s
155162
- [ ] (2/3/3) Win more tricks than everyone else
156163
- [ ] (3/4/5) Win more tricks than everyone else together
157164
- [ ] (2/2/3) Win fewer tricks than everyone else
158-
- [ ] (3/4/5) Win at least three 5s.
159-
- [ ] (3/4/5) Win at least three 9s.
160-
- [ ] (2/2/2) Win at least two 7s.
161-
- [ ] (3/4/4) Win exactly three 6s
162-
- [ ] (2/3/3) Win exactly two 9s
163165
- [ ] (3/4/5) Win all cards of at least one colour (excluding submarines)
164-
- [ ] (3/3/3) Win exactly one submarine (deal new cards if someone has all submarines in hand)
165166
- [ ] (3/3/3) Win the 1 submarine and no other (deal new cards if someone has submarines no. 1 and 4 or 1,2,3 in hand)
166167
- [ ] (3/3/3) Win the 2 submarine and no other (deal new cards if someone has submarines no. 2 and 4 or 1,2,3 in hand)
167168
- [ ] (3/2/2) Do not win two consecutive tricks.

src/task/win_cards_amount_color.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ impl TaskWinCardsAmountColor {
6060
Self::new(false, constraints)
6161
}
6262

63+
pub fn new_exactly_1_submarine() -> Self {
64+
let constraints = HashMap::from([(Card::Submarine as fn(usize) -> Card, 1)]);
65+
Self::new(true, constraints)
66+
}
67+
6368
pub fn new_exactly_2_submarines() -> Self {
6469
let constraints = HashMap::from([(Card::Submarine as fn(usize) -> Card, 2)]);
6570
Self::new(true, constraints)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use std::collections::HashMap;
2+
3+
use crate::{
4+
task::{Task, TaskStatus},
5+
trick::Trick,
6+
};
7+
8+
#[derive(Debug)]
9+
pub struct TaskWinCardsAmountNumber {
10+
constraints: HashMap<usize, usize>,
11+
exactly: bool,
12+
}
13+
14+
fn count_won_except_submarine(tricks: &[Trick], value: usize) -> usize {
15+
tricks.iter().fold(0, |acc, trick| {
16+
acc + trick
17+
.iter()
18+
.filter(|c| c.val() == value && !c.is_submarine())
19+
.count()
20+
})
21+
}
22+
23+
impl TaskWinCardsAmountNumber {
24+
pub fn new(exactly: bool, constraints: HashMap<usize, usize>) -> Self {
25+
Self {
26+
exactly,
27+
constraints,
28+
}
29+
}
30+
31+
pub fn new_at_least_three_5s() -> Self {
32+
let constraints = HashMap::from([(5, 3)]);
33+
Self::new(false, constraints)
34+
}
35+
36+
pub fn new_at_least_three_9s() -> Self {
37+
let constraints = HashMap::from([(9, 3)]);
38+
Self::new(false, constraints)
39+
}
40+
41+
pub fn new_at_least_two_7s() -> Self {
42+
let constraints = HashMap::from([(7, 2)]);
43+
Self::new(false, constraints)
44+
}
45+
46+
pub fn new_exactly_three_6s() -> Self {
47+
let constraints = HashMap::from([(6, 3)]);
48+
Self::new(true, constraints)
49+
}
50+
51+
pub fn new_exactly_two_9s() -> Self {
52+
let constraints = HashMap::from([(9, 2)]);
53+
Self::new(true, constraints)
54+
}
55+
}
56+
57+
impl Task for TaskWinCardsAmountNumber {
58+
fn eval(&self, state: &crate::state::State, ip: usize) -> super::TaskStatus {
59+
let mut done = true;
60+
for (&value, &amount) in &self.constraints {
61+
let won_by_ip = count_won_except_submarine(state.get_player(ip).get_tricks(), value);
62+
63+
if self.exactly && won_by_ip > amount {
64+
return TaskStatus::Failed;
65+
}
66+
67+
if !self.exactly && won_by_ip >= amount {
68+
continue;
69+
}
70+
71+
let missing = amount - won_by_ip;
72+
let mut won_by_others = 0;
73+
for i in 0..state.n_players() {
74+
if i != ip {
75+
won_by_others +=
76+
count_won_except_submarine(state.get_player(i).get_tricks(), value);
77+
}
78+
}
79+
let available = 4 - (won_by_ip + won_by_others);
80+
if available < missing {
81+
return TaskStatus::Failed;
82+
}
83+
done &= (self.exactly && available == 0) || (!self.exactly && won_by_ip >= amount);
84+
}
85+
86+
if done {
87+
return TaskStatus::Done;
88+
}
89+
90+
TaskStatus::Unknown
91+
}
92+
}

0 commit comments

Comments
 (0)