Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Tower of Hanoi Implementation

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tower of Hanoi Implementation

    I have to implement a program that creates the Tower of Hanoi game where there is a human play and computer plays. The human plays allows the user to solve the problems by moving the disks on pegs. The computer plays allows the computer to solve the game and prints out each move.
    package prog05;
     
    import java.util.Stack;
    import prog02.UserInterface;
    import prog02.GUI;
     
    public class Tower {
      static UserInterface ui = new GUI();
     
      static public void main (String[] args) {
        int n = getInt("How many disks?");
        if (n <= 0)
          return;
        Tower tower = new Tower(n);
     
        String[] commands = { "Human plays.", "Computer plays." };
        int c = ui.getCommand(commands);
        if (c == 0)
          tower.play();
        else
          tower.solve();
      }
     
      /** Get an integer from the user using prompt as the request.
       *  Return 0 if user cancels or enters an empty string.  */
      static int getInt (String prompt) {
        while (true) {
          String number = ui.getInfo(prompt);
          if (number == null || number.length() == 0)
            return 0;
          try {
            return Integer.parseInt(number);
          } catch (Exception e) {
            ui.sendMessage(number + " is not a number.  Try again.");
          }
        }
      }
     
      int nDisks;
      StackInt<Integer>[] pegs = (StackInt<Integer>[]) new ArrayStack[3];
     
      Tower (int nDisks) {
        this.nDisks = nDisks;
        for (int i = 0; i < pegs.length; i++)
          pegs[i] = new ArrayStack<Integer>();
     
        // EXERCISE: Initialize game with pile of nDisks disks on peg 'a'
        // (pegs[0]).
      }
     
      void play () {
        while (true /* EXERCISE:  player has not won yet. */) {
          displayPegs();
          String move = getMove();
          int from = move.charAt(0) - 'a';
          int to = move.charAt(1) - 'a';
          move(from, to);
        }
     
        ui.sendMessage("You win!");
      }
     
      String stackToString (StackInt peg) {
        StackInt helper = new ArrayStack();
     
        // String to append items to.
        String s = "";
     
        // EXERCISE:  append the items in peg to s from bottom to top.
     
        return s;
      }
     
      void displayPegs () {
        String s = "";
        for (int i = 0; i < pegs.length; i++) {
          char abc = (char) ('a' + i);
          s = s + abc + stackToString(pegs[i]);
          if (i < pegs.length-1)
            s = s + "\n";
        }
        ui.sendMessage(s);
      }
     
      String getMove () {
        String[] moves = { "ab", "ac", "ba", "bc", "ca", "cb" };
        return moves[ui.getCommand(moves)];
      }
     
      void move (int from, int to) {
        // EXERCISE:  move one disk form pegs[from] to pegs[to].
        // Don't do illegal moves.  Send a warning message instead, like:
        // Cannot place 2 on top of 1.
      }
     
      // EXERCISE:  create Goal class.
     
      void solve () {
        // EXERCISE
      }        
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower of Hanoi Implementation

    A nice old game.
    What questions or problems do you have?

    One problem you'll have getting people to test your code is that you are using a third party package:
    import prog02.UserInterface;

Similar Threads

  1. Tower Defense on Java
    By snowguy13 in forum Java Theory & Questions
    Replies: 60
    Last Post: December 20th, 2011, 08:15 PM
  2. [SOLVED] Towers of Hanoi Algorithm Error once i get to 3 disks??
    By courtenay in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2011, 12:06 AM
  3. Extended Hanooi Tower
    By mahsa in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 31st, 2010, 05:55 PM
  4. Help wih implementation...
    By Frank_nor in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 24th, 2009, 05:43 PM
  5. B+ Tree implementation
    By programmer in forum Java Theory & Questions
    Replies: 2
    Last Post: November 15th, 2009, 05:47 PM