Welcome to Dream.In.Code
Become a Java Expert!

Join 149,523 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,461 people online right now. Registration is fast and FREE... Join Now!




3 x 3 grid puzzle

 
Reply to this topicStart new topic

3 x 3 grid puzzle, game to sort num,bers 1 - 8 in order

philipd
25 May, 2007 - 02:59 AM
Post #1

New D.I.C Head
*

Joined: 25 May, 2007
Posts: 1


My Contributions

Hi I am trying to create a slider puzzle where i have 9 buttons numbered 1 - 8 and then a blank one. I am able to create all my buttons but I am unsure as how to make the numbers move from one button to another.. any hints or help would be greatly appreciated. Im very new to all this. thanks

CODE

//i want this project to display 9 tiles that can be moveble. it is a puzzle game.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*; // needed for containers
import java.awt.event.*;
import java.util.Vector;
import java.io.*;
  
  
   class PhilsPuzzle extends JFrame implements ActionListener, ItemListener {
  
   JButton one = new JButton("1");
   JButton two = new JButton("2");
   JButton three = new JButton("3");
   JButton four = new JButton("4");
   JButton five = new JButton("5");
   JButton six = new JButton("6");
   JButton seven = new JButton("7");
   JButton eight = new JButton("8");
   JButton nine = new JButton("");
  
   public PhilsPuzzle() {
  
   // graphics
       super("Phillys Puzzling Puzzler MaBobby");
       setSize(750,500);
       setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
      
       JPanel myScreen = new JPanel();
       BorderLayout layout = new BorderLayout();// main window
       myScreen.setLayout(layout);
      
       JPanel northscreen = new JPanel();
       FlowLayout northLayout = new FlowLayout();
       northscreen.setLayout(northLayout);
      
       JPanel centreScreen = new JPanel();
       GridBagLayout flowManager = new GridBagLayout();
       GridBagConstraints pos = new GridBagConstraints();
       centreScreen.setLayout(flowManager);
      
        
       myScreen.add("North", northscreen);
       myScreen.add("Center", centreScreen);
      
       one.addActionListener(this);
        two.addActionListener(this);
         three.addActionListener(this);
          four.addActionListener(this);
           five.addActionListener(this);
            six.addActionListener(this);
             seven.addActionListener(this);
              eight.addActionListener(this);
               nine.addActionListener(this); // listeners for all my buttons etc
      
       centreScreen.add(one); // adding content to the window
       centreScreen.add(two);
       centreScreen.add(three);
       centreScreen.add(four);
       centreScreen.add(five);
       centreScreen.add(six);
       centreScreen.add(seven);
       centreScreen.add(eight);
       centreScreen.add(nine);

       pos.gridx = 0; pos.gridy = 1;
       centreScreen.add(one, pos);
      
       pos.gridx = 1; pos.gridy = 1;
       centreScreen.add(two, pos);
      
       pos.gridx = 2; pos.gridy = 1;
       centreScreen.add(three, pos);
        
       pos.gridx = 0; pos.gridy = 2;
       centreScreen.add(four, pos);
      
       pos.gridx = 1; pos.gridy = 2;
       centreScreen.add(five, pos);
      
       pos.gridx = 2; pos.gridy = 2;
       centreScreen.add(six, pos);
      
       pos.gridx = 0; pos.gridy = 3;
       centreScreen.add(seven, pos);
      
       pos.gridx = 1; pos.gridy = 3;
       centreScreen.add(eight, pos);
      
       pos.gridx = 2; pos.gridy = 3;
       centreScreen.add(nine, pos);
      
      
      
      
       setContentPane(myScreen);
       setVisible(true);

       } //end of graphic cd
      
        public void actionPerformed(ActionEvent event) {
        
        
      
              
        }// end of action performed
        
        public void itemStateChanged(ItemEvent event) {
}
      

  public static void main (String args[]) {
           PhilsPuzzle puzzle = new PhilsPuzzle();
           }
        }  

User is offlineProfile CardPM
+Quote Post

akela_p501usa
RE: 3 X 3 Grid Puzzle
25 May, 2007 - 06:10 AM
Post #2

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 1


My Contributions
Hi philipd:
You say your new to this, just think of me as not even close to you. Yet I'd still like to offer some kind of advice. Sure you're looking for a quick fix, someone to let you know exactly where you went wrong. Look at it this way if you're not in a race and you want to learn how to do it on your own not just let an expert correct your mistakes only you become the expert yourself in case things like this happen in the future you will be fully prepared. Look into some of the "Conferences". Here's a link where you can register for upcoming "Conferences", who knows maybe you can develop a support team for whom you can call directly on the phone better yet meet in person....
Here's the link.....\

http://java.sun.com/javaone/sf/index.jsp

Sure hope things work out for you
icon_up.gif
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: 3 X 3 Grid Puzzle
25 May, 2007 - 06:28 AM
Post #3

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
QUOTE

just think of me as not even close to you.

I don't know what you are trying to say with this, but it seems demeening to me. We are here to help, not refer everyone elsewhere. If all websites did that, noone would ever get any help.


As for the problem:
I would suggest you keep track of the empty button's position somehow, and when a numbered button is pressed, it's value is placed onto the empty, and update the empty position. Once you have this working, you can improve the rules of your game to only allow adjacent squares to be changed.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month