C++ Game – Towers of Hanoi

Configurare noua (How To)

Situatie

Game – Towers of Hanoi

Solutie

#include <iostream>
​​using namespace std;
​​
​​const int SIZE = 8;
​​class Tower
​​{
​​private:
​​  int _rings[SIZE]; // vector of rings
​​  int _height;      // number of rings on this tower
​​public:
​​  // creates a tower without rings (default c-tor)
​​  Tower() {
​​    _height = 0;
​​    for (int i = 0; i < 8; i++) {
​​      _rings[i] = 0;
​​    }
​​  }
​​  // creates a tower with height rings that has the values
​​  // height, height-1, ..., 3, 2, 1
​​  Tower(int height) {
​​    _height = height;
​​    for (int i = 0; i < _height; i++) {
​​      _rings[i] = _height - i;
​​    }
​​  }
​​  int GetHeight() {
​​    return _height;
​​  }
​​  // picks the ring on the top (lowers the height by 1)
​​  int Pick() {
​​    if (_height == 0) {
​​      return -1; // handle the case when there are no rings on the tower
​​    }
​​    int ring = _rings[_height - 1];
​​    _rings[_height - 1] = 0;
​​    _height--;
​​    return ring;
​​  }
​​  int Place(int ring) {
​​    if (_height == SIZE) {
​​      exit(-2); // tower is full (no more memory)
​​    }
​​    // ensure that the last ring has a higher value than the placed one
​​    // or that the tower is empty
​​    if ((_height > 0) && (_rings[_height - 1] < ring)) {
​​      return -1; // invalid move
​​    }
​​    // it is safe to place the ring
​​    _rings[_height] = ring;
​​    _height++;
​​    return 1;
​​  }
​​  void Show() {
​​    cout << "| ";
​​    for (int i = 0; i < _height; i++) {
​​      cout << _rings[i] << " ";
​​    }
​​    cout << endl;
​​  }
​​};
​​
​​class Game
​​{
​​  Tower _towers[3]; // each element from _towers is constructed
​​                    // using the default constructor Tower()
​​  int _moves;
​​  int _rings_number;
​​public:
​​  Game(int rings_number)
​​  {
​​    _towers[0] = Tower(rings_number);
​​    _moves = 0;
​​    _rings_number = rings_number;
​​  }
​​  void Show()
​​  {
​​    for (int i = 0; i < 3; i++)
​​      _towers[i].Show();
​​  }
​​
​​  // 1 means _tower[0]
​​  // 2 means _tower[1]
​​  // 3 means _tower[2]
​​  int Move(int towerA, int towerB)
​​  {
​​    // ensure values are between 1 and 3
​​    if ((towerA < 1 || 3 < towerA) ||
​​      (towerB < 1 || 3 < towerB))
​​      return -3;
​​
​​    // ring becomes the top ring of towerA
​​    int ring = _towers[towerA - 1].Pick();
​​    if (ring == -1) // tried to pick from an empty tower
​​      return -1;
​​    int placed = _towers[towerB - 1].Place(ring);
​​    if (placed == -1) // tried to place current ring on smaller ring
​​    {
​​      _towers[towerA - 1].Place(ring);
​​      return -2;
​​    }
​​    // from this point onwards we are sure that the move succeeded
​​    _moves++;
​​  }
​​  bool Over() {
​​    if (_towers[2].GetHeight() == _rings_number)
​​      return true;
​​    else
​​      return false;
​​  }
​​};
​​int main() {
​​  Game game(2);
​​  while (!game.Over()) {
​​    int ta, tb;
​​    game.Show();
​​    cin >> ta >> tb;
​​    game.Move(ta, tb);
​​  }
​​  game.Show();
​​
​​  return 0;
​​}

 

 

Tip solutie

Permanent

Voteaza

(0 din 1 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?