Changed the name of some member variables to conform to the convention with
postfix underscore ("varName_").
This commit is contained in:
parent
608d59fb2e
commit
d2f5cd2e23
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* A class for a Mancala board. The following board layout is assumed
|
* A class for a Mancala board_. The following board_ layout is assumed
|
||||||
*
|
*
|
||||||
* Holes num:
|
* Holes num:
|
||||||
*
|
*
|
||||||
|
@ -39,7 +39,7 @@ using namespace std;
|
||||||
/** Constructor.
|
/** Constructor.
|
||||||
*
|
*
|
||||||
* @param holes is the number of holes, including both house and store, in
|
* @param holes is the number of holes, including both house and store, in
|
||||||
* the board. Must be an even number.
|
* the board_. Must be an even number.
|
||||||
* @param seeds is the initial number of seeds per hole.
|
* @param seeds is the initial number of seeds per hole.
|
||||||
*
|
*
|
||||||
* @exception std::invalid_argument.
|
* @exception std::invalid_argument.
|
||||||
|
@ -56,29 +56,29 @@ Board::Board(int houses, int seeds)
|
||||||
{
|
{
|
||||||
ostringstream msg;
|
ostringstream msg;
|
||||||
msg << "'holes' = " << holes << ": "
|
msg << "'holes' = " << holes << ": "
|
||||||
<< "The number of holes in a board must be equal";
|
<< "The number of holes in a board_ must be equal";
|
||||||
throw invalid_argument(msg.str());
|
throw invalid_argument(msg.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
store2 = 0;
|
store2_ = 0;
|
||||||
store1 = holes / 2;
|
store1_ = holes / 2;
|
||||||
|
|
||||||
board.reserve(holes);
|
board_.reserve(holes);
|
||||||
for (int i = 0; i < holes; ++i)
|
for (int i = 0; i < holes; ++i)
|
||||||
{
|
{
|
||||||
if (i == store1 || i == store2)
|
if (i == store1_ || i == store2_)
|
||||||
board.push_back(0);
|
board_.push_back(0);
|
||||||
else
|
else
|
||||||
board.push_back(seeds);
|
board_.push_back(seeds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int & Board::operator[] (int hole)
|
int & Board::operator[] (int hole)
|
||||||
{
|
{
|
||||||
if (hole < 0 || hole > static_cast<int>(board.size()-1))
|
if (hole < 0 || hole > static_cast<int>(board_.size()-1))
|
||||||
throw invalid_argument("argument out of range");
|
throw invalid_argument("argument out of range");
|
||||||
return board[hole];
|
return board_[hole];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -107,20 +107,20 @@ bool Board::sow(int house, Half playersHalf, int * endId, Board::HoleType * endT
|
||||||
if (seeds == 0)
|
if (seeds == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
board[hole] = 0;
|
board_[hole] = 0;
|
||||||
|
|
||||||
int opponentStore = getStoreHole((playersHalf == SOUTH) ? NORTH : SOUTH);
|
int opponentStore = getStoreHole((playersHalf == SOUTH) ? NORTH : SOUTH);
|
||||||
while (seeds)
|
while (seeds)
|
||||||
{
|
{
|
||||||
hole = ( hole == static_cast<int>( board.size())-1 ) ? 0 : hole + 1;
|
hole = ( hole == static_cast<int>( board_.size())-1 ) ? 0 : hole + 1;
|
||||||
if (hole != opponentStore)
|
if (hole != opponentStore)
|
||||||
{
|
{
|
||||||
++board[hole];
|
++board_[hole];
|
||||||
--seeds;
|
--seeds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*endType = (hole == store1 || hole == store2) ? STORE : HOUSE;
|
*endType = (hole == store1_ || hole == store2_) ? STORE : HOUSE;
|
||||||
*endId = *endType == STORE ? hole2store(hole) : hole2house(hole);
|
*endId = *endType == STORE ? hole2store(hole) : hole2house(hole);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -131,17 +131,17 @@ bool Board::sow(int house, Half playersHalf, int * endId, Board::HoleType * endT
|
||||||
bool Board::isHalfEmpty(Half half) const
|
bool Board::isHalfEmpty(Half half) const
|
||||||
{
|
{
|
||||||
int isEmpty = true;
|
int isEmpty = true;
|
||||||
int hole = half == SOUTH ? store2 + 1 : store1 + 1;
|
int hole = half == SOUTH ? store2_ + 1 : store1_ + 1;
|
||||||
int end = half == SOUTH ? store1 : store2;
|
int end = half == SOUTH ? store1_ : store2_;
|
||||||
|
|
||||||
while (hole != end)
|
while (hole != end)
|
||||||
{
|
{
|
||||||
if (board[hole] != 0)
|
if (board_[hole] != 0)
|
||||||
{
|
{
|
||||||
isEmpty = false;
|
isEmpty = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
hole = ( hole == static_cast<int>( board.size())-1 ) ? 0 : hole + 1;
|
hole = ( hole == static_cast<int>( board_.size())-1 ) ? 0 : hole + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return isEmpty;
|
return isEmpty;
|
||||||
|
@ -151,19 +151,19 @@ bool Board::isHalfEmpty(Half half) const
|
||||||
|
|
||||||
int Board::getSeedsInHouse(int house) const
|
int Board::getSeedsInHouse(int house) const
|
||||||
{
|
{
|
||||||
return board[house2hole(house)];
|
return board_[house2hole(house)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Board::getSeedsInStore(int store) const
|
int Board::getSeedsInStore(int store) const
|
||||||
{
|
{
|
||||||
return board[store2hole(store)];
|
return board_[store2hole(store)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Board::Half Board::getHalf (int hole) const
|
Board::Half Board::getHalf (int hole) const
|
||||||
{
|
{
|
||||||
if (hole <= store1 && hole != store2)
|
if (hole <= store1_ && hole != store2_)
|
||||||
return SOUTH;
|
return SOUTH;
|
||||||
else
|
else
|
||||||
return NORTH;
|
return NORTH;
|
||||||
|
@ -177,11 +177,11 @@ int Board::getOppositeHole( int hole ) const
|
||||||
checkRange (hole);
|
checkRange (hole);
|
||||||
|
|
||||||
if ( hole == 0 )
|
if ( hole == 0 )
|
||||||
opposite = board.size() / 2;
|
opposite = board_.size() / 2;
|
||||||
else if ( hole == static_cast<int>(board.size()) / 2 )
|
else if ( hole == static_cast<int>(board_.size()) / 2 )
|
||||||
opposite = 0;
|
opposite = 0;
|
||||||
else
|
else
|
||||||
opposite = board.size() - hole;
|
opposite = board_.size() - hole;
|
||||||
|
|
||||||
return opposite;
|
return opposite;
|
||||||
}
|
}
|
||||||
|
@ -190,7 +190,7 @@ int Board::getOppositeHole( int hole ) const
|
||||||
int Board::hole2house(int hole) const
|
int Board::hole2house(int hole) const
|
||||||
{
|
{
|
||||||
checkRange (hole);
|
checkRange (hole);
|
||||||
return hole < store1 ? hole : hole - 1;
|
return hole < store1_ ? hole : hole - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -200,9 +200,9 @@ int Board::hole2house(int hole) const
|
||||||
int Board::hole2store(int hole) const
|
int Board::hole2store(int hole) const
|
||||||
{
|
{
|
||||||
checkRange (hole);
|
checkRange (hole);
|
||||||
if ( hole == store1 )
|
if ( hole == store1_ )
|
||||||
return 1;
|
return 1;
|
||||||
else if ( hole == store2)
|
else if ( hole == store2_)
|
||||||
return 2;
|
return 2;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -210,16 +210,16 @@ int Board::hole2store(int hole) const
|
||||||
|
|
||||||
int Board::house2hole(int house) const
|
int Board::house2hole(int house) const
|
||||||
{
|
{
|
||||||
if (house < 1 || house > static_cast<int>(board.size()-2))
|
if (house < 1 || house > static_cast<int>(board_.size()-2))
|
||||||
throw invalid_argument("Invalid house number");
|
throw invalid_argument("Invalid house number");
|
||||||
return house < store1 ? house : house + 1;
|
return house < store1_ ? house : house + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Board::store2hole(int store) const
|
int Board::store2hole(int store) const
|
||||||
{
|
{
|
||||||
if (store < 1 || store > 2)
|
if (store < 1 || store > 2)
|
||||||
throw invalid_argument("Invalid store number");
|
throw invalid_argument("Invalid store number");
|
||||||
return store == 1 ? store1 : store2;
|
return store == 1 ? store1_ : store2_;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,9 +230,9 @@ int Board::getHole(int num, HoleType type) const
|
||||||
int hole;
|
int hole;
|
||||||
if (type == HOUSE)
|
if (type == HOUSE)
|
||||||
{
|
{
|
||||||
if (num < 1 || num > board.size() - 2)
|
if (num < 1 || num > board_.size() - 2)
|
||||||
throw invalid_argument("Invalid house number");
|
throw invalid_argument("Invalid house number");
|
||||||
hole = (num < store1) ? num : num + 1;
|
hole = (num < store1_) ? num : num + 1;
|
||||||
}
|
}
|
||||||
else if (type == STORE)
|
else if (type == STORE)
|
||||||
{
|
{
|
||||||
|
@ -248,9 +248,9 @@ int Board::getStoreHole(Half half) const
|
||||||
{
|
{
|
||||||
int hole;
|
int hole;
|
||||||
if (half == SOUTH)
|
if (half == SOUTH)
|
||||||
hole = store1;
|
hole = store1_;
|
||||||
else if (half == NORTH)
|
else if (half == NORTH)
|
||||||
hole = store2;
|
hole = store2_;
|
||||||
else
|
else
|
||||||
throw invalid_argument("Only store for half SOUTH and NORTH");
|
throw invalid_argument("Only store for half SOUTH and NORTH");
|
||||||
return hole;
|
return hole;
|
||||||
|
@ -258,36 +258,14 @@ int Board::getStoreHole(Half half) const
|
||||||
|
|
||||||
size_t Board::size () const
|
size_t Board::size () const
|
||||||
{
|
{
|
||||||
return board.size();
|
return board_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Board::print(std::ostream & out) const
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
std::ostringstream s1, s2, s3;
|
|
||||||
|
|
||||||
for ( i = 1; i < static_cast<int>(board.size())/2; ++i )
|
|
||||||
{
|
|
||||||
s1 << getSeedsInHouse( board.size()-1 - i ) << "\t";
|
|
||||||
s2 << "\t";
|
|
||||||
s3 << getSeedsInHouse( i ) << "\t";
|
|
||||||
}
|
|
||||||
|
|
||||||
out << "\t" << s1.str() << "\n";
|
|
||||||
out << getSeedsInStore( 2 ) << s2.str() << "\t "<< getSeedsInStore( 1 ) << "\n";
|
|
||||||
out << "\t" << s3.str() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Board::checkRange( int hole ) const
|
bool Board::checkRange( int hole ) const
|
||||||
{
|
{
|
||||||
if ( hole < 0 || hole > static_cast<int>(board.size()-1) )
|
if ( hole < 0 || hole > static_cast<int>(board_.size()-1) )
|
||||||
throw invalid_argument("Invalid hole number");
|
throw invalid_argument("Invalid hole number");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Board::~Board()
|
|
||||||
// {
|
|
||||||
// delete[] board;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
25
src/Board.h
25
src/Board.h
|
@ -12,37 +12,32 @@ public:
|
||||||
|
|
||||||
Board();
|
Board();
|
||||||
Board(int houses, int seeds);
|
Board(int houses, int seeds);
|
||||||
// ~Board();
|
|
||||||
|
|
||||||
int & operator[] (int hole);
|
int & operator[] (int hole);
|
||||||
|
|
||||||
// bool sow(int house, Half playersHalf, int * endId, HoleType * endType);
|
size_t size() const;
|
||||||
|
|
||||||
int getSeedsInHouse(int house) const;
|
int getSeedsInHouse(int house) const;
|
||||||
int getSeedsInStore(int store) const;
|
int getSeedsInStore(int store) const;
|
||||||
int getOppositeHole(int hole) const;
|
|
||||||
void print ( std::ostream & out) const;
|
|
||||||
|
|
||||||
bool isHalfEmpty(Half half) const;
|
int getStoreHole(Half Half) const;
|
||||||
|
int getOppositeHole(int hole) const;
|
||||||
|
|
||||||
int store2hole(int store) const;
|
int store2hole(int store) const;
|
||||||
int hole2store(int hole) const;
|
int hole2store(int hole) const;
|
||||||
int hole2house(int hole) const;
|
int hole2house(int hole) const;
|
||||||
|
|
||||||
int getStoreHole(Half Half) const;
|
|
||||||
int house2hole(int house) const;
|
int house2hole(int house) const;
|
||||||
|
|
||||||
Half getHalf(int hole) const;
|
Half getHalf(int hole) const;
|
||||||
|
bool isHalfEmpty(Half half) const;
|
||||||
size_t size() const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
int store1;
|
int store1_;
|
||||||
int store2;
|
int store2_;
|
||||||
std::vector<int> board;
|
std::vector<int> board_;
|
||||||
|
|
||||||
bool checkRange (int hole) const;
|
bool checkRange (int hole) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -23,7 +23,6 @@ public:
|
||||||
virtual std::string getActivePlayerName() const;
|
virtual std::string getActivePlayerName() const;
|
||||||
virtual std::string getPlayer1Name () const;
|
virtual std::string getPlayer1Name () const;
|
||||||
virtual std::string getPlayer2Name () const;
|
virtual std::string getPlayer2Name () const;
|
||||||
// Board & getBoard() const;
|
|
||||||
|
|
||||||
virtual bool makeMove( int house );
|
virtual bool makeMove( int house );
|
||||||
virtual int getSeedsInHouse( int house ) const;
|
virtual int getSeedsInHouse( int house ) const;
|
||||||
|
|
|
@ -16,7 +16,7 @@ void Session::newGame ()
|
||||||
{
|
{
|
||||||
// todo: do this completely different
|
// todo: do this completely different
|
||||||
// delete game_;
|
// delete game_;
|
||||||
Game * g = new Game( factory.createKalah(), 12);
|
Game * g = new Game( factory_.createKalah(), 12);
|
||||||
g->player1("P1", true);
|
g->player1("P1", true);
|
||||||
g->player2("P2", true);
|
g->player2("P2", true);
|
||||||
game_ = g;
|
game_ = g;
|
||||||
|
|
|
@ -16,7 +16,7 @@ public:
|
||||||
private:
|
private:
|
||||||
IGame * game_;
|
IGame * game_;
|
||||||
GuiWrapper * gui_;
|
GuiWrapper * gui_;
|
||||||
EngineFactory factory;
|
EngineFactory factory_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue