]> git.scottworley.com Git - slidingtile/commitdiff
A where's-the-hole function.
authorScott Worley <scottworley@scottworley.com>
Fri, 1 Jan 2016 04:34:39 +0000 (20:34 -0800)
committerScott Worley <scottworley@scottworley.com>
Fri, 1 Jan 2016 04:34:39 +0000 (20:34 -0800)
sliding_tile_lib.cc
sliding_tile_lib.h
sliding_tile_lib_test.cc

index 3b7806bd0a204eb113146871eb5deaedcfbb7fd3..b8ddc154a1a829e3ce7843678c34901334ef7ddf 100644 (file)
@@ -1,6 +1,7 @@
 #include "sliding_tile_lib.h"
 
 #include <istream>
+#include <stdexcept>
 
 signed char adjacent[BOARD_SIZE][5] = {
   1,   4,   -1,  -1,  -1,
@@ -63,3 +64,12 @@ std::istream& operator>>(std::istream& is, Board& board) {
   }
   return is;
 }
+
+signed char Board::hole() {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    if (board[i] == 0) {
+      return i;
+    }
+  }
+  throw std::runtime_error("Board with no hole");
+}
index 89c65c1ee51c7b3ecc2a0e6c561c2a2e6c78a9db..84d422e2faaf09d44c663a253c1cc02c94418cb1 100644 (file)
@@ -9,6 +9,7 @@ const int BOARD_SIZE = BOARD_DIM * BOARD_DIM;
 struct Board {
   signed char board[BOARD_SIZE];
   bool is_valid();
+  signed char hole();
 };
 std::istream& operator>>(std::istream& is, Board& board);
 
index 7f032a436761d278e8fdf61230f0d94607516408..12cc46b8a051394b03d7fd93b7d724a25917c92c 100644 (file)
@@ -76,3 +76,13 @@ TEST(Board, HighTileInput) {
   is >> b;
   EXPECT_TRUE(is.fail());
 }
+
+TEST(Board, Hole) {
+  Board b{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
+  EXPECT_EQ(8, b.hole());
+}
+
+TEST(Board, NoHole) {
+  Board b{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
+  EXPECT_THROW(b.hole(), std::runtime_error);
+}