Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

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

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




Memory Game - Please Help

2 Pages V  1 2 >  

Memory Game - Please Help, i need help fixing my array for this memory game

needhelpbad

28 Nov, 2008 - 09:44 PM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

CODE


ok i did that wrong n e way i made this memory game and its suppose to use an array of integers and place them in the cells of the game board randomly and you have to click to find a match only right now every square in my board has the same number and i can't figure out what i did wrong
I just need to know how to correctly randomize my board

This post has been edited by needhelpbad: 28 Nov, 2008 - 09:51 PM

User is offlineProfile CardPM
+Quote Post


itpro4470

RE: Memory Game - Please Help

28 Nov, 2008 - 09:52 PM
Post #2

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 131



Thanked: 1 times
Dream Kudos: 25
My Contributions
If you post the code it would really help. It sounds like however you're iterating through the Array is pulling the same value or possibly the contents of the Array is being loaded with the same value. I'd start my debugging by focusing on how the array is populated then how information is pulled from it.
User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

28 Nov, 2008 - 09:55 PM
Post #3

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

oh sorry here's what i have

CODE
import javax.swing.*;
import java.awt.*;
import java.lang.Math;

public class MemoryCharm extends java.applet.Applet
{
   private int xMouse;
   private int yMouse;
   private boolean mouseClicked = false;
   private boolean firstRun = true;
   private static final int WIDTH = 100;

    
  private int [][]array = new int [4][4];
private Color[] color = {Color.WHITE, Color.BLUE, Color.RED, Color.YELLOW, Color.DARK_GRAY, Color.GREEN, Color.ORANGE, Color.PINK};
  private int firstX, firstY, nextX, nextY;
    
    
    /*Sets the background of your memory board to black*/
    
   public void init()
   {
      setBackground(Color.BLACK);      
   }     
    
    
   public void paint(Graphics canvas)
   {
      if(firstRun) //for the first run we need to build our random board
      {
         buildBoard(4);
            displayBoard(canvas);
         firstRun = false;
      }
      else // once our board is built we will display the game
      {
         if (mouseClicked) // if the mouse has been clicked
         {
            displayHit(canvas);//find which box the user clicked
            mouseClicked = false;
         }
      }
   }
  

    
   public boolean mouseDown(Event e, int x, int y )
   {
      mouseClicked = true;
      xMouse = x;
      yMouse = y;
      repaint();
      return true;
   }
    
    
   public void update ( Graphics g )
   {
      paint(g);
   }


  public void buildBoard(int s)
   {
        int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
        
            for (int i = 0; i<values.length-1; i++)
            {
               int chosenNumber = (int)((values.length-i)*Math.random());
                  int temp = values[chosenNumber+i];
                values[chosenNumber+i] = values[i];
               values[i] = temp;
            }

        int counter = 0;
        
        for(int i = 0; i<4; i++)
        {
            for(int k = 0; k <4; k++)
            {
                array[i][k] = values[counter++];
            }
        }
              
  }
  

   public void displayBoard(Graphics canvas)
   {
      canvas.setColor(Color.WHITE);
       
      for(int i =0; i < 400; i+= WIDTH)
         for(int j = 0; j < 400; j+= WIDTH)
            canvas.drawRect(i, j, WIDTH, WIDTH);
   }

   public void displayHit(Graphics g)
   {
    for(int i = 0; i<4; i++)
        for(int j = 0; j<4; j++)
            {
                g.setColor(color[array[i][j]]);
            
            
   //    g.setColor(Color.WHITE);
        int x = xMouse/WIDTH;
        x *= WIDTH;
        int y = yMouse/WIDTH;
        y *= WIDTH;      
      g.fillOval(x+30, y+30, 40, 40);
            }
   }
    
    
}

User is offlineProfile CardPM
+Quote Post

itpro4470

RE: Memory Game - Please Help

28 Nov, 2008 - 09:58 PM
Post #4

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 131



Thanked: 1 times
Dream Kudos: 25
My Contributions
try creating a random number generator and create the integers randomly and place them into the array.

CODE

Random gen = new Random();
int[] intArray = new int[6];
for(int i = 0;i<intArray.Length;i++)
{
    intArray[i]=gen.nextInt();
}


User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

28 Nov, 2008 - 10:03 PM
Post #5

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

sorry guess i should have been a little more detailed im trying to use the numbers 1-7 so i created a 2d array that repeats all of the numbers (which will make the matches) i want the random number to be chosen from that array but each number should only be chosen once. I know that probably sound confusing so sorry i just been working on this for a long while and its kind of complicated to me too
User is offlineProfile CardPM
+Quote Post

itpro4470

RE: Memory Game - Please Help

28 Nov, 2008 - 10:11 PM
Post #6

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 131



Thanked: 1 times
Dream Kudos: 25
My Contributions
Ok I hope I'm understanding you correctly. I'd suggest:

create a separate array to hold the used values

use a random generator to generate a number in the range of the length of the array

check the other array to see if the value has been used if not then assign it

place the used value in the used array

User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

28 Nov, 2008 - 10:19 PM
Post #7

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

i did that or i thought i did
the original array i created was int array
then i created the array to hold the values (below)
and i attempted to randomize it

CODE
int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
        
            for (int i = 0; i<values.length-1; i++)
            {
               int chosenNumber = (int)((values.length-i)*Math.random());
                  int temp = values[chosenNumber+i];
                values[chosenNumber+i] = values[i];
               values[i] = temp;
            }

User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

29 Nov, 2008 - 07:40 AM
Post #8

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

If there is anyone out there that could help me with this i would be greatly appreciative. I'm creating a memory game in which you click the squares and try to find the matching circles. The colored circles are assigned to each block using a random 2D array. My problem is for some reason my array isn't working so instead of getting different colors i get the same color circle each time i click . Could someone please help me!!!!!! here's what i have.
CODE
  import javax.swing.*;
import java.awt.*;
import java.lang.Math;

public class MemoryCharm extends java.applet.Applet
{
   private int xMouse;
   private int yMouse;
   private boolean mouseClicked = false;
   private boolean firstRun = true;
   private static final int WIDTH = 100;

    
  private int [][]array = new int [4][4];
private Color[] color = {Color.WHITE, Color.BLUE, Color.RED, Color.YELLOW, Color.DARK_GRAY, Color.GREEN, Color.ORANGE, Color.PINK};
  private int firstX, firstY, nextX, nextY;
    
    
    /*Sets the background of your memory board to black*/
    
   public void init()
   {
      setBackground(Color.BLACK);      
   }    
    
    
   public void paint(Graphics canvas)
   {
      if(firstRun) //for the first run we need to build our random board
      {
         buildBoard(4);
            displayBoard(canvas);
         firstRun = false;
      }
      else // once our board is built we will display the game
      {
         if (mouseClicked) // if the mouse has been clicked
         {
            displayHit(canvas);//find which box the user clicked
            mouseClicked = false;
         }
      }
   }
  

    
   public boolean mouseDown(Event e, int x, int y )
   {
      mouseClicked = true;
      xMouse = x;
      yMouse = y;
      repaint();
      return true;
   }
    
    
   public void update ( Graphics g )
   {
      paint(g);
   }


  public void buildBoard(int s)
   {
        int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
        
            for (int i = 0; i<values.length-1; i++)
            {
               int chosenNumber = (int)((values.length-i)*Math.random());
                  int temp = values[chosenNumber+i];
                values[chosenNumber+i] = values[i];
               values[i] = temp;
            }

        int counter = 0;
        
        for(int i = 0; i<4; i++)
        {
            for(int k = 0; k <4; k++)
            {
                array[i][k] = values[counter++];
            }
        }
              
  }
  

   public void displayBoard(Graphics canvas)
   {
      canvas.setColor(Color.WHITE);
      
      for(int i =0; i < 400; i+= WIDTH)
         for(int j = 0; j < 400; j+= WIDTH)
            canvas.drawRect(i, j, WIDTH, WIDTH);
   }

   public void displayHit(Graphics g)
   {
    for(int i = 0; i<4; i++)
        for(int j = 0; j<4; j++)
            {
                g.setColor(color[array[i][j]]);
            
            
   //    g.setColor(Color.WHITE);
        int x = xMouse/WIDTH;
        x *= WIDTH;
        int y = yMouse/WIDTH;
        y *= WIDTH;      
      g.fillOval(x+30, y+30, 40, 40);
            }
   }
    
    
}

User is offlineProfile CardPM
+Quote Post

windra

RE: Memory Game - Please Help

29 Nov, 2008 - 08:21 AM
Post #9

New D.I.C Head
*

Joined: 27 Nov, 2008
Posts: 17

The array is working perfectly, I printed the results of the arrays after randomising, and it appears fine, the elements are randomised.

It also appears to be working in dispalyHit() too
g.setColor(color[array[i][j]]); gets a different color each time.

All i can think of is that maybe you need to repaint g after you set the color?

User is offlineProfile CardPM
+Quote Post

pbl

RE: Memory Game - Please Help

29 Nov, 2008 - 02:07 PM
Post #10

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(needhelpbad @ 28 Nov, 2008 - 10:19 PM) *

i did that or i thought i did
the original array i created was int array
then i created the array to hold the values (below)
and i attempted to randomize it

CODE
int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
        
            for (int i = 0; i<values.length-1; i++)
            {
               int chosenNumber = (int)((values.length-i)*Math.random());
                  int temp = values[chosenNumber+i];
                values[chosenNumber+i] = values[i];
               values[i] = temp;
            }



This is the "real" way of doing that type of stuff

CODE

        ArrayList<Integer> al = new ArrayList<Integer>();
        for(int i = 0; i <= 7; i++) {
            al.add(i);
            al.add(i);
        }

        int[] value = new int[al.size()];
        Random ran = new Random();
        for(int i = 0; i < value.length; i++) {
            value[i] = al.remove(ran.nextInt(al.size()));
        }    

User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

29 Nov, 2008 - 09:59 PM
Post #11

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

okay someone please help me i added the repaint method and my code still doesn't work the circles aren't matching instead im gettin a grid filled with all the same colored circles can anyone help me!!!
User is offlineProfile CardPM
+Quote Post

pbl

RE: Memory Game - Please Help

29 Nov, 2008 - 10:15 PM
Post #12

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Now you have a question
We'll have to guess it from your previous post....
Now here

CODE

    public void displayHit(Graphics g)
    {
        for(int i = 0; i<4; i++)
            for(int j = 0; j<4; j++)
            {
                g.setColor(color[array[i][j]]);
                //    g.setColor(Color.WHITE);
                int x = xMouse/WIDTH;
                x *= WIDTH;
                int y = yMouse/WIDTH;
                y *= WIDTH;      
                g.fillOval(x+30, y+30, 40, 40);
            }
    }

even if you change the color 16 times... you always draw your ovals at the same location one over the others

Don't know if it is related but these variables
private int firstX, firstY, nextX, nextY
are never used
User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

29 Nov, 2008 - 10:33 PM
Post #13

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

well what i was trying to do was randomize it so that each time u click a different square it would show a different color using each one twice so i tried to randomize my array
CODE
  public void buildBoard(int s)
   {
        int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
        
            for (int i = 0; i<values.length-1; i++)
            {
               int chosenNumber = (int)((values.length-i)*Math.random());
                  int temp = values[chosenNumber+i];
                values[chosenNumber+i] = values[i];
               values[i] = temp;
            }

        int counter = 0;
        
        for(int i = 0; i<4; i++)
        {
            for(int k = 0; k <4; k++)
            {
                array[i][k] = values[counter++];
            }
        }
              
  }
  


and i know i havent' used those variables yet ive changed the code a few times since then and haven't got back to that pact
User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

29 Nov, 2008 - 10:36 PM
Post #14

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

thanks 4 the tip but if i do it that way i don't really understand what it does
User is offlineProfile CardPM
+Quote Post

pbl

RE: Memory Game - Please Help

29 Nov, 2008 - 10:50 PM
Post #15

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(needhelpbad @ 29 Nov, 2008 - 10:33 PM) *

well what i was trying to do was randomize it so that each time u click a different square it would show a different color using each one twice so i tried to randomize my array
CODE
  public void buildBoard(int s)
   {
        int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
        
            for (int i = 0; i<values.length-1; i++)
            {
               int chosenNumber = (int)((values.length-i)*Math.random());
                  int temp = values[chosenNumber+i];
                values[chosenNumber+i] = values[i];
               values[i] = temp;
            }

        int counter = 0;
        
        for(int i = 0; i<4; i++)
        {
            for(int k = 0; k <4; k++)
            {
                array[i][k] = values[counter++];
            }
        }
              
  }
  


and i know i havent' used those variables yet ive changed the code a few times since then and haven't got back to that pact

So that was my first post: "how to randomize an array" but your problem is not there...
You can have randomize the array as many times as you want you are drawing the same oval 16 times at the same place
User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

29 Nov, 2008 - 10:55 PM
Post #16

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

ok so maybe my question is how do i match the location with the location of each click so that it also corresponds with the array??
User is offlineProfile CardPM
+Quote Post

pbl

RE: Memory Game - Please Help

29 Nov, 2008 - 10:58 PM
Post #17

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Duplicate topics... merged
Please avoid double posting mad.gif
User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

29 Nov, 2008 - 11:04 PM
Post #18

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

sorry i still kind of learning how to work the site. But anyway so my problem is with the location my circles how do i match the location of the click to correspond with my random array
User is offlineProfile CardPM
+Quote Post

pbl

RE: Memory Game - Please Help

29 Nov, 2008 - 11:10 PM
Post #19

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(needhelpbad @ 29 Nov, 2008 - 11:04 PM) *

sorry i still kind of learning how to work the site.

No problem at all
QUOTE(needhelpbad @ 29 Nov, 2008 - 11:04 PM) *

But anyway so my problem is with the location my circles how do i match the location of the click to correspond with my random array

Your random array is for colors not location... as far as I can see
You are drawing 16 ovals (of different colors) at the same location

User is offlineProfile CardPM
+Quote Post

needhelpbad

RE: Memory Game - Please Help

29 Nov, 2008 - 11:25 PM
Post #20

New D.I.C Head
*

Joined: 28 Nov, 2008
Posts: 12

ok well now i've completely gotten lost. so i changed the location but now i can't click in each square anymore i can only click one square can you tell me where i went wrong
CODE
public void displayHit(Graphics g)
   {    
        for(int i = 0; i<4; i++)
            for(int j = 0; j<4; j++)
            {
                g.setColor(color[array[i][j]]);
                
                int x = xMouse/WIDTH;
                x = WIDTH;
                int y = yMouse/WIDTH;
                y = WIDTH;      
              g.fillOval(x+30, y+30, 40, 40);
            }
   }

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:53AM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month