title: Towers of hanoi, with every move shown
body: <p>I need to write a recursive towers of hanoi program, which I have done, but it needs to output the positions of the discs represented by stars and that's what I'm having trouble with. It needs to output a picture representation of the discs' location every time a move is made. I wrote code to do this for 3 discs, but I need it to be more flexible and work for 3, 4, or 5. Here is the code I have so far.</p>
  
  <pre><code>// This program displays a solution to the Towers of
  // Hanoi game.
  #include &lt;iostream&gt;
  using namespace std;
  
  // Function prototype
  void moveDiscs(int, int, int, int, int);
  
  int main()
  {
     int count = 0;
     int NUM_DISCS = 3;   // Number of discs to move
     const int FROM_PEG = 1;    // Initial "from" peg
     const int TO_PEG = 3;      // Initial "to" peg
     const int TEMP_PEG = 2;    // Initial "temp" peg
  
     // Play the game.
     moveDiscs(NUM_DISCS, FROM_PEG, TO_PEG, TEMP_PEG, count);
     cout &lt;&lt; "All the pegs are moved!\n";
  
     system("PAUSE");
     return 0;
  }
  
  //***************************************************
  // The moveDiscs function displays a disc move in   *
  // the Towers of Hanoi game.                        *
  // The parameters are:                              *
  //    num:     The number of discs to move.         *
  //    fromPeg: The peg to move from.                *
  //    toPeg:   The peg to move to.                  *
  //    tempPeg: The temporary peg.                   *
  //***************************************************
  
  void moveDiscs(int num, int fromPeg, int toPeg, int tempPeg, int count)
  {
     if (num &gt; 0)
     {  
        moveDiscs(num - 1, fromPeg, tempPeg, toPeg, count);
        cout &lt;&lt; "Move a disc from peg " &lt;&lt;fromPeg
             &lt;&lt; " to peg " &lt;&lt; toPeg &lt;&lt;endl;
             count++; 
             if (fromPeg == 1 &amp;&amp; toPeg == 3)
             {
                         if (num == 1)
                         {
                            if(count == 3)
                            cout&lt;&lt;"       *"&lt;&lt;endl&lt;&lt; "       *"&lt;&lt;endl&lt;&lt; "       *"&lt;&lt;endl;
                            else 
                            cout &lt;&lt; "*"&lt;&lt; endl &lt;&lt;"*      *"&lt;&lt;endl;
                         }
                         else
                         cout &lt;&lt; "   *"&lt;&lt;endl &lt;&lt;"   *   *"&lt;&lt;endl;
             }
             if (fromPeg==1 &amp;&amp; toPeg==2)
             cout &lt;&lt; "*  *  *"&lt;&lt;endl;
             if (fromPeg ==3 &amp;&amp; toPeg==2)
             cout &lt;&lt; "   *"&lt;&lt;endl &lt;&lt;"*  *"&lt;&lt;endl; 
             if (fromPeg==2 &amp;&amp; toPeg==1)
             cout &lt;&lt; "*  *  *"&lt;&lt;endl;
             if (fromPeg==2 &amp;&amp; toPeg==3)
             cout &lt;&lt; "       *"&lt;&lt; endl &lt;&lt;"*      *"&lt;&lt;endl;
        moveDiscs(num - 1, tempPeg, toPeg, fromPeg, count);
     }
  }
  </code></pre>