Skip to content

Commit 18064a0

Browse files
committed
Add combo_locations_for_card() method, docs improvements
1 parent 09348db commit 18064a0

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

gomori/src/board.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct Diff {
4444

4545
/// The effects that playing a card would have.
4646
///
47-
/// Returned by [`Board::execute()`].
47+
/// Returned by [`Board::calculate()`].
4848
pub struct CalculatedEffects<'a> {
4949
/// This struct ties together the board and its diff, to prevent any possible mixups
5050
board: &'a Board,
@@ -146,12 +146,7 @@ impl Board {
146146
let mut set = CardsSet::new();
147147
for &(i, j, field) in &self.fields {
148148
if won.contains(i, j) {
149-
for card in field.hidden_cards() {
150-
set = set.insert(card);
151-
}
152-
if let Some(card) = field.top_card() {
153-
set = set.insert(card);
154-
}
149+
set |= field.all_cards();
155150
}
156151
}
157152
set
@@ -260,6 +255,17 @@ impl Board {
260255
bitboard
261256
}
262257

258+
/// Returns all the coordinates that already have a card on them and are valid places to play the given card.
259+
pub fn combo_locations_for_card(&self, card: Card) -> BitBoard {
260+
let mut bitboard = BitBoard::empty_board_centered_at(self.fields[0].0, self.fields[0].1);
261+
for &(i, j, field) in &self.fields {
262+
if field.can_place_card(card) {
263+
bitboard = bitboard.insert(i, j);
264+
}
265+
}
266+
bitboard
267+
}
268+
263269
/// Returns a [`CompactField`] if there are any cards at the given coordinate.
264270
pub fn get(&self, i: i8, j: i8) -> Option<CompactField> {
265271
for &(i_field, j_field, compact_field) in &self.fields {
@@ -499,6 +505,11 @@ mod python {
499505
self.locations_for_card(card)
500506
}
501507

508+
#[pyo3(name = "combo_locations_for_card")]
509+
fn py_combo_locations_for_card(&self, card: Card) -> BitBoard {
510+
self.combo_locations_for_card(card)
511+
}
512+
502513
#[pyo3(name = "get")]
503514
fn py_get(&self, i: i8, j: i8) -> Option<CompactField> {
504515
self.get(i, j)

gomori/src/board/compact_field.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl CompactField {
8787

8888
/// All cards on the field.
8989
///
90-
/// Equal to [`hidden_cards()`] + [`top_card()`], if any.
90+
/// Equal to [`hidden_cards()`](Self::hidden_cards) + [`top_card()`](Self::top_card), if any.
9191
pub fn all_cards(self) -> CardsSet {
9292
if let Some(c) = self.top_card() {
9393
self.hidden_cards().insert(c)

gomori/src/cards_set.rs

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ use crate::Card;
55
/// Allows intersection/union/xor with other such sets via bitwise ops.
66
/// Also implements [`IntoIterator`], so it can be converted into e.g.
77
/// a vector with `Vec::from_iter(cards_set)`.
8+
///
9+
/// ```
10+
/// use gomori::{card, CardsSet};
11+
/// let mut set = CardsSet::new();
12+
/// // This is an immutable data type, so functions like `insert` return a new `CardsSet`.
13+
/// set = set.insert(card!("7♥"));
14+
/// set = set.insert(card!("7♥")); // Inserting a second time has no effect
15+
/// set = set.insert(card!("2♥"));
16+
/// assert_eq!(Vec::from_iter(set), vec![card!("2♥"), card!("7♥")]);
17+
/// ```
818
#[cfg_attr(feature = "python", pyo3::pyclass)]
919
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1020
pub struct CardsSet {

gomori/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mod python {
124124
gomori,
125125
IllegalMoveException,
126126
pyo3::exceptions::PyException,
127-
"Describes why the card cannot be played."
127+
"Describes why a move is illegal."
128128
);
129129

130130
impl From<IllegalMove> for PyErr {

gomori/src/protocol_types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub enum Request {
4545
#[derive(Clone, Debug, Serialize, Deserialize)]
4646
pub struct Okay();
4747

48+
/// Black or white.
4849
#[cfg_attr(feature = "python", pyo3::pyclass)]
4950
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
5051
#[serde(rename_all = "lowercase")]

0 commit comments

Comments
 (0)