littlewing/
board.rs

1use std::prelude::v1::*;
2
3pub fn draw(squares: Vec<String>) -> String {
4    let line = "  +---+---+---+---+---+---+---+---+\n";
5    let file = "";
6    draw_with(squares, line, file)
7}
8
9pub fn draw_with_coordinates(squares: Vec<String>) -> String {
10    let line = "  +---+---+---+---+---+---+---+---+\n";
11    let file = "    a   b   c   d   e   f   g   h\n";
12    draw_with(squares, line, file)
13}
14
15pub fn draw_compact_with_coordinates(squares: Vec<String>) -> String {
16    let line = "+--------+\n";
17    let file = " abcdefgh\n";
18    draw_with(squares, line, file)
19}
20
21fn draw_with(squares: Vec<String>, line: &str, file: &str) -> String {
22    debug_assert!(squares.len() == 64);
23    let with_spaces = line.len() > 12;
24    let with_coords = !file.is_empty();
25    let mut out = String::new();
26    out.push_str(line);
27    for i in (0..8).rev() {
28        if with_spaces {
29            out.push_str("  ");
30        } else {
31            out.push_str("|");
32        }
33        for j in 0..8 {
34            let s = &squares[8 * i + j];
35            if with_spaces {
36                out.push_str(&format!("| {} ", s));
37            } else {
38                out.push_str(&format!("{}", s));
39            }
40        }
41        if with_coords {
42            out.push_str(&format!("| {}\n", i + 1));
43        } else {
44            out.push_str("|\n");
45        }
46        if with_spaces {
47            out.push_str(line);
48        }
49    }
50    if !with_spaces {
51        out.push_str(line);
52    }
53    out.push_str(file);
54    out
55}
56
57#[allow(dead_code)]
58static PIECES_WITHOUT_COORDS: &str = "  \
59  +---+---+---+---+---+---+---+---+
60  | r | n | b | q | k | b | n | r |
61  +---+---+---+---+---+---+---+---+
62  | p | p | p | p | p | p | p | p |
63  +---+---+---+---+---+---+---+---+
64  |   |   |   |   |   |   |   |   |
65  +---+---+---+---+---+---+---+---+
66  |   |   |   |   |   |   |   |   |
67  +---+---+---+---+---+---+---+---+
68  |   |   |   |   | P |   |   |   |
69  +---+---+---+---+---+---+---+---+
70  |   |   |   |   |   |   |   |   |
71  +---+---+---+---+---+---+---+---+
72  | P | P | P | P |   | P | P | P |
73  +---+---+---+---+---+---+---+---+
74  | R | N | B | Q | K | B | N | R |
75  +---+---+---+---+---+---+---+---+
76";
77
78#[allow(dead_code)]
79static PIECES_WITH_COORDS: &str = "  \
80  +---+---+---+---+---+---+---+---+
81  | r | n | b | q | k | b | n | r | 8
82  +---+---+---+---+---+---+---+---+
83  | p | p | p | p | p | p | p | p | 7
84  +---+---+---+---+---+---+---+---+
85  |   |   |   |   |   |   |   |   | 6
86  +---+---+---+---+---+---+---+---+
87  |   |   |   |   |   |   |   |   | 5
88  +---+---+---+---+---+---+---+---+
89  |   |   |   |   | P |   |   |   | 4
90  +---+---+---+---+---+---+---+---+
91  |   |   |   |   |   |   |   |   | 3
92  +---+---+---+---+---+---+---+---+
93  | P | P | P | P |   | P | P | P | 2
94  +---+---+---+---+---+---+---+---+
95  | R | N | B | Q | K | B | N | R | 1
96  +---+---+---+---+---+---+---+---+
97    a   b   c   d   e   f   g   h
98";
99
100#[allow(dead_code)]
101static BITBOARD_WITH_COORDS: &str = " \
102 Bitboard  (0xFFFF00001000EFFF)
103+--------+
104|11111111| 8
105|11111111| 7
106|00000000| 6
107|00000000| 5
108|00001000| 4
109|00000000| 3
110|11110111| 2
111|11111111| 1
112+--------+
113 abcdefgh
114";
115
116#[cfg(test)]
117mod tests {
118    use bitboard::BitboardExt;
119    use color::*;
120    use common::*;
121    use fen::FEN;
122    use game::Game;
123    use piece_move::PieceMove;
124    use piece_move_generator::PieceMoveGenerator;
125    use square::*;
126    use super::*;
127
128    #[test]
129    fn test_draw() {
130        colorize(false);
131        let mut game = Game::from_fen(DEFAULT_FEN).unwrap();
132        game.make_move(PieceMove::new(E2, E4, DOUBLE_PAWN_PUSH));
133        assert_eq!(format!("{}", game), PIECES_WITHOUT_COORDS);
134    }
135
136    #[test]
137    fn test_draw_with_coordinates() {
138        colorize(false);
139        let mut game = Game::from_fen(DEFAULT_FEN).unwrap();
140        game.show_coordinates = true;
141        game.make_move(PieceMove::new(E2, E4, DOUBLE_PAWN_PUSH));
142        assert_eq!(format!("{}", game), PIECES_WITH_COORDS);
143    }
144
145    #[test]
146    fn test_draw_compact_with_coordinates() {
147        colorize(false);
148        let mut game = Game::from_fen(DEFAULT_FEN).unwrap();
149        game.make_move(PieceMove::new(E2, E4, DOUBLE_PAWN_PUSH));
150        let bb = game.bitboards[WHITE as usize] | game.bitboards[BLACK as usize];
151        assert_eq!(format!("{}", bb.to_debug_string()), BITBOARD_WITH_COORDS);
152    }
153}