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
use super::options::BoardSide;
use super::{board_label_text, RenderOptions};

use crate::errors::GobanError;
use crate::goban::StoneColor;
use crate::Goban;

pub fn render(goban: &Goban, options: &RenderOptions) -> Result<String, GobanError> {
    let (x_range, y_range) = options.goban_range.get_ranges(goban, options)?;
    let width = x_range.end - x_range.start;
    let height = y_range.end - y_range.start;
    if !options.label_sides.is_empty() && width > 25 || height > 99 {
        return Err(GobanError::UnlabellableRange);
    }
    let mut lines: Vec<String> = vec![];
    let label_padding = if options.label_sides.contains(BoardSide::West) {
        "   "
    } else {
        ""
    };
    if options.label_sides.contains(BoardSide::North) {
        let line: String = x_range.clone().map(board_label_text).collect();
        lines.push(format!("{}{}", label_padding, line));
    }
    for y in y_range {
        let mut line = x_range.clone().map(|x| char_at(goban, x, y)).collect();
        if options.label_sides.contains(BoardSide::West) {
            line = format!("{: >2} {}", y + 1, line);
        }
        if options.label_sides.contains(BoardSide::East) {
            line.push_str(&format!(" {}", y + 1));
        }
        lines.push(line);
    }
    if options.label_sides.contains(BoardSide::South) {
        let line: String = x_range.clone().map(board_label_text).collect();
        lines.push(format!("{}{}", label_padding, line));
    }
    Ok(lines.join("\n"))
}

fn char_at(goban: &Goban, x: u8, y: u8) -> char {
    let max_x = goban.size().0 - 1;
    let max_y = goban.size().1 - 1;
    match goban.stone_color(x, y) {
        Some(StoneColor::White) => '●',
        Some(StoneColor::Black) => '○',
        None => match (x, y) {
            (0, 0) => '┏',
            (x, 0) if x == max_x => '┓',
            (0, y) if y == max_y => '┗',
            (x, y) if x == max_x && y == max_y => '┛',
            (_, 0) => '┯',
            (0, _) => '┠',
            (_, y) if y == max_y => '┷',
            (x, _) if x == max_x => '┨',
            (_, _) => '┼',
        },
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use crate::render::GobanRange;
    use crate::{Goban, RenderOptions};

    use super::render;

    fn build_diagram(sgf_dir: &str, options: &RenderOptions) -> String {
        let d: PathBuf = [
            env!("CARGO_MANIFEST_DIR"),
            "tests",
            "data",
            sgf_dir,
            "input.sgf",
        ]
        .iter()
        .collect();
        let sgf = std::fs::read_to_string(d).unwrap();
        let goban = Goban::from_sgf(&sgf, &options.node_description).unwrap();
        render(&goban, &options).unwrap()
    }

    #[test]
    fn full_board() {
        let options = RenderOptions::default();
        let diagram = build_diagram("last_move", &options);
        let expected = "\
┏┯┯┯┯┯┯┯┯┯┯○●●●●┯┯┓
┠┼┼┼┼┼┼┼┼┼┼○○○●○●┼┨
┠┼┼┼○○●○○○┼┼○●●○●●┨
○○○○○●○○●┼○┼┼○●○○○●
○●●●○●●●┼┼┼┼○┼┼┼○●┨
●┼●○┼┼┼┼○┼┼┼●○┼●●┼●
┠●●○┼┼○○┼●○○○●●┼┼●┨
┠●○○○○●○┼○●●●○┼┼┼┼┨
┠●○●┼○●○○○○○●●●●●┼┨
┠┼●●●┼●○●●●●○○○○○●┨
┠┼┼●○┼●●○┼┼○┼┼┼┼┼●┨
┠┼●┼●○┼○┼○○┼┼┼┼┼○●┨
┠●●●○○┼○○┼●○┼┼○┼○●┨
┠●○○○●●●●●●○┼┼○●●┼┨
○●●○●┼●○○○●●○┼┼○●┼┨
┠○○●●●┼●●○○○○○○┼●┼┨
┠○┼○┼●●●○┼┼●●○●●┼┼┨
┠┼○○●┼┼●○┼○●┼●┼┼┼┼┨
┗┷┷┷┷┷┷┷┷┷○┷●┷┷┷┷┷┛";
        assert_eq!(diagram, expected);
    }

    #[test]
    fn labels() {
        let mut options = RenderOptions::default();
        options.label_sides = "nw".parse().unwrap();
        let diagram = build_diagram("last_move", &options);
        let expected = "   ABCDEFGHJKLMNOPQRST
 1 ┏┯┯┯┯┯┯┯┯┯┯○●●●●┯┯┓
 2 ┠┼┼┼┼┼┼┼┼┼┼○○○●○●┼┨
 3 ┠┼┼┼○○●○○○┼┼○●●○●●┨
 4 ○○○○○●○○●┼○┼┼○●○○○●
 5 ○●●●○●●●┼┼┼┼○┼┼┼○●┨
 6 ●┼●○┼┼┼┼○┼┼┼●○┼●●┼●
 7 ┠●●○┼┼○○┼●○○○●●┼┼●┨
 8 ┠●○○○○●○┼○●●●○┼┼┼┼┨
 9 ┠●○●┼○●○○○○○●●●●●┼┨
10 ┠┼●●●┼●○●●●●○○○○○●┨
11 ┠┼┼●○┼●●○┼┼○┼┼┼┼┼●┨
12 ┠┼●┼●○┼○┼○○┼┼┼┼┼○●┨
13 ┠●●●○○┼○○┼●○┼┼○┼○●┨
14 ┠●○○○●●●●●●○┼┼○●●┼┨
15 ○●●○●┼●○○○●●○┼┼○●┼┨
16 ┠○○●●●┼●●○○○○○○┼●┼┨
17 ┠○┼○┼●●●○┼┼●●○●●┼┼┨
18 ┠┼○○●┼┼●○┼○●┼●┼┼┼┼┨
19 ┗┷┷┷┷┷┷┷┷┷○┷●┷┷┷┷┷┛";
        assert_eq!(diagram, expected);
    }

    #[test]
    fn range() {
        let mut options = RenderOptions::default();
        options.goban_range = GobanRange::Ranged(1..7, 0..5);
        let diagram = build_diagram("prob45", &options);
        let expected = "\
┯○○●●┯
○┼○○●┼
┼○●●●┼
○○●┼┼┼
●●┼┼┼┼";
        assert_eq!(diagram, expected);
    }

    #[test]
    fn range_with_labels() {
        let mut options = RenderOptions::default();
        options.label_sides = "nwes".parse().unwrap();
        options.goban_range = GobanRange::Ranged(1..7, 0..5);
        let diagram = build_diagram("prob45", &options);
        println!("{}", diagram);
        let expected = "   BCDEFG
 1 ┯○○●●┯ 1
 2 ○┼○○●┼ 2
 3 ┼○●●●┼ 3
 4 ○○●┼┼┼ 4
 5 ●●┼┼┼┼ 5
   BCDEFG";
        assert_eq!(diagram, expected);
    }

    #[test]
    fn shrink_wrap() {
        let mut options = RenderOptions::default();
        options.goban_range = GobanRange::ShrinkWrap;
        let diagram = build_diagram("prob45", &options);
        let expected = "\
┏┯○○●●┯
○○┼○○●┼
●┼○●●●┼
●○○●┼┼┼
┠●●┼┼┼┼
┠┼┼┼┼┼┼";
        assert_eq!(diagram, expected);
    }
}