1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <memory>
#include <tuple>
#include <map>
struct Position { int x, y; };
bool operator<(Position const &a, Position const &b)
{
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
}
std::ostream &operator<<(std::ostream &os, Position const &p)
{
return os << '(' << p.x << ", " << p.y << ')';
}
struct Board
{
class Piece
{
Position p;
friend struct ::Board;
public:
Position const &pos = p; //could just as well be a function
Piece(Position const &p)
: p(p)
{
}
};
void swap(Position const &source, Position const &target)
{
std::cout << "Swapping " << source << " to " << target << std::endl;
pieces[target].swap(pieces[source]);
pieces[target]->p = target;
pieces[source]->p = source;
std::cout << "Swapped " << source << " to " << target << std::endl;
}
void add(Position const &p)
{
pieces[p] = std::unique_ptr<Piece>(new Piece(p));
}
Piece *at(Position const &p)
{
return pieces[p].get();
}
private:
std::map<Position, std::unique_ptr<Piece>> pieces;
};
int main()
{
Board b;
b.add({1, 1});
b.add({1, 2});
b.add({2, 1});
b.add({2, 2});
std::cout << "First swap:" << std::endl;
b.swap({1, 1}, {1, 2});
std::cout << std::endl
<< "Second swap:" << std::endl;
b.swap(b.at({2, 1})->pos, {2, 2});
}
|