From: Scott Worley Date: Sat, 9 Jan 2016 10:22:49 +0000 (-0800) Subject: operator< for Board X-Git-Url: http://git.scottworley.com/slidingtile/commitdiff_plain/310a7132ffc617b93969703de7f3bf1c5ea0fa30?ds=sidebyside;hp=9b1ce99505059bba03b178c45721c73f3d36fb35 operator< for Board --- diff --git a/sliding_tile_lib.cc b/sliding_tile_lib.cc index 6b42f28..e4f2f12 100644 --- a/sliding_tile_lib.cc +++ b/sliding_tile_lib.cc @@ -62,6 +62,17 @@ bool Board::operator!=(const Board& o) const { return !operator==(o); } +bool Board::operator<(const Board& o) const { + for (int i = 0; i < BOARD_SIZE; i++) { + if (board[i] < o.board[i]) { + return true; + } else if (board[i] > o.board[i]) { + return false; + } + } + return false; +} + std::istream& operator>>(std::istream& is, Board& board) { for (int i = 0; i < BOARD_SIZE; i++) { if (!is.good()) { diff --git a/sliding_tile_lib.h b/sliding_tile_lib.h index 300ecdd..772817e 100644 --- a/sliding_tile_lib.h +++ b/sliding_tile_lib.h @@ -22,6 +22,7 @@ struct Board { int distance(const InvertedBoard& invo) const; bool operator==(const Board& o) const; bool operator!=(const Board& o) const; + bool operator<(const Board& o) const; }; std::istream& operator>>(std::istream& is, Board& board); std::ostream& operator<<(std::ostream& os, const Board& board); diff --git a/sliding_tile_lib_test.cc b/sliding_tile_lib_test.cc index 54d7db6..5597a1b 100644 --- a/sliding_tile_lib_test.cc +++ b/sliding_tile_lib_test.cc @@ -42,6 +42,8 @@ TEST(Board, Equal) { EXPECT_TRUE(b1 == b2); EXPECT_FALSE(b1 != b2); EXPECT_EQ(b1, b2); + EXPECT_FALSE(b1 < b2); + EXPECT_FALSE(b2 < b1); } TEST(Board, NotEqual) { @@ -50,6 +52,8 @@ TEST(Board, NotEqual) { EXPECT_FALSE(b1 == b2); EXPECT_TRUE(b1 != b2); EXPECT_NE(b1, b2); + EXPECT_TRUE(b1 < b2); + EXPECT_FALSE(b2 < b1); } TEST(Board, GoodInput) {