]> git.scottworley.com Git - slidingtile/blob - sliding_tile_lib.cc
5037c8edbda5877b2ab2d0d2e6ac37d7cd981461
[slidingtile] / sliding_tile_lib.cc
1 #include "sliding_tile_lib.h"
2
3 #include <istream>
4 #include <ostream>
5 #include <stdexcept>
6
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 };
25
26 bool Board::is_valid() const {
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
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;
61 board.board[i] = numeric;
62 }
63 if (!board.is_valid()) {
64 is.setstate(std::istream::failbit);
65 }
66 return is;
67 }
68
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
79 signed char Board::hole() const {
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 }