Memory Game - Please Help

i need help fixing my array for this memory game

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 3072 Views - Last Post: 30 November 2008 - 08:29 PM Rate Topic: -----

#1 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Memory Game - Please Help

Posted 28 November 2008 - 10:44 PM

 


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 November 2008 - 10:51 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Memory Game - Please Help

#2 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: Memory Game - Please Help

Posted 28 November 2008 - 10:52 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Re: Memory Game - Please Help

Posted 28 November 2008 - 10:55 PM

oh sorry here's what i have

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);
			}
   }
	
	
}
 

Was This Post Helpful? 0
  • +
  • -

#4 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: Memory Game - Please Help

Posted 28 November 2008 - 10:58 PM

try creating a random number generator and create the integers randomly and place them into the array.

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


Was This Post Helpful? 0
  • +
  • -

#5 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Re: Memory Game - Please Help

Posted 28 November 2008 - 11:03 PM

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
Was This Post Helpful? 0
  • +
  • -

#6 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: Memory Game - Please Help

Posted 28 November 2008 - 11:11 PM

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
Was This Post Helpful? 0
  • +
  • -

#7 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Re: Memory Game - Please Help

Posted 28 November 2008 - 11: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

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;
			} 

Was This Post Helpful? 0
  • +
  • -

#8 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Re: Memory Game - Please Help

Post icon  Posted 29 November 2008 - 08:40 AM

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.
  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);
			}
   }
	
	
}

Was This Post Helpful? 0
  • +
  • -

#9 windra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 08

Re: Memory Game - Please Help

Posted 29 November 2008 - 09:21 AM

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?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Memory Game - Please Help

Posted 29 November 2008 - 03:07 PM

View Postneedhelpbad, on 28 Nov, 2008 - 10:19 PM, said:

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

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

		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()));
		}	


Was This Post Helpful? 0
  • +
  • -

#11 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Re: Memory Game - Please Help

Posted 29 November 2008 - 10:59 PM

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!!!
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Memory Game - Please Help

Posted 29 November 2008 - 11:15 PM

Now you have a question
We'll have to guess it from your previous post....
Now here

	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
Was This Post Helpful? 0
  • +
  • -

#13 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Re: Memory Game - Please Help

Posted 29 November 2008 - 11: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
  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
Was This Post Helpful? 0
  • +
  • -

#14 needhelpbad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-November 08

Re: Memory Game - Please Help

Posted 29 November 2008 - 11:36 PM

thanks 4 the tip but if i do it that way i don't really understand what it does
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Memory Game - Please Help

Posted 29 November 2008 - 11:50 PM

View Postneedhelpbad, on 29 Nov, 2008 - 10:33 PM, said:

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
  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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2