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
use crate::board::RectangularBoard;
use rand::Rng;
use simplesvg::{Attr, Color, Fig, Svg};
use std::collections::{HashMap, HashSet};

pub fn render_single_tiling_from_vec(boards: Vec<&RectangularBoard>) -> String {
    let mut tile_hashmap = HashMap::new();

    for i in (1..boards.len()).rev() {
        tile_hashmap.insert(boards[i].clone(), vec![boards[i - 1].clone()]);
    }

    render_single_tiling(boards.last().unwrap(), &tile_hashmap)
}

pub fn render_single_tiling<S: ::std::hash::BuildHasher>(
    board: &RectangularBoard,
    tile_hashmap: &HashMap<RectangularBoard, Vec<RectangularBoard>, S>,
) -> String {
    // TODO: maybe remove gap_size now that we've implemented borders
    let gap_size = 0.0;
    let box_size = 50.0;
    let padding = 10.0;

    // TODO: make these configurable
    let colors = vec![
        Color(30, 56, 136),
        Color(71, 115, 170),
        Color(245, 230, 99),
        Color(255, 173, 105),
        Color(156, 56, 72),
        Color(124, 178, 135),
        Color(251, 219, 136),
    ];

    let mut boxes = Vec::new();

    // choose a random initial colour
    // we do this so that when you render a single tile, it won't always be the first colour in the colors vector
    let mut color_index = rand::thread_rng().gen_range(0, colors.len());
    let mut current = board;

    while tile_hashmap.contains_key(current) {
        // choose a random source for this board state
        let next = rand::thread_rng().gen_range(0, tile_hashmap[current].len());
        let next_board = tile_hashmap.get(current).unwrap().get(next).unwrap();

        let mut tiled_positions = HashSet::new();

        // compute the tile that was placed here
        for y in 0..next_board.height {
            for x in 0..next_board.width {
                if next_board.board[y][x] ^ current.board[y][x] {
                    // we just tiled this position
                    tiled_positions.insert((x, y));
                }
            }
        }

        for (x, y) in tiled_positions.iter() {
            // draw the underlying box
            let rect = Fig::Rect(
                (*x as f32) * (box_size + gap_size) + padding,
                (*y as f32) * (box_size + gap_size) + padding,
                box_size,
                box_size,
            )
            .styled(Attr::default().fill(colors[color_index]));

            boxes.push(rect);

            enum Border {
                Left,
                Right,
                Top,
                Bottom,
            };

            // helper function to construct our borders
            let border = |x: usize, y: usize, b: Border, gray: bool| {
                let xs = match b {
                    Border::Right => (x as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
                    _ => (x as f32) * (box_size + gap_size) + padding,
                };
                let ys = match b {
                    Border::Top => (y as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
                    _ => (y as f32) * (box_size + gap_size) + padding,
                };
                let xe = match b {
                    Border::Left => (x as f32) * (box_size + gap_size) + padding,
                    _ => (x as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
                };
                let ye = match b {
                    Border::Bottom => (y as f32) * (box_size + gap_size) + padding,
                    _ => (y as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
                };

                let mut b = Fig::Line(xs, ys, xe, ye);
                b = b.styled(
                    Attr::default()
                        .stroke(if gray {
                            Color(211, 211, 211)
                        } else {
                            Color(0, 0, 0)
                        })
                        .stroke_width(0.5),
                );

                b
            };

            // left border
            boxes.push(border(
                *x,
                *y,
                Border::Left,
                tiled_positions.contains(&(*x - 1, *y)),
            ));
            // right border
            boxes.push(border(
                *x,
                *y,
                Border::Right,
                tiled_positions.contains(&(*x + 1, *y)),
            ));
            // top border
            boxes.push(border(
                *x,
                *y,
                Border::Top,
                tiled_positions.contains(&(*x, *y + 1)),
            ));
            // bottom border
            boxes.push(border(
                *x,
                *y,
                Border::Bottom,
                tiled_positions.contains(&(*x, *y - 1)),
            ));
        }

        // increment the color index by 1
        color_index = (color_index + 1) % colors.len();

        current = next_board;
    }

    Svg(
        vec![Fig::Multiple(boxes)],
        (50 * board.width) as u32 + 2 * (padding as u32),
        (50 * board.height) as u32 + 2 * (padding as u32),
    )
    .to_string()
}