]>
Commit | Line | Data |
---|---|---|
82d6eed5 SW |
1 | #include "sliding_tile_lib.h" |
2 | ||
32688d85 | 3 | #include <istream> |
cada47bf | 4 | #include <ostream> |
f3f55aff | 5 | #include <stdexcept> |
32688d85 | 6 | |
82d6eed5 SW |
7 | signed char adjacent[BOARD_SIZE][5] = { |
8 | 1, 4, -1, -1, -1, | |
9 | 0, 2, 5, -1, -1, | |
10 | 1, 3, 6, -1, -1, | |
11 | 2, 7, -1, -1, -1, | |
12 | 0, 5, 8, -1, -1, | |
13 | 1, 4, 6, 9, -1, | |
14 | 2, 5, 7, 10, -1, | |
15 | 3, 6, 11, -1, -1, | |
16 | 4, 9, 12, -1, -1, | |
17 | 5, 8, 10, 13, -1, | |
18 | 6, 9, 11, 14, -1, | |
19 | 7, 10, 15, -1, -1, | |
20 | 8, 13, -1, -1, -1, | |
21 | 9, 12, 14, -1, -1, | |
22 | 10, 13, 15, -1, -1, | |
23 | 11, 14, -1, -1, -1, | |
24 | }; | |
32688d85 | 25 | |
cea272cf | 26 | bool Board::is_valid() const { |
b18667f2 SW |
27 | bool seen[BOARD_SIZE]; |
28 | for (int i = 0; i < BOARD_SIZE; i++) { | |
29 | seen[i] = false; | |
30 | } | |
31 | ||
32 | for (int i = 0; i < BOARD_SIZE; i++) { | |
33 | if (board[i] < 0 || board[i] >= BOARD_SIZE || seen[board[i]]) { | |
34 | return false; | |
35 | } | |
36 | seen[board[i]] = true; | |
37 | } | |
38 | ||
39 | // Redundant because pigeon-hole-principle, but check anyway | |
40 | for (int i = 0; i < BOARD_SIZE; i++) { | |
41 | if (!seen[i]) { | |
42 | return false; | |
43 | } | |
44 | } | |
45 | ||
46 | return true; | |
47 | } | |
48 | ||
32688d85 SW |
49 | std::istream& operator>>(std::istream& is, Board& board) { |
50 | for (int i = 0; i < BOARD_SIZE; i++) { | |
51 | if (!is.good()) { | |
52 | is.setstate(std::istream::failbit); | |
53 | break; | |
54 | } | |
55 | if (i > 0 && is.get() != ',') { | |
56 | is.setstate(std::istream::failbit); | |
57 | break; | |
58 | } | |
59 | int numeric; | |
60 | is >> numeric; | |
19bd29f9 | 61 | board.board[i] = numeric; |
32688d85 | 62 | } |
b18667f2 SW |
63 | if (!board.is_valid()) { |
64 | is.setstate(std::istream::failbit); | |
65 | } | |
32688d85 SW |
66 | return is; |
67 | } | |
f3f55aff | 68 | |
cada47bf SW |
69 | std::ostream& operator<<(std::ostream& os, const Board& board) { |
70 | for (int i = 0; i < BOARD_SIZE; i++) { | |
71 | if (i > 0) { | |
72 | os << " "; | |
73 | } | |
74 | os << int(board.board[i]); | |
75 | } | |
76 | return os; | |
77 | } | |
78 | ||
cea272cf | 79 | signed char Board::hole() const { |
f3f55aff SW |
80 | for (int i = 0; i < BOARD_SIZE; i++) { |
81 | if (board[i] == 0) { | |
82 | return i; | |
83 | } | |
84 | } | |
85 | throw std::runtime_error("Board with no hole"); | |
86 | } |