1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// /// Turn 2d coordinates into a 1d index
// #[must_use]
// pub fn xy_to_idx<T: mirl::math::NumberWithMonotoneOps>(
// x: T,
// y: T,
// width: T,
// ) -> T {
// y * width + x
// }
// /// Turn a 1d index into a 2d coordinate
// #[must_use]
// pub fn idx_to_xy<T: mirl::math::NumberWithMonotoneOps + Copy>(
// idx: T,
// width: T,
// ) -> (T, T) {
// let y = idx / width;
// let x = idx % width;
// (x, y)
// }
// /// Generate a minefield with a safe area of 3x3
// #[must_use]
// #[cfg(feature = "debug-window")]
// pub fn generate_minefield(
// width: usize,
// height: usize,
// safe_x: usize,
// safe_y: usize,
// mines: usize,
// ) -> Vec<bool> {
// let total_tiles = width * height;
// let mut id_board: Vec<usize> = 0.repeat_value(total_tiles);
// let mut board = Vec::with_capacity(total_tiles);
// let safe_range = 1;
// for x in -safe_range..safe_range {
// for y in -safe_range..safe_range {
// id_board[xy_to_idx(
// safe_x.saturated_add_sign(x),
// safe_y.saturated_add_sign(y),
// width,
// )] = usize::MAX;
// }
// }
// let offset = id_board.iter().filter(|x| **x == usize::MAX).count();
// #[allow(clippy::needless_range_loop)] // Clippy, no.
// for i in 0..total_tiles {
// if id_board[i] == 0 {
// id_board[i] = i + offset;
// } else if id_board[i] == usize::MAX {
// id_board[i] = 0;
// }
// }
// let mut remaining_mines = mines as f64;
// for i in id_board {
// let chance = remaining_mines / (total_tiles - i) as f64;
// if chance > rand::random() {
// remaining_mines -= 1.0;
// board.push(true);
// } else {
// board.push(false);
// }
// }
// board
// }
// fn count_mines_around_place(
// position: (usize, usize),
// board: &[bool],
// width: usize,
// height: usize,
// ) -> usize {
// let mut mines = 0;
// for x in -1..1 {
// for y in -1..1 {
// let new_pos = position.try_tuple_into().add((x, y));
// if new_pos.0 > 0
// && new_pos.1 > 0
// && new_pos.0 < width as isize
// && new_pos.1 < height as isize
// && board
// [xy_to_idx(new_pos.0 as usize, new_pos.1 as usize, width)]
// {
// mines += 1;
// }
// }
// }
// mines
// }
// /// Puts all numbers into place
// #[must_use]
// pub fn get_numbers_from_minefield(
// board: &[bool],
// width: usize,
// height: usize,
// ) -> Vec<usize> {
// let mut new_board = Vec::new();
// for i in 0..board.len() {
// new_board.push(count_mines_around_place(
// idx_to_xy(i, width),
// board,
// width,
// height,
// ));
// }
// new_board
// }
// /// Convert state boards to a final visual representation
// #[must_use]
// pub fn get_visual_board(
// minefield: &[bool],
// numbers: &[usize],
// revealed: &[bool],
// flagged: &[bool],
// ) -> Vec<MinesweeperTile> {
// let mut result = Vec::new();
// for i in 0..minefield.len() {
// if flagged[i] {
// result.push(MinesweeperTile::Flag);
// } else if !revealed[i] {
// result.push(MinesweeperTile::UnRevealed);
// } else if minefield[i] {
// result.push(MinesweeperTile::Mine);
// } else {
// result.push(match numbers[i] {
// 0 => MinesweeperTile::Empty,
// 1 => MinesweeperTile::One,
// 2 => MinesweeperTile::Two,
// 3 => MinesweeperTile::Three,
// 4 => MinesweeperTile::Four,
// 5 => MinesweeperTile::Five,
// 6 => MinesweeperTile::Six,
// 7 => MinesweeperTile::Seven,
// 8 => MinesweeperTile::Eight,
// 9 => MinesweeperTile::Nine,
// _ => MinesweeperTile::Flag,
// });
// }
// }
// result
// }
// /// LOGIC
// pub fn handle_minesweeper_logic(
// flag_mode: bool,
// click_pos: (usize, usize),
// mask: &mut [bool],
// flagged: &mut [bool],
// minefield: &[bool],
// numbers: &[usize],
// width: usize,
// ) -> (Vec<MinesweeperTile>, bool) {
// let idx = xy_to_idx(click_pos.0, click_pos.1, width);
// let mut lost = false;
// if flag_mode {
// if !mask[idx] {
// flagged[idx] = !flagged[idx];
// }
// } else if !flagged[idx] {
// mask[idx] = true;
// if minefield[idx] {
// lost = true;
// }
// }
// (get_visual_board(minefield, numbers, mask, flagged), lost)
// }
// #[allow(missing_docs)]
// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
// #[cfg_attr(feature = "c_compatible", repr(C))] pub enum MinesweeperTile {
// UnRevealed,
// Empty,
// One,
// Two,
// Three,
// Four,
// Five,
// Six,
// Seven,
// Eight,
// Nine,
// Flag,
// Mine,
// }