Results 1 to 8 of 8
  1. #1
    JordanJava1 Guest

    Default Memory Game Help

    My classroom summative is to create a game that incorporates all we've learned this semester. I have decided to make a memory game and I am using a generator to randomly generate a 4 by 4 array with different numbers from 1 to 16. I have all my buttons set up and the array code done but i need help setting each button to a different location of the array. Here is my coding so far. If someone could take the code and rewrite it so it works i would be very thankful!

    Java Code:
    import java.awt.*; 
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import java.util.Random;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.lang.Integer;
    import javax.swing.JButton;
    
    class tictactoe extends JFrame implements ActionListener{
      
      JFrame frame;         
      JPanel contentPanel;  
      JButton box1, box2, box3, box4, box5, box6, box7, box8, box9, box10, box11, box12, box13, box14, box15, box16, clearscore, clearplayfield;//variable for the button
    
      
      
      
      public tictactoe (String title){
        super( title );
     
        frame = new JFrame("Memory Game");
        frame.setSize(600, 700); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible( true );
        
        contentPanel = new JPanel(); 
        contentPanel.setLayout(new GridLayout(4, 4, 25,25));     
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));  
        
        box1 = new JButton( new ImageIcon (""));
        box1.setActionCommand("box1");
        box1.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box1.addActionListener(this);
        contentPanel.add(box1);
        
        box10 = new JButton("");
        box10.setActionCommand("box10");
        box10.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box10.addActionListener(this);
        contentPanel.add(box1);
        
        box11 = new JButton("");
        box11.setActionCommand("box11");
        box11.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box11.addActionListener(this);
        contentPanel.add(box1);
        
        box12 = new JButton("");
        box12.setActionCommand("box12");
        box12.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box12.addActionListener(this);
        contentPanel.add(box1);
        
        box13 = new JButton("");
        box13.setActionCommand("box13");
        box13.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box13.addActionListener(this);
        contentPanel.add(box1);
        
        box14 = new JButton("");
        box14.setActionCommand("box14");
        box14.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box14.addActionListener(this);
        contentPanel.add(box1);
        
        box15 = new JButton("");
        box15.setActionCommand("box15");
        box15.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box15.addActionListener(this);
        contentPanel.add(box1);
        
        box16 = new JButton("");
        box16.setActionCommand("box16");
        box16.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box16.addActionListener(this);
        contentPanel.add(box1);
        
        box2 = new JButton("");
        box2.setActionCommand("box2");
        box2.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box2.addActionListener(this);
        contentPanel.add(box2);
        
        box3 = new JButton("");
        box3.setActionCommand("box3");
        box3.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box3.addActionListener(this);
        contentPanel.add(box3);
        
        box4 = new JButton("");
        box4.setActionCommand("box4");
        box4.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box4.addActionListener(this);
        contentPanel.add(box4);
        
        box5 = new JButton("");
        box5.setActionCommand("box5");
        box5.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box5.addActionListener(this);
        contentPanel.add(box5);
        
        box6 = new JButton("");
        box6.setActionCommand("box6");
        box6.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box6.addActionListener(this);
        contentPanel.add(box6);
        
        box7 = new JButton("");
        box7.setActionCommand("box7");
        box7.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box7.addActionListener(this);
        contentPanel.add(box7);
        
        box8 = new JButton("");
        box8.setActionCommand("box8");
        box8.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box8.addActionListener(this);
        contentPanel.add(box8);
        
        box9 = new JButton("");
        box9.setActionCommand("box9");
        box9.setAlignmentX(JButton.CENTER_ALIGNMENT);
        box9.addActionListener(this);
        contentPanel.add(box9);     
    
                                 
        frame.setContentPane(contentPanel);  
        
        int array [][] = new int [4][4];
        
        array=FillArray(array);
       DisplayArray(array);
    
       
       
     
      
      }
      
      
      
      public static void DisplayArray (int array [][]){
      
      for (int row = 0; row<4; row++){
          for (int col = 0; col <4; col++){
            System.out.print (array[row][col] + "  ");
          }
          System.out.println();
        }
      }
      
       public static int[][] FillArray (int array [][]){
        Random randomGenerator = new Random();
        int temp = 0;
        boolean found = false;
        for (int row = 0; row<4; row++){
          for (int col = 0; col <4; col++){
    
             do{
             temp=randomGenerator.nextInt(16)+1;
             found = Search(temp,array);
             if (found == false){
                array[row][col]=temp;
                System.out.print (array[row][col] + "  ");
              }
           }while (found == true);
          }
          System.out.println();
        }
          
         
         return (array);
       }
    
       
       
       
    public static boolean Search(int number, int[][] array){
       boolean found = false;
       for (int row = 0; row<4; row++){
          for (int col = 0; col <4; col++){
            if (array[row][col] == number){
              System.out.println("The searched value is found at element " + row + col);
             found = true;
            }
         }
       }
       return(found);
    }
    
    public static String Searchcell(int number, int[][] array){
       String found = "";
       for (int row = 0; row<4; row++){
          for (int col = 0; col <4; col++){
            if (array[row][col] == number){
              System.out.println("The searched value is found at element " + row + col);
             found = row + "-" + col;
            }
         }
       }
       return(found);
    }
    
    
    
    
    
    
    public void actionPerformed( ActionEvent evt)  {
      
      
    
      
    
    
        
      }
      public static void main(String[]args){
        tictactoe program = new tictactoe("Tic Tac Toe");
      }
    }
    Last edited by JordanJava1; 12-10-2010 at 04:12 PM. Reason: show coding more clearly

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    1. Please use CODE tags and indent your code to make it readable.
    2. You need a collection for your JButtons -- either a JButton[] array or an ArrayList<JButton>.
    3. Use a loop to create your JButton objects.
    4. Write a method to shuffle your JButton objects and place them on your JPanel.
    5 (Most important). Do not use copy and paste when you are programming. If you follow this rule, you will realize that being tempted to break it is a signal of a problem in your design. In this case, it would have been a clue that all of those five-line initialization blocks should have been one five-line method in a loop, and you would have avoided the bug of writing
    Java Code:
            contentPanel.add(box1);
    eight different times. In the real world, blocks of code that have been copied and pasted are a maintenance nightmare. Don't do it. If you think that copy and paste is easier than designing a collection, a method, and a loop, then that just means you need more practice with collections, methods and loops, and this is a good opportunity to get some more practice.

    -Gary-

  3. #3
    JordanJava1 Guest

    Default

    Uhm, first of all I didn't copy and paste anything from anyone. This is all my own coding and I would like someone to take it and show me how to set each button as a location in the array. You are of no help right now by giving information I am not looking for.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    I was not accusing you of stealing from anyone. You copied and pasted your own code from one place to another, and in so doing, introduced logical errors (creating many JButton objects, but adding the same one multiple times), and ended up with an unworkable design.

    Maybe someday you'll understand that I was trying to help, and actually did give you helpful advice. But since you don't want my help, good luck to you.

    -Gary-

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JordanJava1 View Post
    You are of no help right now by giving information I am not looking for.
    OP, you are biting the hand that's feeding you. I join Gary in wishing much luck to you.
    Last edited by Fubarable; 12-11-2010 at 04:34 AM.

  6. #6
    JordanJava1 Guest

    Default

    When my program is finished I will post the final coding and show you both that it will run with 16 blocks of 5 line button codes. I do not care about that. Those button are set and are stable. I needed help with setting (eg. array[0][0] to box1). You did not help me with that. I do not care about what luck you''re wishing because it is useless. If you honestly care so much about this go bother someone who cares.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JordanJava1 View Post
    When my program is finished I will post the final coding and show you both that it will run with 16 blocks of 5 line button codes. I do not care about that. Those button are set and are stable. I needed help with setting (eg. array[0][0] to box1). You did not help me with that. I do not care about what luck you''re wishing because it is useless. If you honestly care so much about this go bother someone who cares.
    Please let me try to clarify a few things for you.

    First off we are all unpaid volunteers who help others with Java programming problems because we love programming and love sharing this enthusiasm and creative experience with others. Our only pay is to see projects blossom, to watch a member's programming expertise increase and finally to receive acknowledgement from others if something we posted was helpful to them.

    Often before we can help someone, we need to ask them to clarify their post or provide more information. While these types of post don't directly help the original poster, if the poster follows the recommendations, it will usually increase their chances of getting helpful posts from the requester or others.

    Often we see other glaring problems with posted code, that while perhaps not directly related to the original poster's problem at hand, is something that will cause significant problems now, or if not now, later in the completion of this project or even in future projects, and on seeing this we feel obligated to point this out to the poster. This was the spirit of Gary's first post, and again, while it may not have been directly helpful to you towards solving your current question, if you read his post in the spirit with which it was given, you will eventually find it helpful (and soon).

    One one extreme, if you find a post by a member to be abusive, then please report it, and we will likely delete it, and sometimes may ban the poster. If on the other hand you find a post here to be helpful, then please thank the poster. The forum members generally appreciate this gesture and will be more likely to help you in the future. The tricky thing is what to do in the middle -- if you find a post neither abusive, nor helpful to your direct problem. The smart course would be to acknowledge the post and thank the poster for volunteering their time and effort to consider your problem, again even if you didn't consider it to be helpful, and then explain what problems you're still having. Again, the members here would genuinely appreciate this, and again your gesture would go a long way towards motivating others here to help you further.

    To take the other tack though, to insult others for taking time to consider your problem and offer what they felt was helpful advice, such as by stating: "You are of no help right now by giving information I am not looking for", is only going to label you as someone that no one wants to help, that doesn't deserve help, and I'm sure that's not what you want to have happen. You may want to reappraise your approach here.

    Myself, I'll be more than happy to spend a Saturday afternoon studying your code and offering suggestions if I feel you are someone I want to help, and many here will feel the same, and so I will await your reply before putting in the effort. Just remember that your replies to me and to Gary are read by all the members, and that it may affect their decision on whether they want to help you or not.

    Again, much luck, whether you desire these wishes or not.
    Last edited by Fubarable; 12-11-2010 at 11:16 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by banned View Post
    OP Is clearly a jerk.
    I wouldn't go that far, as there are far worse statements he could have said and that we've seen here, but I would say that he's probably a little insensitive. Fortunately this can change.

    Cheers.
    Last edited by Fubarable; 12-13-2010 at 11:11 PM.

Similar Threads

  1. Memory game - swing
    By Vizualni in forum AWT / Swing
    Replies: 7
    Last Post: 07-27-2010, 07:56 PM
  2. Memory game
    By ronyyy in forum New To Java
    Replies: 9
    Last Post: 04-15-2010, 12:00 PM
  3. Help to make memory game :=)
    By arian88 in forum AWT / Swing
    Replies: 7
    Last Post: 10-15-2009, 07:23 AM
  4. Memory Game
    By torres in forum AWT / Swing
    Replies: 6
    Last Post: 04-22-2009, 12:00 AM
  5. help with memory game!
    By rac in forum New To Java
    Replies: 6
    Last Post: 04-14-2009, 12:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •