Slight refactoring

- Remove some duplication in InformationPlayerStrategy::decide()
- Add method GameView::get_other_players()
This commit is contained in:
Felix Bauckholt 2019-02-24 18:49:28 +01:00 committed by Jeff Wu
parent 385feeb6ba
commit b3a8209b74
2 changed files with 18 additions and 25 deletions

View file

@ -451,18 +451,20 @@ pub trait GameView {
}).is_some()
}
fn can_see(&self, card: &Card) -> bool {
fn get_other_players(&self) -> Vec<Player> {
self.get_board().get_players().filter(|&player| {
player != self.me()
}).any(|player| {
}).collect()
}
fn can_see(&self, card: &Card) -> bool {
self.get_other_players().iter().any(|player| {
self.has_card(&player, card)
})
}
fn someone_else_can_play(&self) -> bool {
self.get_board().get_players().filter(|&player| {
player != self.me()
}).any(|player| {
self.get_other_players().iter().any(|player| {
self.get_hand(&player).iter().any(|card| {
self.get_board().is_playable(card)
})

View file

@ -462,9 +462,7 @@ impl InformationPlayerStrategy {
}
fn get_hint_sum_info(&self, total_info: u32, view: &OwnedGameView) -> ModulusInformation {
view.get_board().get_players().filter(|&player| {
player != self.me
}).fold(
view.get_other_players().iter().fold(
ModulusInformation::new(total_info, 0),
|mut sum_info, player| {
let answer = self.get_hint_info_for_player(&player, total_info, view);
@ -1008,7 +1006,17 @@ impl PlayerStrategy for InformationPlayerStrategy {
let public_useless_indices = self.find_useless_cards(view, &self.get_my_public_info());
let useless_indices = self.find_useless_cards(view, &private_info);
if view.board.discard_size() <= soft_discard_threshold {
let will_hint =
if view.board.discard_size() <= soft_discard_threshold && useless_indices.len() > 0 { false }
// hinting is better than discarding dead cards
// (probably because it stalls the deck-drawing).
else if view.board.hints_remaining > 0 && view.someone_else_can_play() { true }
else { false };
if will_hint {
return self.get_hint();
}
// if anything is totally useless, discard it
if public_useless_indices.len() > 1 {
let info = self.get_hint_sum_info(public_useless_indices.len() as u32, view);
@ -1018,23 +1026,6 @@ impl PlayerStrategy for InformationPlayerStrategy {
// TODO: after that, potentially prefer useless indices that arent public
return TurnChoice::Discard(useless_indices[0]);
}
}
// hinting is better than discarding dead cards
// (probably because it stalls the deck-drawing).
if view.board.hints_remaining > 0 {
if view.someone_else_can_play() {
return self.get_hint();
}
}
// if anything is totally useless, discard it
if public_useless_indices.len() > 1 {
let info = self.get_hint_sum_info(public_useless_indices.len() as u32, view);
return TurnChoice::Discard(public_useless_indices[info.value as usize]);
} else if useless_indices.len() > 0 {
return TurnChoice::Discard(useless_indices[0]);
}
// NOTE: the only conditions under which we would discard a potentially useful card:
// - we have no known useless cards