]> git.scottworley.com Git - slidingtile/blame - sliding_tile_lib.cc
Input validation
[slidingtile] / sliding_tile_lib.cc
CommitLineData
82d6eed5
SW
1#include "sliding_tile_lib.h"
2
32688d85
SW
3#include <istream>
4
82d6eed5
SW
5signed char adjacent[BOARD_SIZE][5] = {
6 1, 4, -1, -1, -1,
7 0, 2, 5, -1, -1,
8 1, 3, 6, -1, -1,
9 2, 7, -1, -1, -1,
10 0, 5, 8, -1, -1,
11 1, 4, 6, 9, -1,
12 2, 5, 7, 10, -1,
13 3, 6, 11, -1, -1,
14 4, 9, 12, -1, -1,
15 5, 8, 10, 13, -1,
16 6, 9, 11, 14, -1,
17 7, 10, 15, -1, -1,
18 8, 13, -1, -1, -1,
19 9, 12, 14, -1, -1,
20 10, 13, 15, -1, -1,
21 11, 14, -1, -1, -1,
22};
32688d85 23
b18667f2
SW
24bool Board::is_valid() {
25 bool seen[BOARD_SIZE];
26 for (int i = 0; i < BOARD_SIZE; i++) {
27 seen[i] = false;
28 }
29
30 for (int i = 0; i < BOARD_SIZE; i++) {
31 if (board[i] < 0 || board[i] >= BOARD_SIZE || seen[board[i]]) {
32 return false;
33 }
34 seen[board[i]] = true;
35 }
36
37 // Redundant because pigeon-hole-principle, but check anyway
38 for (int i = 0; i < BOARD_SIZE; i++) {
39 if (!seen[i]) {
40 return false;
41 }
42 }
43
44 return true;
45}
46
32688d85
SW
47std::istream& operator>>(std::istream& is, Board& board) {
48 for (int i = 0; i < BOARD_SIZE; i++) {
49 if (!is.good()) {
50 is.setstate(std::istream::failbit);
51 break;
52 }
53 if (i > 0 && is.get() != ',') {
54 is.setstate(std::istream::failbit);
55 break;
56 }
57 int numeric;
58 is >> numeric;
19bd29f9 59 board.board[i] = numeric;
32688d85 60 }
b18667f2
SW
61 if (!board.is_valid()) {
62 is.setstate(std::istream::failbit);
63 }
32688d85
SW
64 return is;
65}