From f3f55aff4e3346b297215050c2076c821fd60c83 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 31 Dec 2015 20:34:39 -0800 Subject: [PATCH 1/1] A where's-the-hole function. --- sliding_tile_lib.cc | 10 ++++++++++ sliding_tile_lib.h | 1 + sliding_tile_lib_test.cc | 10 ++++++++++ 3 files changed, 21 insertions(+) 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); +} -- 2.44.1