Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- using namespace std;
- class State {
- private:
- int heapc;
- int *coins;
- public:
- // State with h heaps, where the i-th heap starts with c[i] coins.
- State(int h, const int c[]) {
- heapc = h;
- delete[] coins;
- coins = new int[heapc];
- for (int i = 0; i<h; i++)
- coins[i] = c[i];
- }
- ~State() {
- delete[] coins;
- }
- void next(const Move &move) throw(logic_error) {
- if ((move.getSourceCoins() > State.getCoins(move.getSource())) || (move.getTargetCoins() > move.getSourceCoins()))
- throw logic_error("insufficient coins");
- else if ((move.getSource()<0) || (move.getSource() >= heapc))
- throw logic_error("invalid heap");
- else {
- coins[move.getSource()] -= move.getSourceCoins();
- coins[move.getTarget()] += move.getTargetCoins();
- }
- }
- bool winning() const {
- bool flag = true;
- for (int i = 0; i<heapc; i++)
- if (coins[i] != 0)
- {
- flag = false;
- break;
- }
- return flag;
- }
- int getHeaps() const {
- return heapc;
- }
- int getCoins(int h) const throw(logic_error) {
- if (h > heapc)
- throw logic_error("This heap does not exist");
- return coins[h];
- }
- friend ostream & operator << (ostream &out, const State &state) {
- for (int i = 0; i<state.getHeaps() - 1; i++)
- out << state.getCoins(i) << ", ";
- out << state.getCoins(state.getHeaps() - 1);
- return out;
- }
- };
- class Move {
- private:
- int src, srccoin, trg, trgcoin;
- public:
- // Take sc coins from heap sh and put tc coins to heap th.
- Move(int sh, int sc, int th, int tc) {
- src = sh;
- srccoin = sc;
- trg = th;
- trgcoin = tc;
- }
- int getSource() const {
- return src;
- }
- int getSourceCoins() const {
- return srccoin;
- }
- int getTarget() const {
- return trg;
- }
- int getTargetCoins() const {
- return trgcoin;
- }
- friend ostream & operator << (ostream &out, const Move &move) {
- if (move.getTargetCoins() == 0)
- out << "takes " << move.getSourceCoins() << " coins from heap " << move.getSource() << " and puts nothing";
- else
- out << "takes " << move.getSourceCoins() << " coins from heap " << move.getSource() << " and puts " << move.getTargetCoins() << " to heap " << move.getTarget();
- return out;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement