1use std::ops::Add;
2use std::cmp::Ordering;
3use num_traits::identities::Zero;
4use crate::cats::Colour::*;
5use crate::cats::{Colour, CellCategory};
6
7#[derive(Debug, Clone, Hash)]
8pub struct Cell {
9 pub row: usize,
10 pub col: usize,
11 pub colour: Colour,
12 pub cat: CellCategory,
13}
14
15impl Add for Cell {
16 type Output = Self;
17
18 fn add(self, rhs: Self) -> Self {
19 rhs
20 }
21}
22
23impl Zero for Cell {
24 fn zero() -> Self {
25 Self {
26 row: 0,
27 col: 0,
28 colour: Black,
29 cat: CellCategory::BG,
30 }
31 }
32
33 fn is_zero(&self) -> bool {
34 self.row == 0 && self.col == 0
35 }
36}
37
38impl Ord for Cell {
39 fn cmp(&self, other: &Self) -> Ordering {
40 (self.row, &self.col).cmp(&(other.row, &other.col))
41 }
42}
43
44impl PartialOrd for Cell {
45 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
46 Some(self.cmp(other))
47 }
48}
49
50impl PartialEq for Cell {
51 fn eq(&self, other: &Self) -> bool {
52 (self.row, &self.col) == (other.row, &other.col)
53 }
54}
55
56impl Eq for Cell {}
57
58impl Cell {
59 pub fn new(x: usize, y: usize, colour: usize) -> Self {
60 Self { row: x, col: y, colour: Colour::new(colour), cat: CellCategory::BG }
61 }
62
63 pub const fn new_empty() -> Self {
64 Self { row: 0, col: 0, colour: NoColour, cat: CellCategory::BG }
65 }
66
67 pub fn new_colour(x: usize, y: usize, colour: Colour) -> Self {
68 Self { row: x, col: y, colour, cat: CellCategory::BG }
69 }
70
71 pub fn above(&self, other: &Self) -> bool {
72 other.col < self.col
73 }
74
75 pub fn below(&self, other: &Self) -> bool {
76 other.col > self.col
77 }
78
79 pub fn left(&self, other: &Self) -> bool {
80 other.row < self.row
81 }
82
83 pub fn right(&self, other: &Self) -> bool {
84 other.row > self.row
85 }
86
87 #[allow(clippy::nonminimal_bool)]
88 pub fn next(&self, other: &Self) -> bool {
89 let self_row: i16 = self.row as i16;
90 let self_col: i16 = self.col as i16;
91 let other_row: i16 = other.row as i16;
92 let other_col: i16 = other.col as i16;
93
94 other_col == self_col - 1 && other_row == self_row ||
95 other_col == self_col + 1 && other_row == self_row ||
96 other_row == self_row - 1 && other_col == self_col ||
97 other_row == self_row + 1 && other_col == self_col
98 }
99
100 #[allow(clippy::nonminimal_bool)]
101 pub fn adjacent(&self, other: &Self) -> bool {
102 let self_row: i16 = self.row as i16;
103 let self_col: i16 = self.col as i16;
104 let other_row: i16 = other.row as i16;
105 let other_col: i16 = other.col as i16;
106
107 other_col == self_col - 1 && other_row == self_row - 1 ||
108 other_col == self_col - 1 && other_row == self_row + 1 ||
109 other_col == self_col + 1 && other_row == self_row - 1 ||
110 other_col == self_col + 1 && other_row == self_row + 1
111 }
112
113 pub fn next_colour(&self, other: &Self) -> bool {
114 self.colour == other.colour && self.next(other)
115 }
116
117 pub fn adjacent_colour(&self, other: &Self) -> bool {
118 self.colour == other.colour && self.adjacent(other)
119 }
120}
121