1 Replies - 1252 Views - Last Post: 29 March 2011 - 11:14 AM Rate Topic: -----

#1 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Recursive Problem in Towers of Hanoi

Posted 29 March 2011 - 10:26 AM

I'm having trouble with my recursive tower's of hanoi (again). My recursive method is supposed to report the first move it's going to make, run through the a copied array, then move that first move. Instead it flips everything upside down on the second tower. Thanks for all your help in advance!

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class HanoiDoubArray extends JFrame
{
	int disks;
	int total;
	int [][] a;
	int [][] b;
	int movingDisk, movingLocation, startTower, endTower;
	int numRun = 1;
	boolean moving=false;
	int topDisk=0;
	JButton change = new JButton("             " +
			"                       1     " +
			"                          " +
			"       ");
	JButton change2 = new JButton("2");
	JButton change3 = new JButton("                       " +
			"             3          " +
			"                         ");
	JButton play=new JButton("Play");
	JButton pause=new JButton("Pause");
	JButton step=new JButton("Step");
	
	JSlider slider = new JSlider(1,10,2);
	
	public static void main(String[] args)
	{
		HanoiDoubArray window = new HanoiDoubArray();
		window.setVisible(true);
		window.setResizable(false);
	}
	
	public HanoiDoubArray()
	{
		setSize(800,600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(0,0);
		Container contentPane = getContentPane();
		Container button = new Container();
		button.setLayout(new BorderLayout());
		MyDrawPanel drawPanel = new MyDrawPanel();
		contentPane.add(drawPanel);
		BorderLayout bd = new BorderLayout();
		contentPane.setLayout(bd);
		contentPane.add(button,BorderLayout.SOUTH);
		contentPane.add(drawPanel, BorderLayout.CENTER);
		
		Container containerNorth = new Container();
		containerNorth.setLayout(new FlowLayout());
		containerNorth.add(play);
		containerNorth.add(pause);
		containerNorth.add(step);
		containerNorth.add(slider);
		contentPane.add(containerNorth, BorderLayout.NORTH);
		button.add(change, BorderLayout.WEST);
		button.add(change2, BorderLayout.CENTER);
		button.add(change3, BorderLayout.EAST);
		ChangeListener1 inputListener1 = new ChangeListener1();
		ChangeListener2 inputListener2 = new ChangeListener2();
		ChangeListener3 inputListener3 = new ChangeListener3();
		PlayListener playListen = new PlayListener();
		change.addActionListener(inputListener1);
		change2.addActionListener(inputListener2);
		change3.addActionListener(inputListener3);
		play.addActionListener(playListen);
		
		String anaString = JOptionPane.showInputDialog(null, "Number of disks? (1-8)");
		
		try
		{
			disks = Integer.parseInt(anaString);
				
		}
		catch (NumberFormatException e)
		{
			JOptionPane.showMessageDialog(new JFrame(), 
					"Invalid Number. Exiting.");
			System.exit(5);
		}
		
		if(disks<1 || disks>8)
		{
			JOptionPane.showMessageDialog(new JFrame(), "Invalid Number. Exiting.");
			System.exit(5);
		}
		
		a=new int[disks][3];
		for (int row=0; row < a.length; row++)
		{
			for(int col=0; col < a[0].length; col++)
			{
				a[row][col]=0;
			}
		}
		int numToInsert = 8;
		for (int i=disks; i > 0; i--)
		{
			a[i-1][0] = numToInsert;
			numToInsert--;
		}
		printArray();
		
		for (int i=0; i<a.length;i++)
		{
			total+=i;
		}
	}
	
	public void printArray()
	{
	//	System.out.println("\n\n");
		for (int row=0; row < a.length; row++)
		{
			for(int col=0; col < a[0].length; col++)
			{
		//		System.out.print(a[row][col]);
			}
		//	System.out.println();
		}
	}
	
	public void printArrayB()
	{
		System.out.println("\n\n");
		for (int row=0; row < b.length; row++)
		{
			for(int col=0; col < b[0].length; col++)
			{
				System.out.print(b[row][col]);
			}
			System.out.println();
		}
	}
	
	private class MyDrawPanel extends JPanel
	{
		public void paintComponent (Graphics g)
		{
			g.setColor(Color.BLACK);
			g.fillRect(20, 500, 750, 25);
			g.setColor(Color.BLACK); //3rd tower
			g.fillRect(650, 20, 25, 500);
			g.setColor(Color.BLACK); //2nd tower
			g.fillRect(375, 20, 25, 500);
			g.setColor(Color.BLACK); //1st tower
			g.fillRect(100, 20, 25, 500);
			
			for(int row=0; row<=a.length-1;row++)
			{
				for (int col=0; col<3; col++)
				{ 
					if(a[row][col]==8)
					{
						g.setColor(Color.cyan);
						g.fillRoundRect(15+(275*col), 260+(30*(row+(8-disks))), 200, 30, 20, 20);
					}
					
					if(a[row][col]==7)
					{
						g.setColor(Color.red);
						g.fillRoundRect(24+(275*col), 260+(30*(row+(8-disks))), 180, 30, 20, 20);
					}
					
					if(a[row][col]==6)
					{
						g.setColor(Color.magenta);
						g.fillRoundRect(34+(275*col), 260+(30*(row+(8-disks))), 160, 30, 20, 20);
					}
					
					if(a[row][col]==5)
					{
						g.setColor(Color.green);
						g.fillRoundRect(44+(275*col), 260+(30*(row+(8-disks))), 140, 30, 20, 20);
					}
					
					if(a[row][col]==4)
					{
						g.setColor(Color.gray);
						g.fillRoundRect(54+(275*col), 260+(30*(row+(8-disks))), 120, 30, 20, 20);
					}
					
					if(a[row][col]==3)
					{
						g.setColor(Color.yellow);
						g.fillRoundRect(64+(275*col), 260+(30*(row+(8-disks))), 100, 30, 20, 20);
					}
					
					if(a[row][col]==2)
					{
						g.setColor(Color.pink);
						g.fillRoundRect(74+(275*col), 260+(30*(row+(8-disks))), 80, 30, 20, 20);
					}
					
					if(a[row][col]==1)
					{
						g.setColor(Color.blue);
						g.fillRoundRect(84+(275*col), 260+(30*(row+(8-disks))), 60, 30, 20, 20);
					}
				}
			}
		}
	}
	

	
	private class ChangeListener1 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();
			
			if (moving)
			{
				int lowestLocation=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][0]==0)
					{
						lowestLocation=i;
					}
				}
				
				a[lowestLocation][0]=topDisk;
				topDisk=0;
				lowestLocation=0;
				
				moving=false;
				change.setEnabled(true);
				change2.setEnabled(true);
				change3.setEnabled(true);
				checkWin();
			}
			else
			{
				for (int i=0; i<a.length; i++)
				{
					if (a[i][0] != 0)
					{
						topDisk=a[i][0];
						a[i][0]=0;
						moving=true;
						i=19349;
					}
					
				}
				int locationTopDisk2=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][1]!=0)
					{
						locationTopDisk2=i;
						i=a.length+1;
					}
				}
				if (locationTopDisk2==0)
				{
					
				}
				else if (a[locationTopDisk2][1] < topDisk)
				{
					change2.setEnabled(false);
				}
				
				int locationTopDisk3=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][2]!=0)
					{
						locationTopDisk3=i;
						i=a.length+1;
					}
				}
				if (locationTopDisk3==0)
				{
					
				}
				else if (a[locationTopDisk3][2] < topDisk)
				{
					change3.setEnabled(false);
				}
			}
			printArray();
		}
	}
	
	private class ChangeListener2 implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			repaint();
			
			if (moving)
			{
				int lowestLocation=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][1]==0)
					{
						lowestLocation=i;
					}
				}
				
				a[lowestLocation][1]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				change.setEnabled(true);
				change2.setEnabled(true);
				change3.setEnabled(true);
				checkWin();
			}
			else
			{
				for (int i=0; i<a.length; i++)
				{
					if (a[i][1] != 0)
					{
						topDisk=a[i][1];
						a[i][1]=0;
						moving=true;
						i=19349;
					}
					
				}
				int locationTopDisk2=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][0]!=0)
					{
						locationTopDisk2=i;
						i=a.length+1;
					}
				}
				if (locationTopDisk2==0)
				{
					
				}
				else if (a[locationTopDisk2][0] < topDisk)
				{
					change.setEnabled(false);
				}
				
				int locationTopDisk3=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][2]!=0)
					{
						locationTopDisk3=i;
						i=a.length+1;
					}
				}
				if (locationTopDisk3==0)
				{
					
				}
				else if (a[locationTopDisk3][2] < topDisk)
				{
					change3.setEnabled(false);
				}
			}
			printArray();
		}
	}
	
	private class ChangeListener3 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();
			
			if (moving)
			{
				int lowestLocation=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][2]==0)
					{
						lowestLocation=i;
					}
				}
				
				a[lowestLocation][2]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				change.setEnabled(true);
				change2.setEnabled(true);
				change3.setEnabled(true);
				checkWin();
			}
			else
			{
				for (int i=0; i<a.length; i++)
				{
					if (a[i][2] != 0)
					{
						topDisk=a[i][2];
						a[i][2]=0;
						moving=true;
						i=19349;
					}
					
				}
				int locationTopDisk2=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][1]!=0)
					{
						locationTopDisk2=i;
						i=a.length+1;
					}
				}
				if (locationTopDisk2==0)
				{
					
				}
				else if (a[locationTopDisk2][1] < topDisk)
				{
					change2.setEnabled(false);
				}
				
				int locationTopDisk3=0;
				for(int i=0; i<a.length; i++)
				{
					if (a[i][1]!=0)
					{
						locationTopDisk3=i;
						i=a.length+1;
					}
				}
				if (locationTopDisk3==0)
				{
					
				}
				else if (a[locationTopDisk3][1] < topDisk)
				{
					change2.setEnabled(false);
				}
			}
			printArray();
		}
	}
	
	public void checkWin()
	{
		if(a[disks-1][0] == 0 && a[disks-1][1] == 0)
		{
			JOptionPane.showMessageDialog(new JFrame(), "You win!!!");
			System.exit(5);
		}
	}
	
	 public static void manySec(long s) {
	     try {
	       Thread.currentThread().sleep(s * 1000);
	       }
	     catch (InterruptedException e) {
	       e.printStackTrace();
	       }
	     }

	
	int temp;
	public void moveDisk(int startTower2, int endTower2)
	{
		//printArray();
		repaint();
		int topDisk=0;
		for (int i=0; i<b.length; i++)
		{
			if (b[i][startTower2]==0)
			{
				topDisk=i;
			}
			else
			{
				topDisk=i;
				i=10;
			}
		}
		
		int movingLocation2=disks-1;
		for(int i=0; i<b.length; i++)
		{
			if (b[i][endTower2]!=0)
			{
				movingLocation2=i-1;
				i=b.length+1;
			}
		}
		
		temp=b[topDisk][startTower2];
		b[topDisk][startTower2]=0;
		b[movingLocation2][endTower2]=temp;
		repaint();
		printArrayB();
		if (numRun==1)
		{
			movingDisk=topDisk;
			movingLocation=movingLocation2;
			startTower=startTower2;
			endTower=endTower2;
			
			numRun++;
		}
	}
	
	public void moveTower(int disks, int start, int end, int temp)
	{
		if (disks ==1)
		{
			moveDisk(start,end);
		}
		
		else
		{
			moveTower(disks-1, start, temp, end);
			moveDisk(start, end);
			moveTower(disks-1, temp, end, start);
			
			repaint();
		}
	}
	

	private class PlayListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			b=new int[a.length][a[0].length];
			for(int row=0; row<a.length; row++)
				for (int col=0; col<a[0].length; col++)
					b[row][col]=a[row][col];
			
			//printArrayB();
			
			moveTower(disks,0,2,1);
			System.out.println("From Tower: " + startTower+
					"\nTo Tower: " +endTower+
					"\nMoving Disk: " + movingDisk+
					"\nMoving Location: " + movingLocation);
			
			temp=a[movingDisk][startTower];
			a[movingDisk][startTower]=0;
			a[movingLocation][endTower]=temp;
			numRun=1;
			checkWin();
			repaint();
			printArrayB();
		}
	}
}
	



Is This A Good Question/Topic? 0
  • +

Replies To: Recursive Problem in Towers of Hanoi

#2 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Re: Recursive Problem in Towers of Hanoi

Posted 29 March 2011 - 11:14 AM

Eureka, I had a break through. In case anyone needs it, this is my towers of hanoi with a step button. Any suggestions on how to get the play button to work? Is it possible to click on a button (or have it perform an action performed on that button) within a never button?

Thanks!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
//Max Matthews
public class Hanoi extends JFrame
{
	int fromTower, toTower, fromLocation, toLocation;
	int runLoop=1;
	JButton play = new JButton("Play");
	MyDrawPanel drawPanel;
	JButton pause = new JButton("Pause");
	JButton step = new JButton("Step");
	JButton pole1 = new JButton("         " +
			"                          1                " +
			"                ");
	JButton pole2 = new JButton("2");
	JButton pole3 = new JButton("           " +
			"                        3   " +
			"                                ");
	JSlider slider = new JSlider(1,10,2);
	int numOfDisks;
	int[][] game;
	int[][] game2;
	int totalGoal =0;
	int topDisk=0;
	boolean moving=false;
	static Color[] colors = {Color.black, Color.blue,
		Color.PINK, Color.green, Color.magenta, Color.YELLOW, Color.RED, Color.cyan};
	ArrayList<Integer> moves = new ArrayList<Integer>();
	
	public static void main(String args[])
	{
		Hanoi window = new Hanoi();
		window.setVisible(true); //makes the window visible
		window.setResizable(false);
		
		Random rgen = new Random();
		for (int i=0; i<colors.length; i++) {
		    int randomPosition = rgen.nextInt(colors.length);
		    Color temp = colors[i];
		    colors[i] = colors[randomPosition];
		    colors[randomPosition] = temp;
		}
		
		
	}
	
	public Hanoi()
	{
		setTitle("Towers of Hanoi"); //sets the title of window to Bank Teller
    	setSize(800,500); //Sets the size of the window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Makes it so the "X" button works
    	setLocation(0,0); //makes it appear at a certain place on the screen
    	
    	Container contentPane = getContentPane(); //new ContentPane
    	contentPane.setLayout(new BorderLayout());
    	
    	drawPanel = new MyDrawPanel();//new DrawPanel
    	contentPane.add(drawPanel, BorderLayout.CENTER);
    	    	
        Container buttonPane = new Container();
    	buttonPane.setLayout(new FlowLayout());
    	contentPane.add(buttonPane, BorderLayout.NORTH);
    	buttonPane.add(play);
    	buttonPane.add(pause);
    	buttonPane.add(step);
    	buttonPane.add(slider);
    	
    	
        Container buttonPaneTowers = new Container();
    	buttonPaneTowers.setLayout(new BorderLayout());
    	contentPane.add(buttonPaneTowers, BorderLayout.SOUTH);
    	buttonPaneTowers.add(pole1, BorderLayout.WEST);
    	buttonPaneTowers.add(pole2, BorderLayout.CENTER);
    	buttonPaneTowers.add(pole3, BorderLayout.EAST);
    	
    	pause.setEnabled(false);
    	
    	String ansString = JOptionPane.showInputDialog
    		(null, "Number of Disks (1-8)");
    	
    	try
    	{
        	numOfDisks = Integer.parseInt(ansString);
    	}
    	catch (NumberFormatException E)
    	{
    		JOptionPane.showMessageDialog
    			(new JFrame(), "Invalid Number. Now exiting.");
    		System.exit(5);
    	}
    	
    	if (numOfDisks<1 || numOfDisks>8)
    	{
    		JOptionPane.showMessageDialog
				(new JFrame(), "Invalid Number. Now exiting.");
    		System.exit(5);
    	}
    	
    	game = new int[numOfDisks][3];
    	for (int row=0;row<game.length;row++)
    		for (int col=0;col<game[0].length;col++)
    			game[row][col]=0;
    	
    	int numToInsert=8;
    	for (int i=numOfDisks; i>0; i--)
    	{
    		game[i-1][0]=numToInsert;
    		numToInsert--;
    	}
    	
		game2 = new int[numOfDisks][3];
    	for (int row=0;row<game.length;row++)
    		for (int col=0;col<game[0].length;col++)
    			game2[row][col]=game[row][col];
		moveTower(numOfDisks,0,2,1);
    	    	
    	Button1 b1Listener = new Button1();
    	Button2 b2Listener = new Button2();
    	Button3 b3Listener = new Button3();
		pole1.addActionListener(b1Listener);
		pole2.addActionListener(b2Listener);
		pole3.addActionListener(b3Listener);
    	Play playListener = new Play();
    	Pause pauseListener = new Pause();
    	Step stepListener = new Step();
		play.addActionListener(playListener);
		pause.addActionListener(pauseListener);
		step.addActionListener(stepListener);
		
    	for (int row=0;row<game.length;row++)
    	{
    		totalGoal+=game[row][0];
    	}
    	
	}
	
	private class MyDrawPanel extends JPanel
    {
    	public void paintComponent (Graphics g)
    	{    	
    		super.paintComponent(g);
    		g.drawString("Disk Moving: " + topDisk, 330,25);
    		g.setColor(Color.GRAY);
    		g.fillRect(50,335,700,50);
    		
    		g.setColor(Color.GRAY);
    		g.fillRect(150, 67, 17, 270);
    		g.fillRect(375, 67, 17, 270);
    		g.fillRect(620, 67, 17, 270);
    		    		
    		g.setColor(Color.BLUE);
    		for (int row=game.length-1;row>=0;row--)
        	{
        		g.setColor(Color.blue);
        		if (game[row][0]==8){g.setColor(colors[7]);
        			g.fillRoundRect(67, 100+(29*(row+(8-numOfDisks))), 180, 25, 10, 10);}
        		if (game[row][0]==7){g.setColor(colors[6]);
        			g.fillRoundRect(77,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][0]==6){g.setColor(colors[5]);
        			g.fillRoundRect(87,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][0]==5){g.setColor(colors[4]);
        			g.fillRoundRect(97,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][0]==4){g.setColor(colors[3]);
        			g.fillRoundRect(107,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][0]==3){g.setColor(colors[2]);
        			g.fillRoundRect(117,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][0]==2){g.setColor(colors[1]);
        			g.fillRoundRect(127,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][0]==1){g.setColor(colors[0]);
        			g.fillRoundRect(137,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}
    		for (int row=0;row<game.length;row++)
        	{
        		g.setColor(Color.blue);
        		if (game[row][1]==8){g.setColor(colors[7]);
        			g.fillRoundRect(293,100+(29*(row+(8-numOfDisks))),180,25, 10, 10);}
        		if (game[row][1]==7){g.setColor(colors[6]);
        			g.fillRoundRect(303,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][1]==6){g.setColor(colors[5]);
        			g.fillRoundRect(313,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][1]==5){g.setColor(colors[4]);
        			g.fillRoundRect(323,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][1]==4){g.setColor(colors[3]);
        			g.fillRoundRect(333,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][1]==3){g.setColor(colors[2]);
        			g.fillRoundRect(343,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][1]==2){g.setColor(colors[1]);
        			g.fillRoundRect(353,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][1]==1){g.setColor(colors[0]);
        			g.fillRoundRect(363,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}   
      		for (int row=0;row<game.length;row++)
        	{
        		g.setColor(Color.blue);
        		if (game[row][2]==8){g.setColor(colors[7]);
        			g.fillRoundRect(67+470,100+(29*(row+(8-numOfDisks))),180,25, 10, 10);}
        		if (game[row][2]==7){g.setColor(colors[6]);
        			g.fillRoundRect(77+471,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][2]==6){g.setColor(colors[5]);
        			g.fillRoundRect(87+471,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][2]==5){g.setColor(colors[4]);
        			g.fillRoundRect(97+471,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][2]==4){g.setColor(colors[3]);
        			g.fillRoundRect(107+471,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][2]==3){g.setColor(colors[2]);
        			g.fillRoundRect(117+471,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][2]==2){g.setColor(colors[1]);
        			g.fillRoundRect(127+471,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][2]==1){g.setColor(colors[0]);
        			g.fillRoundRect(137+471,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}
      		
      		
    			
    	}
    }
	
	private class Button1 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();

			if (moving)
			{
				int lowestLocation=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]==0)
					{
						lowestLocation=i;
					}
				}
				
				game[lowestLocation][0]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				
				pole1.setEnabled(true);
				pole2.setEnabled(true);
				pole3.setEnabled(true);
				checkIfWon();
			}
			else
			{
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]!=0)
					{
						topDisk=game[i][0];
						game[i][0]=0;
						
						moving=true;
												     
						i=game.length+1;
					}
				}
				
				//find tower two top disk
				int locationTopDisk2=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]!=0)
					{
						locationTopDisk2=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk2==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk2][1])
					pole2.setEnabled(false);
				
				//find tower three top disk
				int locationTopDisk3=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][2]!=0)
					{
						locationTopDisk3=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk3==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk3][2])
					pole3.setEnabled(false);
			}
			printArray();
		}
	}
	
	private class Button2 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();
			
			if (moving)
			{
				int lowestLocation=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]==0)
					{
						lowestLocation=i;
					}
				}
				
				game[lowestLocation][1]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				
				pole1.setEnabled(true);
				pole2.setEnabled(true);
				pole3.setEnabled(true);
				checkIfWon();
			}
			else
			{
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]!=0)
					{
						topDisk=game[i][1];
						game[i][1]=0;
						
						moving=true;
						     
						i=game.length+1; //makes the loop exit
					}
				}
				
				//find tower one top disk
				int locationTopDisk2=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]!=0)
					{
						locationTopDisk2=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk2==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk2][0])
					pole1.setEnabled(false);
				
				//find tower three top disk
				int locationTopDisk3=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][2]!=0)
					{
						locationTopDisk3=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk3==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk3][2])
					pole3.setEnabled(false);
			}
			printArray();
		}
	}
	
	private class Button3 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
				repaint();
				
				if (moving)
				{
					int lowestLocation=0;
					
					for (int i=0; i<game.length; i++)
					{
						if (game[i][2]==0)
						{
							lowestLocation=i;
						}
					}
					
					game[lowestLocation][2]=topDisk;
					topDisk=0;
					lowestLocation=0;
					moving=false;
					
					pole1.setEnabled(true);
					pole2.setEnabled(true);
					pole3.setEnabled(true);
					
					checkIfWon();
				}
				else
				{
					for (int i=0; i<game.length; i++)
					{
						if (game[i][2]!=0)
						{
							topDisk=game[i][2];
							game[i][2]=0;
							
							moving=true;
							     
							i=game.length+1;
						}
					}	
					
					//find tower two top disk
					int locationTopDisk2=0;
					for (int i=0; i<game.length; i++)
					{
						if (game[i][1]!=0)
						{
							locationTopDisk2=i;
							i=game.length+1;
						}
					}
					
					if (locationTopDisk2==0)
					{
						
					}
					else if(topDisk>game[locationTopDisk2][1])
						pole2.setEnabled(false);
					
					//find tower three top disk
					int locationTopDisk3=0;
					for (int i=0; i<game.length; i++)
					{
						if (game[i][0]!=0)
						{
							locationTopDisk3=i;
							i=game.length+1;
						}
					}
					
					if (locationTopDisk3==0)
					{
						
					}
					else if(topDisk>game[locationTopDisk3][0])
						pole1.setEnabled(false);
				}
				printArray();
				
		}
	}
	
	int temp;
	public void moveDisk(int startTower, int endTower)
	{	
		//System.out.println(startTower+"\t"+endTower);
		//printArray();
		//manySec(.5);
		drawPanel.repaint();
		//find the disk you are moving
		int topDisk=0;
		for (int i=0; i<game.length; i++)
		{
			if (game2[i][startTower]==0)
			{
				drawPanel.repaint();
				topDisk=i;
			}
			else
			{
				topDisk=i;
				i=10; //makes the loop quit
				drawPanel.repaint();
			}	
		}
		
		//find where you are moving to on the tower
		int movingLocation=numOfDisks-1;
		for (int i=0; i<game.length; i++)
		{
			if (game2[i][endTower]!=0)
			{
				movingLocation=i-1;
				i=game2.length+1;
			}
			drawPanel.repaint();
		}
		
		temp=game2[topDisk][startTower];
		game2[topDisk][startTower]=0;
		game2[movingLocation][endTower]=temp;
		drawPanel.repaint();
		
		
	
			fromTower=startTower;
			toTower=endTower;
			fromLocation=topDisk;
			toLocation=movingLocation;
			
			moves.add(fromTower);
			moves.add(toTower);

			
			System.out.println("Disk: " + fromLocation + "\nfrom tower: " 
					+ fromTower + "\nto tower: " + toTower + "\nat location: " + 
					toLocation+"\n\n\n");
		

		
	}
	
	
	public void moveTower(int numDisks, int start, int end, int temp)
	{
		repaint();
		if (numDisks==1)
		{
			moveDisk(start,end);
			drawPanel.repaint();
		}
		else
		{
			moveTower(numDisks-1, start, temp, end);
			moveDisk(start, end);
			moveTower(numDisks-1, temp, end, start);
			
			drawPanel.repaint();
			
		}
		drawPanel.repaint();
		printArray();
	}
	
	 public static void manySec(double a)
	 {
		 int s = (int) Math.round(a*1000);
	     try
	     {
	       Thread.currentThread().sleep(s);
	     }
	     catch (InterruptedException e)
	     {
	       e.printStackTrace();
	     }
	 }

	private class Play implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{

		}
	}
	
	private class Pause implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	private class Step implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			moveDiskForReal(moves.get(0), moves.get(1));
			System.out.println(moves.get(0)+" "+ moves.get(1));
			moves.remove(0);
			moves.remove(0);
			repaint();
			checkIfWon();
		}
	}
	
	int temp2;
	public void moveDiskForReal(int startTower, int endTower)
	{	
		drawPanel.repaint();
		int topDisk=0;
		for (int i=0; i<game.length; i++)
		{
			if (game[i][startTower]==0)
			{
				drawPanel.repaint();
				topDisk=i;
			}
			else
			{
				topDisk=i;
				i=10; //makes the loop quit
				drawPanel.repaint();
			}	
		}
		
		//find where you are moving to on the tower
		int movingLocation=numOfDisks-1;
		for (int i=0; i<game.length; i++)
		{
			if (game[i][endTower]!=0)
			{
				movingLocation=i-1;
				i=game.length+1;
			}
			drawPanel.repaint();
		}
		
		temp=game[topDisk][startTower];
		game[topDisk][startTower]=0;
		game[movingLocation][endTower]=temp;
		drawPanel.repaint();
		
		
		runLoop++;
		
	}
	
	private void printArray()
	{
    	for (int row=0;row<game.length;row++)
    	{
    		for (int col=0;col<game[0].length;col++)
    		{
    		//	System.out.print(game[row][col]);
    		}
    	//	System.out.println();
    	}
    //	System.out.println("\n");
	}
	
	private void checkIfWon()
	{
		int totalOfColumn=0;
    	for (int row=0;row<game.length;row++)
    	{
    		totalOfColumn+=game[row][2];
    	}
    	
    	if (totalGoal==totalOfColumn)
    	{
    		JOptionPane.showMessageDialog
			(new JFrame(), "You won! Now exiting.");
    		System.exit(0);
    	}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1