From: Scott Worley Date: Fri, 1 Jan 2016 04:34:39 +0000 (-0800) Subject: A where's-the-hole function. X-Git-Url: http://git.scottworley.com/slidingtile/commitdiff_plain/f3f55aff4e3346b297215050c2076c821fd60c83?ds=sidebyside;hp=b18667f24728cb960b0c60fa55824062582278bf A where's-the-hole function. --- diff --git a/sliding_tile_lib.cc b/sliding_tile_lib.cc index 3b7806b..b8ddc15 100644 --- a/sliding_tile_lib.cc +++ b/sliding_tile_lib.cc @@ -1,6 +1,7 @@ #include "sliding_tile_lib.h" #include +#include 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"); +} diff --git a/sliding_tile_lib.h b/sliding_tile_lib.h index 89c65c1..84d422e 100644 --- a/sliding_tile_lib.h +++ b/sliding_tile_lib.h @@ -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); diff --git a/sliding_tile_lib_test.cc b/sliding_tile_lib_test.cc index 7f032a4..12cc46b 100644 --- a/sliding_tile_lib_test.cc +++ b/sliding_tile_lib_test.cc @@ -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); +}