Skip to main content

picrs_lib/
table.rs

1use std::fmt::Display;
2
3use crate::axis::Axis;
4use bit_set::BitSet;
5
6#[derive(Debug)]
7pub struct Table {
8    pub columns: Vec<Axis>,
9    pub rows: Vec<Axis>,
10    pub ncorrect: usize,
11    pub num_of_axis: usize,
12}
13impl Display for Table {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        write!(f, "     ")?;
16        for column in self.columns.iter() {
17            write!(f, "{}", column)?;
18        }
19
20        writeln!(f, "")?;
21
22        for row in self.rows.iter() {
23            writeln!(f, "{}: {:?}", row, row.state)?;
24        }
25
26        Ok(())
27    }
28}
29impl Table {
30    pub fn new(num_columns: usize, num_rows: usize, filled: usize) -> Self {
31        //gen random filled squares to gen the hints based off of (this way we make sure the game
32        //board is an actually solvable puzzle
33        let mut rng = rand::rng();
34        let filled = rand::seq::index::sample(&mut rng, num_rows * num_columns, filled)
35            .into_iter()
36            .collect::<Vec<usize>>();
37
38        let mut columns: Vec<BitSet> = vec![BitSet::new(); num_columns];
39        let mut rows: Vec<BitSet> = vec![BitSet::new(); num_rows];
40
41        for x in filled {
42            columns[x % num_columns].insert(x / num_columns);
43            rows[x / num_columns].insert(x % num_columns);
44        }
45
46        let mut already_correct = 0;
47        let columns = columns
48            .iter()
49            .map(|bitset| {
50                let mut axis = Axis::new(&bitset);
51                if bitset.len() == 0 {
52                    axis.solved = true;
53                    already_correct += 1;
54                }
55                axis
56            })
57            .collect::<Vec<Axis>>();
58
59        let rows = rows
60            .iter()
61            .map(|bitset| {
62                let mut axis = Axis::new(&bitset);
63                if bitset.len() == 0 {
64                    axis.solved = true;
65                    already_correct += 1;
66                }
67                axis
68            })
69            .collect::<Vec<Axis>>();
70
71        Table {
72            columns,
73            rows,
74            ncorrect: already_correct,
75            num_of_axis: num_columns + num_rows,
76        }
77    }
78    pub fn toggle_square(&mut self, (x, y): (usize, usize)) {
79        let column_was_solved = self.columns[x].is_solved();
80        let row_was_solved = self.rows[y].is_solved();
81
82        self.columns[x].toggle_state(y);
83        self.rows[y].toggle_state(x);
84
85        let column_now_solved = self.columns[x].is_solved();
86        let row_now_solved = self.rows[y].is_solved();
87
88        if row_was_solved && !row_now_solved {
89            self.ncorrect -= 1;
90        }
91        if !row_was_solved && row_now_solved {
92            self.ncorrect += 1
93        }
94
95        if column_was_solved && !column_now_solved {
96            self.ncorrect -= 1;
97        }
98        if !column_was_solved && column_now_solved {
99            self.ncorrect += 1
100        }
101    }
102    pub fn is_solved(&self) -> bool {
103        self.ncorrect == self.num_of_axis
104    }
105}