|
|
|
|
@ -79,7 +79,8 @@ pub(crate) fn check_set(
|
|
|
|
|
cards.next().unwrap(),
|
|
|
|
|
cards.next().unwrap(),
|
|
|
|
|
);
|
|
|
|
|
if is_set((a, b, c)) {
|
|
|
|
|
match is_set((a, b, c)) {
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
query
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.for_each(|(entity, _, mut visibility, mut transform)| {
|
|
|
|
|
@ -94,6 +95,10 @@ pub(crate) fn check_set(
|
|
|
|
|
});
|
|
|
|
|
commands.trigger(UiMessage("Yipee!".into()));
|
|
|
|
|
}
|
|
|
|
|
Err(invalid_set_err) => {
|
|
|
|
|
commands.trigger(UiMessage(format!("{}", invalid_set_err)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if cards.len() > 3 {
|
|
|
|
|
commands.trigger(UiMessage("Too many cards!".into()));
|
|
|
|
|
} else if cards.len() < 3 {
|
|
|
|
|
@ -101,8 +106,34 @@ pub(crate) fn check_set(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct InvalidSetErr {
|
|
|
|
|
color: bool,
|
|
|
|
|
number: bool,
|
|
|
|
|
pattern: bool,
|
|
|
|
|
shape: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for InvalidSetErr {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
|
let mut issues = vec![];
|
|
|
|
|
if !self.color {
|
|
|
|
|
issues.push("Color is an issue...");
|
|
|
|
|
}
|
|
|
|
|
if !self.number {
|
|
|
|
|
issues.push("Number is an issue...");
|
|
|
|
|
}
|
|
|
|
|
if !self.pattern {
|
|
|
|
|
issues.push("Pattern is an issue...");
|
|
|
|
|
}
|
|
|
|
|
if !self.shape {
|
|
|
|
|
issues.push("Shapes is an issue...");
|
|
|
|
|
}
|
|
|
|
|
write!(f, "{}", issues.join("\n"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Helper function to determine if three cards constitute a set
|
|
|
|
|
fn is_set((a, b, c): (&Card, &Card, &Card)) -> bool {
|
|
|
|
|
fn is_set((a, b, c): (&Card, &Card, &Card)) -> Result<(), InvalidSetErr> {
|
|
|
|
|
let color = {
|
|
|
|
|
((a.color == b.color) && (b.color == c.color) && (c.color == a.color))
|
|
|
|
|
|| ((a.color != b.color) && (b.color != c.color) && (c.color != a.color))
|
|
|
|
|
@ -119,7 +150,16 @@ fn is_set((a, b, c): (&Card, &Card, &Card)) -> bool {
|
|
|
|
|
((a.shape == b.shape) && (b.shape == c.shape) && (c.shape == a.shape))
|
|
|
|
|
|| ((a.shape != b.shape) && (b.shape != c.shape) && (c.shape != a.shape))
|
|
|
|
|
};
|
|
|
|
|
color && number && pattern && shape
|
|
|
|
|
if color && number && pattern && shape {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(InvalidSetErr {
|
|
|
|
|
color,
|
|
|
|
|
number,
|
|
|
|
|
shape,
|
|
|
|
|
pattern,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Event, Clone)]
|
|
|
|
|
@ -227,7 +267,7 @@ pub(crate) fn check_for_sets(
|
|
|
|
|
})
|
|
|
|
|
// return the first valid set
|
|
|
|
|
.find_map(|[(ea, ca), (eb, cb), (ec, cc)]| {
|
|
|
|
|
if is_set((ca, cb, cc)) {
|
|
|
|
|
if is_set((ca, cb, cc)).is_ok() {
|
|
|
|
|
info!("\n\t{}\n\t{}\n\t{}", ca, cb, cc);
|
|
|
|
|
Some((ea, eb, ec))
|
|
|
|
|
} else {
|
|
|
|
|
|