Advertisement
Guest User

Balls Game

a guest
Dec 5th, 2011
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. /* игра с двойно свързана циклична структура - прочитане на числа (брой топчета), които държат в ръце хора подредени в кръг. първият дава по едно от своите топчета на всички по часовниковата стрелка. когато му свършат “предава щафетата” на следващия след него. играта свършва, когато се достигне до играч, който няма топчета. */
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. class DoubleLink
  10. {
  11.         DoubleLink* previous;
  12.         T payload;
  13.         DoubleLink* next;
  14.  
  15. public:
  16.         DoubleLink ()
  17.         {
  18.                 previous = this;
  19.                 next = this;
  20.         }
  21.  
  22.         DoubleLink (T init)
  23.         {
  24.                 previous = this;
  25.                 next = this;
  26.                 payload = init;
  27.         }
  28.  
  29.         void AddLink (DoubleLink* new_elem)
  30.         {
  31.                 DoubleLink* a = this;
  32.                 DoubleLink* b = this->next;
  33.                 a->next = new_elem;
  34.                 new_elem->previous = a;
  35.  
  36.                 b->previous = new_elem;
  37.                 new_elem->next = b;
  38.  
  39.         }
  40.  
  41.         inline DoubleLink* GetPrevious()
  42.         {
  43.                 return previous;
  44.         }
  45.  
  46.         inline DoubleLink* GetNext()
  47.         {
  48.                 return next;
  49.         }
  50.  
  51.         inline DoubleLink* SetNext(DoubleLink* new_next )
  52.         {
  53.                 return (next = new_next);
  54.         }
  55.  
  56.  
  57.         inline DoubleLink* SetPrev(DoubleLink* new_prev )
  58.         {
  59.                 return (previous = new_prev);
  60.         }
  61.  
  62.         inline T GetData ()
  63.         {
  64.                 return payload;
  65.         }
  66.  
  67.         inline T SetData (T new_data)
  68.         {
  69.                 return (payload = new_data);
  70.         }
  71.  
  72.         void Delete ()
  73.         {
  74.                 if ( this != this->GetNext() ) {
  75.                         this->GetPrevious()->SetNext( this->GetNext() );
  76.                         this->GetNext()->SetPrev( this->GetPrevious() );
  77.                 }
  78.  
  79.                 delete this;
  80.         }
  81. };
  82.  
  83. void PrintElements (DoubleLink<int> *dl)
  84. {
  85.         DoubleLink<int> *curr = dl->GetNext();
  86.  
  87.         do
  88.         {
  89.                 cout << curr->GetPrevious()->GetData() << " ";
  90.                 curr = curr->GetNext();
  91.         }
  92.         while (curr != dl);
  93.  
  94.         cout << curr->GetPrevious()->GetData() << endl;
  95. }
  96.  
  97. void PlayGame (DoubleLink<int>* dl)
  98. {
  99.         bool flag = true;
  100.         DoubleLink<int>* curr = dl;
  101.  
  102.         cout << "-- start game -- " << endl;
  103.  
  104.         while ( curr != curr->GetNext() ) {
  105.  
  106.                 while ( curr->GetData() ) {
  107.                         DoubleLink<int>* p = curr->GetNext();
  108.  
  109.                         cout << (flag ? '>' : '<');
  110.  
  111.                         while (curr->GetData() ) {
  112.                                 curr->SetData( curr->GetData() - 1);
  113.                                 p->SetData (p->GetData() + 1);
  114.                                 if (flag)
  115.                                         p = p->GetNext();
  116.                                 else
  117.                                         p = p->GetPrevious();
  118.                         }
  119.  
  120.                         curr = p;
  121.                         flag = !flag;
  122.  
  123.                         PrintElements(dl);
  124.                 }
  125.  
  126.                 DoubleLink<int>* newc = flag ? curr->GetNext() : curr->GetPrevious();
  127.                 if (curr == dl)
  128.                         dl = newc;
  129.                 curr->Delete();
  130.                 curr = newc;
  131.         }
  132. }
  133.  
  134.  
  135. int main ()
  136. {
  137.         ifstream file ("input.txt");
  138.         if (!file)
  139.         {
  140.                 cerr << "File could not be opened.\n";
  141.                 return -1;
  142.         }
  143.  
  144.         DoubleLink<int> *curr = NULL;
  145.  
  146.         while (!file.eof())
  147.         {
  148.                 int x;
  149.                 file >> x;
  150.                 if (curr)
  151.                         curr->AddLink(new DoubleLink<int> (x));
  152.                 else
  153.                         curr = new DoubleLink<int> (x);
  154.  
  155.                 curr = curr->GetNext();
  156.         }
  157.  
  158.         PrintElements(curr->GetNext());
  159.         PlayGame(curr->GetNext());
  160.  
  161. //      system ("PAUSE");
  162.         return 0;
  163. }
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement