Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. class State {
  7. private:
  8. int heapc;
  9. int *coins;
  10.  
  11. public:
  12. // State with h heaps, where the i-th heap starts with c[i] coins.
  13. State(int h, const int c[]) {
  14. heapc = h;
  15. delete[] coins;
  16. coins = new int[heapc];
  17. for (int i = 0; i<h; i++)
  18. coins[i] = c[i];
  19. }
  20. ~State() {
  21. delete[] coins;
  22. }
  23. void next(const Move &move) throw(logic_error) {
  24. if ((move.getSourceCoins() > State.getCoins(move.getSource())) || (move.getTargetCoins() > move.getSourceCoins()))
  25. throw logic_error("insufficient coins");
  26. else if ((move.getSource()<0) || (move.getSource() >= heapc))
  27. throw logic_error("invalid heap");
  28. else {
  29. coins[move.getSource()] -= move.getSourceCoins();
  30. coins[move.getTarget()] += move.getTargetCoins();
  31. }
  32. }
  33. bool winning() const {
  34. bool flag = true;
  35. for (int i = 0; i<heapc; i++)
  36. if (coins[i] != 0)
  37. {
  38. flag = false;
  39. break;
  40. }
  41. return flag;
  42. }
  43.  
  44. int getHeaps() const {
  45. return heapc;
  46. }
  47. int getCoins(int h) const throw(logic_error) {
  48. if (h > heapc)
  49. throw logic_error("This heap does not exist");
  50. return coins[h];
  51. }
  52. friend ostream & operator << (ostream &out, const State &state) {
  53. for (int i = 0; i<state.getHeaps() - 1; i++)
  54. out << state.getCoins(i) << ", ";
  55. out << state.getCoins(state.getHeaps() - 1);
  56. return out;
  57. }
  58. };
  59.  
  60. class Move {
  61. private:
  62. int src, srccoin, trg, trgcoin;
  63. public:
  64. // Take sc coins from heap sh and put tc coins to heap th.
  65. Move(int sh, int sc, int th, int tc) {
  66. src = sh;
  67. srccoin = sc;
  68. trg = th;
  69. trgcoin = tc;
  70.  
  71. }
  72. int getSource() const {
  73. return src;
  74. }
  75. int getSourceCoins() const {
  76. return srccoin;
  77. }
  78. int getTarget() const {
  79. return trg;
  80. }
  81. int getTargetCoins() const {
  82. return trgcoin;
  83. }
  84. friend ostream & operator << (ostream &out, const Move &move) {
  85. if (move.getTargetCoins() == 0)
  86. out << "takes " << move.getSourceCoins() << " coins from heap " << move.getSource() << " and puts nothing";
  87. else
  88. out << "takes " << move.getSourceCoins() << " coins from heap " << move.getSource() << " and puts " << move.getTargetCoins() << " to heap " << move.getTarget();
  89. return out;
  90. }
  91.  
  92.  
  93. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement