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
use std::{rc::Rc, cell::RefCell};
pub mod pixel_board_randomizer;

pub trait Pixel {
    fn get_invalid_location_offsets_for_other_pixel(&self, other_pixel: &Self) -> Vec<(i16, i16)>;
}

pub struct PixelBoard<T: Pixel> {
    width: usize,
    height: usize,
    pixels: Vec<Option<Rc<RefCell<T>>>>
}

impl<T: Pixel> Clone for PixelBoard<T> {
    fn clone(&self) -> Self {
        Self {
            width: self.width.clone(),
            height: self.height.clone(),
            pixels: self.pixels.clone()
        }
    }
}

impl<T: Pixel> PixelBoard<T> {
    pub fn new(width: usize, height: usize) -> Self {
        let mut pixels = Vec::new();
        for _ in 0..(width * height) {
            pixels.push(None);
        }
        PixelBoard {
            width: width,
            height: height,
            pixels: pixels
        }
    }
    pub fn set(&mut self, x: usize, y: usize, pixel: Rc<RefCell<T>>) {
        let index = y * self.width + x;
        let _ = self.pixels[index].insert(pixel);
    }
    pub fn exists(&self, x: usize, y: usize) -> bool {
        let index = y * self.width + x;
        self.pixels[index].is_some()
    }
    pub fn get(&self, x: usize, y: usize) -> Option<Rc<RefCell<T>>> {
        let index = y * self.width + x;
        self.pixels[index].clone()
    }
    pub fn get_width(&self) -> usize {
        self.width
    }
    pub fn get_height(&self) -> usize {
        self.height
    }
}