Skip to content

Commit 8fb0fae

Browse files
committed
Add display trait to technique mask
1 parent 721fc53 commit 8fb0fae

File tree

1 file changed

+61
-0
lines changed
  • rustoku-lib/src/core/techniques

1 file changed

+61
-0
lines changed

rustoku-lib/src/core/techniques/mod.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::board::Board;
22
use super::candidates::Candidates;
33
use super::masks::Masks;
44
use bitflags::bitflags;
5+
use std::fmt;
56

67
mod hidden_pairs;
78
mod hidden_singles;
@@ -44,6 +45,37 @@ bitflags! {
4445
}
4546
}
4647

48+
impl fmt::Display for TechniqueMask {
49+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50+
if self.is_empty() {
51+
return write!(f, "None");
52+
}
53+
54+
let mut techniques = Vec::new();
55+
56+
if self.contains(TechniqueMask::NAKED_SINGLES) {
57+
techniques.push("Naked Singles");
58+
}
59+
if self.contains(TechniqueMask::HIDDEN_SINGLES) {
60+
techniques.push("Hidden Singles");
61+
}
62+
if self.contains(TechniqueMask::NAKED_PAIRS) {
63+
techniques.push("Naked Pairs");
64+
}
65+
if self.contains(TechniqueMask::HIDDEN_PAIRS) {
66+
techniques.push("Hidden Pairs");
67+
}
68+
if self.contains(TechniqueMask::LOCKED_CANDIDATES) {
69+
techniques.push("Locked Candidates");
70+
}
71+
if self.contains(TechniqueMask::XWING) {
72+
techniques.push("X-Wing");
73+
}
74+
75+
write!(f, "{}", techniques.join(", "))
76+
}
77+
}
78+
4779
// Now the actual implementation of the techniques, these would operate on
4880
// references to Board, Masks, and CandidatesCache.
4981
pub struct TechniquePropagator<'a> {
@@ -143,3 +175,32 @@ pub trait TechniqueRule {
143175
/// Applies the technique to the given propagator.
144176
fn apply(&self, prop: &mut TechniquePropagator, path: &mut Vec<(usize, usize, u8)>) -> bool;
145177
}
178+
179+
#[cfg(test)]
180+
mod tests {
181+
use super::*;
182+
183+
#[test]
184+
fn test_display_empty_mask() {
185+
let mask = TechniqueMask::empty();
186+
assert_eq!(format!("{}", mask), "None");
187+
}
188+
189+
#[test]
190+
fn test_display_single_technique() {
191+
let mask = TechniqueMask::NAKED_SINGLES;
192+
assert_eq!(format!("{}", mask), "Naked Singles");
193+
194+
let mask = TechniqueMask::XWING;
195+
assert_eq!(format!("{}", mask), "X-Wing");
196+
}
197+
198+
#[test]
199+
fn test_display_multiple_techniques() {
200+
let mask = TechniqueMask::EASY;
201+
assert_eq!(format!("{}", mask), "Naked Singles, Hidden Singles");
202+
203+
let mask = TechniqueMask::NAKED_SINGLES | TechniqueMask::XWING | TechniqueMask::LOCKED_CANDIDATES;
204+
assert_eq!(format!("{}", mask), "Naked Singles, Locked Candidates, X-Wing");
205+
}
206+
}

0 commit comments

Comments
 (0)