Skip to content
Browse files

[refactor] Delete MoveTo in CGame & Board

  • Loading branch information...
1 parent 4aed016 commit de178f46262ff788016dffdc96b38cd08c65f581 @xinhuang committed Feb 24, 2012
View
3 ChineseChecker_Lib/Presenter/Board.h
@@ -20,11 +20,12 @@ class Board
virtual std::vector<Square*> GetSquaresInFormation(const Point& goalLocation) const;
virtual Square* SquareAt(const Point& location);
+ virtual bool IsAccessible( const Square& from, const Square& to ) const;
+
protected:
void InitializeGoalSquares();
int GetSquareIndex(const Point& location) const;
int GetSquareIndex(int x, int y) const;
- bool IsAccessible( const Square& from, const Square& to ) const;
bool IsAdjacent( const Square &from, const Square &to ) const;
View
9 ChineseChecker_Lib/Presenter/Piece.cpp
@@ -4,17 +4,21 @@
#include <cassert>
+#include "Board.h"
+
struct Piece::Data
{
Data(int id) : id(id) {}
const int id;
Square* square;
+ Board* board;
};
-Piece::Piece(int id) : _d(new Data(id))
+Piece::Piece(Board* board, int id) : _d(new Data(id))
{
_d->square = nullptr;
+ _d->board = board;
}
Piece::~Piece()
@@ -24,6 +28,9 @@ Piece::~Piece()
bool Piece::MoveTo( Square* square )
{
+ if (square != nullptr && _d->square != nullptr
+ && !_d->board->IsAccessible(*get_Square(), *square))
+ return false;
if (_d->square != nullptr)
{
assert(this == _d->square->get_Piece());
View
3 ChineseChecker_Lib/Presenter/Piece.h
@@ -2,11 +2,12 @@
#define __PIECE_H__
class Square;
+class Board;
class Piece
{
public:
- Piece(int id);
+ Piece(Board* board, int id);
virtual ~Piece();
virtual const Square* get_Square() const;
View
2 ChineseChecker_Lib/Presenter/PieceGroup.cpp
@@ -50,7 +50,7 @@ void PieceGroup::CreatePieces( int pieceTotal )
_d->pieces.resize(pieceTotal);
for (int i = 0; i < pieceTotal; ++i)
{
- _d->pieces[i] = new Piece(get_Id());
+ _d->pieces[i] = new Piece(_d->board, get_Id());
}
}
View
9 ChineseChecker_UnitTest/PieceTest.cpp
@@ -4,6 +4,7 @@ using ::testing::Return;
using ::testing::NiceMock;
#include "mocks/PieceMock.h"
+#include "mocks/BoardMock.h"
#include "mocks/SquareMock.h"
#include <Presenter/Piece.h>
@@ -13,21 +14,25 @@ class PieceTest : public ::testing::Test
public:
virtual void SetUp()
{
- _sut = new Piece(Id);
+ _board = new BoardMock();
+ _sut = new Piece(_board, Id);
}
virtual void TearDown()
{
delete _sut; _sut = nullptr;
+ delete _board; _board = nullptr;
}
protected:
static const int Id = 2;
Piece* _sut;
+ BoardMock* _board;
+ SquareMock* _locSquare;
};
TEST_F(PieceTest, Constructor_Typical)
{
- Piece* sut = new Piece(Id);
+ Piece* sut = new Piece(nullptr, Id);
ASSERT_NE(nullptr, sut);
ASSERT_EQ(Id, sut->get_Id());
View
3 ChineseChecker_UnitTest/mocks/PieceMock.h
@@ -7,7 +7,8 @@
class PieceMock : public Piece
{
public:
- PieceMock() : Piece(-1) {}
+ PieceMock() : Piece(nullptr, -1) {}
+ PieceMock(Board* board) : Piece(board, -1) {}
MOCK_CONST_METHOD0(get_Square, const Square*());
MOCK_METHOD0(get_Square, Square*());

0 comments on commit de178f4

Please sign in to comment.
Something went wrong with that request. Please try again.