Created workspace
This commit is contained in:
64
canvas/src/card.rs
Normal file
64
canvas/src/card.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use crate::{CardField, Icon};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
// There are 60 cards in the base set
|
||||
pub struct Card {
|
||||
pub field: [CardField; 5],
|
||||
}
|
||||
|
||||
impl Card {
|
||||
pub const fn empty() -> Self {
|
||||
Self {
|
||||
field: [CardField::Empty; 5],
|
||||
}
|
||||
}
|
||||
pub fn from_dbg_string(text: &str) -> Self {
|
||||
let mut result = Self::empty();
|
||||
let mut current_field = 0;
|
||||
let mut char_iter = text.chars();
|
||||
let match_icon = |x: char| {
|
||||
let field = match x {
|
||||
's' => Icon::Square,
|
||||
'c' => Icon::Circle,
|
||||
't' => Icon::Triangle,
|
||||
'l' => Icon::Color,
|
||||
_ => return None,
|
||||
};
|
||||
Some(field)
|
||||
};
|
||||
while let Some(c) = char_iter.next() {
|
||||
if let Some(f) = match_icon(c) {
|
||||
result.field[current_field] = CardField::Icon(f);
|
||||
current_field += 1;
|
||||
continue;
|
||||
}
|
||||
let field = match c {
|
||||
'p' => {
|
||||
let f = match_icon(char_iter.next().expect("No icon after point symbol"))
|
||||
.expect("Incorrect icon after point symbol");
|
||||
CardField::PointsPer(f)
|
||||
}
|
||||
'd' => {
|
||||
let f1 =
|
||||
match_icon(char_iter.next().expect("No first icon after double symbol"))
|
||||
.expect("Incorrect first icon after double symbol");
|
||||
let f2 = match_icon(
|
||||
char_iter
|
||||
.next()
|
||||
.expect("No second icon after double symbol"),
|
||||
)
|
||||
.expect("Incorrect second icon after double symbol");
|
||||
CardField::DoubleIcon((f1, f2))
|
||||
}
|
||||
'.' => CardField::Empty,
|
||||
'-' => continue,
|
||||
_ => panic!("Unknown character {c}"),
|
||||
};
|
||||
|
||||
result.field[current_field] = field;
|
||||
current_field += 1;
|
||||
}
|
||||
for c in text.chars() {}
|
||||
result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user