7 Replies - 2436 Views - Last Post: 22 October 2012 - 09:00 AM Rate Topic: -----

#1 nasr.89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-June 12

help about towers of hanoi animated

Posted 21 October 2012 - 10:51 AM

hi guys, i need your help in coding a java program that solves towers of hanoi problem animated. i tried to use threads bu i couldn't assign the move process to the rectangle i which i want move..this is my code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
//Max Matthews
public class Hanoi extends JFrame
{
	int numOfDisks;
	int[][] game;
	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};
	
	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());
    	
    	Hanoi.MyDrawPanel drawPanel = new Hanoi.MyDrawPanel();//new DrawPanel
    	contentPane.add(drawPanel, BorderLayout.CENTER);
    	    	
        Container buttonPane = new Container();
    	buttonPane.setLayout(new FlowLayout());
    	contentPane.add(buttonPane, BorderLayout.NORTH);
    	
    	
    	
        Container buttonPaneTowers = new Container();
    	buttonPaneTowers.setLayout(new BorderLayout());
    	contentPane.add(buttonPaneTowers, BorderLayout.SOUTH);
    	
    	
    	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);
    	}
    	 	
    	    	
    	
    	
	}
	
	private class MyDrawPanel extends JPanel
    {
    	public void paintComponent (Graphics 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);}
                }
               
      		
      		System.out.println("Repainting");
    			
    	}
    }
		}


Is This A Good Question/Topic? 0
  • +

Replies To: help about towers of hanoi animated

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: help about towers of hanoi animated

Posted 21 October 2012 - 11:20 AM

for (int row=game.length-1;row>=0;row--)

you never initialized game[][]
Was This Post Helpful? 0
  • +
  • -

#3 nasr.89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-June 12

Re: help about towers of hanoi animated

Posted 21 October 2012 - 11:31 AM

View Postpbl, on 21 October 2012 - 11:20 AM, said:

for (int row=game.length-1;row>=0;row--)

you never initialized game[][]


this is the orginal code for a game and not working well

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
//Max Matthews
public class Hanoi extends JFrame
{
	JButton play = new JButton("Play");
	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 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};
	
	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());
    	
    	MyDrawPanel 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--;
    	}
    	    	
    	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)
    	{    	
    		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);}
        	}
      		
      		System.out.println("Repainting");
    			
    	}
    }
	
	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);
		repaint();
		//find the disk you are moving
		int topDisk=0;
		for (int i=0; i<game.length; i++)
		{
			if (game[i][startTower]==0)
			{
				repaint();
				topDisk=i;
			}
			else
			{
				topDisk=i;
				i=10; //makes the loop quit
				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;
			}
			repaint();
		}
		
		temp=game[topDisk][startTower];
		game[topDisk][startTower]=0;
		game[movingLocation][endTower]=temp;
		repaint();
	}
	
	public void moveTower(int numDisks, int start, int end, int temp)
	{
		repaint();
		if (numDisks==1)
		{
			moveDisk(start,end);
			repaint();
		}
		else
		{
			moveTower(numDisks-1, start, temp, end);
			moveDisk(start, end);
			moveTower(numDisks-1, temp, end, start);
			
			repaint();
		}
		repaint();
	}
	
	 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)
		{
			moveTower(8,0,2,1);
			repaint();
		}
	}
	
	private class Pause implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	private class Step implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	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
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: help about towers of hanoi animated

Posted 21 October 2012 - 11:44 AM

May be, but at least that version initialized game :)

game = new int[numOfDisks][3];
Was This Post Helpful? 0
  • +
  • -

#5 nasr.89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-June 12

Re: help about towers of hanoi animated

Posted 21 October 2012 - 11:46 AM

btw i had no help yet..please help me :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: help about towers of hanoi animated

Posted 21 October 2012 - 11:50 AM

What do you mean no help ???
I explain to you that your array wasn't initialized
You post other code
I show you where in that other code you posted the array was correctly initialized showing you what you should also do in the newer version

Beside rewriting the whole thing I don't see how I can be more useful
Was This Post Helpful? 0
  • +
  • -

#7 nasr.89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-June 12

Re: help about towers of hanoi animated

Posted 21 October 2012 - 01:58 PM

that i cant do it moves from tower to another
Was This Post Helpful? 0
  • +
  • -

#8 nasr.89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-June 12

Re: help about towers of hanoi animated

Posted 22 October 2012 - 09:00 AM

is there anyone can help?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1