refactor private member names and make more methods private

This commit is contained in:
Andrea Blankenstijn 2022-01-05 16:04:01 +01:00
parent a8189b4723
commit c6371bb6bd
4 changed files with 40 additions and 40 deletions

Binary file not shown.

View File

@ -20,13 +20,13 @@ void Board2D::add_unit(const Coord c)
if (!(space.capacity >= space.value)) throw InvalidMove {"Space is overloaded"};
space.value++;
_units[space.owner]++;
cout << "total: " << _units[space.owner] << endl;
units[space.owner]++;
cout << "total: " << units[space.owner] << endl;
}
auto Board2D::begin() const noexcept -> const_iterator
{
return _spaces.cbegin();
return spaces.cbegin();
}
void Board2D::capture(const Coord c, const Player p) noexcept
@ -35,47 +35,47 @@ void Board2D::capture(const Coord c, const Player p) noexcept
if (s.owner != p) {
cout << "= P" << static_cast<int>(p) << " captures " << s.coord.x << ","
<< s.coord.y << " : +" << s.value << endl;
_units[s.owner] -= s.value;
_units[p] += s.value;
units[s.owner] -= s.value;
units[p] += s.value;
s.owner = p;
}
}
auto Board2D::end() const noexcept -> const_iterator
{
return _spaces.cend();
return spaces.cend();
}
void Board2D::init(const size_type w, const size_type h)
void Board2D::init(const size_type w_, const size_type h_)
{
_width = w;
_height = h;
w = w_;
h = h_;
_spaces.reserve(w * h); // NOLINT
for (size_type y = 0; y < h; y++) {
for (size_type x = 0; x < w; x++) {
_spaces.push_back(Coord {x, y});
if (x == 0 || x == w - 1) {
if (y == 0 || y == h - 1) {
_spaces.back().capacity = 1;
spaces.reserve(w_ * h_); // NOLINT
for (size_type y = 0; y < h_; y++) {
for (size_type x = 0; x < w_; x++) {
spaces.push_back(Coord {x, y});
if (x == 0 || x == w_ - 1) {
if (y == 0 || y == h_ - 1) {
spaces.back().capacity = 1;
}
else {
_spaces.back().capacity = 2;
spaces.back().capacity = 2;
}
}
else {
if (y == 0 || y == h - 1) {
_spaces.back().capacity = 2;
if (y == 0 || y == h_ - 1) {
spaces.back().capacity = 2;
}
else {
_spaces.back().capacity = 3;
spaces.back().capacity = 3;
}
}
}
}
_units.emplace(Player::P1, 0);
_units.emplace(Player::P2, 0);
units.emplace(Player::P1, 0);
units.emplace(Player::P2, 0);
}
auto Board2D::is_overloaded(Coord c) const noexcept -> bool
@ -121,16 +121,16 @@ auto Board2D::neighbours(const Coord c) const noexcept -> vector<Coord>
vector<Coord> nghbrs;
if (c.x > 0) nghbrs.emplace_back(Coord {c.x - 1, c.y});
if (c.x < _width - 1) nghbrs.emplace_back(Coord {c.x + 1, c.y});
if (c.x < w - 1) nghbrs.emplace_back(Coord {c.x + 1, c.y});
if (c.y > 0) nghbrs.emplace_back(Coord {c.x, c.y - 1});
if (c.y < _height - 1) nghbrs.emplace_back(Coord {c.x, c.y + 1});
if (c.y < h - 1) nghbrs.emplace_back(Coord {c.x, c.y + 1});
return nghbrs;
}
auto Board2D::operator()(const Coord c) const noexcept -> const Board2D::Space&
{
return _spaces.at(coord2idx(c));
return spaces.at(coord2idx(c));
}
void Board2D::remove_unit(const Coord c)
@ -139,20 +139,20 @@ void Board2D::remove_unit(const Coord c)
const auto owner = s.owner;
if (s.value > 1) {
s.value--;
_units[owner]--;
units[owner]--;
}
else if (s.value == 1) {
s.reset();
_units[owner]--;
units[owner]--;
}
else throw logic_error("Space is empty");
cout << "- remove " << c.x << "," << c.y << " "
<< "total: " << _units[owner] << endl;
<< "total: " << units[owner] << endl;
}
auto Board2D::size() const noexcept -> pair<size_type, size_type>
{
return {_width, _height};
return {w, h};
}
auto Board2D::spread(const Coord src, const vector<Coord>& targets, const Player p)
@ -188,7 +188,7 @@ auto Board2D::winner(const Board2D& b) noexcept -> Player
{
Player winner {Player::NONE};
for (auto [key, value] : b._units) {
for (auto [key, value] : b.units) {
if (value > 0) {
if (winner == Player::NONE) winner = key;
else return Player::NONE;
@ -200,15 +200,15 @@ auto Board2D::winner(const Board2D& b) noexcept -> Player
auto Board2D::at(const Coord c) noexcept -> Space&
{
return _spaces.at(coord2idx(c));
return spaces.at(coord2idx(c));
}
auto Board2D::at(const Coord c) const noexcept -> const Space&
{
return _spaces.at(coord2idx(c));
return spaces.at(coord2idx(c));
}
auto Board2D::coord2idx(const Coord c) const noexcept -> size_type
{
return c.y * _width + c.x;
return c.y * w + c.x;
}

View File

@ -51,16 +51,13 @@ namespace core
Board2D() = default;
Board2D(size_type, size_type);
void add_unit(Coord);
[[nodiscard]] auto begin() const noexcept -> const_iterator;
void capture(Coord, Player) noexcept;
[[nodiscard]] auto end() const noexcept -> const_iterator;
void init(size_type, size_type);
[[nodiscard]] auto is_overloaded(Coord) const noexcept -> bool;
auto move(Coord, Player) -> Generator<std::pair<Space, Player>>;
[[nodiscard]] auto neighbours(Coord) const noexcept -> std::vector<Coord>;
[[nodiscard]] auto operator()(Coord) const noexcept -> const Space&;
void remove_unit(Coord);
[[nodiscard]] auto size() const noexcept -> std::pair<size_type, size_type>;
auto spread(Coord, const std::vector<Coord>&, Player) -> Generator<Space>;
[[nodiscard]] auto winner() const noexcept -> Player;
@ -68,14 +65,17 @@ namespace core
[[nodiscard]] static auto winner(const Board2D&) noexcept -> Player;
private:
size_type _height;
Spaces _spaces;
std::map<Player, int> _units;
size_type _width;
size_type h;
Spaces spaces;
std::map<Player, int> units;
size_type w;
void add_unit(Coord);
[[nodiscard]] auto at(Coord) noexcept -> Space&;
[[nodiscard]] auto at(Coord) const noexcept -> const Space&;
void capture(Coord, Player) noexcept;
[[nodiscard]] auto coord2idx(Coord) const noexcept -> size_type;
void remove_unit(Coord);
};
}

Binary file not shown.