# Implementing a counter for recursive program

• 04-19-2013, 08:02 PM
wtbarr
Implementing a counter for recursive program
Hi all,

So I was given a homework assignment to write a code that completes the "Towers of Hanoi" as a program based on recursion. I have completed the code, so I am NOT asking anybody to write the code for me.

However, I would like to improve the code by adding a total moves counter at the very end, but I am not sure where to place my counter or how to implement it in my current code.

Here is my code:

Code:

```import java.util.Scanner; public class TowersofHanoi {     public static void main(String[] args) throws IllegalArgumentException {         System.out.println("How many disks?");                 Scanner input = new Scanner(System.in);                 int n = input.nextInt();         char a = 'A', b = 'B', t = 'T';                 moveDisks(n, a, b, t);     }         public static void moveDisks(int n,  char A, char B, char T){         //Pre-condition: n>0                 //Throws an error if number of disks equals 0         if(n==0){             throw new IllegalArgumentException("Number of disks must be an integer greater than 0");         } else if(n==1){             //base case, one disk             moveOneDisk(n, A, B);         } else {             //recursive case, n>1             moveDisks(n-1, A, T, B);             moveOneDisk(n, A, B);             moveDisks(n-1, T, B, A);         }     }         public static void moveOneDisk(int n, char A, char B){         System.out.println("Move disk " + n + " from " + A + " to " + B);     } }```

Currently the output would look like (this is for 3 disks):

Code:

```Move disk 1 from A to B Move disk 2 from A to T Move disk 1 from B to T Move disk 3 from A to B Move disk 1 from T to A Move disk 2 from T to B Move disk 1 from A to B```
But what I want to make happen is:

Code:

```Move disk 1 from A to B Move disk 2 from A to T Move disk 1 from B to T Move disk 3 from A to B Move disk 1 from T to A Move disk 2 from T to B Move disk 1 from A to B Total moves: 7```

Any help is greatly appreciated! :)
• 04-19-2013, 10:19 PM
monkeyjr97
Re: Implementing a counter for recursive program
every time a move happens just increment an integer:

define at the start of move disks

int moves = 0;

then every time you make a move increment the counter or call a method that does so:
Code:

```    public void increment() {     moves ++;     }```
then after you are finished printing all the moves just print out:

System.out.println("Total number of " + moves);

that's the info you need, but ill leave it to you, to work out where to put the print statement.

This is way id go about it anyways
• 04-20-2013, 12:26 AM
jim829
Re: Implementing a counter for recursive program
You could just use a static variable named moveCount and increment it in your moveOneDisk method. Also, why not do the check for n != 0 before your first call to moveDisks as opposed to each time you enter moveDisks? One design methodology is to have a single public method which checks the validity of the arguments and then have the actual processing performed in a private method which is called from the public method.

Regards,
Jim
• 04-20-2013, 12:41 PM
JosAH
Re: Implementing a counter for recursive program
If you relax your precondition a bit; i.e. if n <= 0 there is nothing to move, you can rewrite your recursive method to:

Code:

```    public static void moveDisks(int n,  char A, char B, char T){         //Pre-condition: n>0         if(n > 0){             moveDisks(n-1, A, T, B);             moveOneDisk(n, A, B);             moveDisks(n-1, T, B, A);         }     }```
kind regards,

Jos